C#WPF计算器!一个刚刚入门一个星期Csharp的应届生!
(C#WPF计算器!一个刚刚入门一个星期Csharp的应届生!)
先入正题吧!
1:创建wpf应用。我用的VS是2013!
2:创建wpf文件并且起名字!
3:起一条虚线
3:打开我们电脑基本的计算器,然后我们模仿他来做计算器吧!
4:在工具箱找到lable,把他拖进来。并且起一个名字!设置他的属性。
4:再然后在工具箱找到button按钮把他拖进来。并且帮他起名字,还有设置他的属性!
5:写完整的界面,按钮直接复制粘贴就可以的!下一步进行后台代码!
6:定义三个变量存放数据!
7:设置按钮0的点击事件!然后数字键复制粘贴就可以了!
8:设置符号键,给他赋值!
9:设置C按键清除所有!这个问了很多人才找到的C按键清除lable!
10:设置等于号按键促发加减乘除!
11:然后一个计算器完成了!下面是全部代码!
namespaceLVcalculation
{
??///
??///MainWindow.xaml的交互逻辑
??///
??publicpartialclassMainWindow:Window
??{
????stringnumber1=null,number2=null,flag=null;//第一个变量存放按下的第一个字符,第二个变量存放按下符号键的字符。第三个存放符号的字符!
????publicMainWindow左边()右边
????{
??????InitializeComponent左边()右边;
????}
????privatevoidbutton0_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"0";
????????lable1.Content=number1;
??????}
??????else{
????????number2=number2+"0";
????????lable1.Content=number2;
???????
??????}
????}
????privatevoidbutton1_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"1";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"1";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton2_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"2";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"2";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton3_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"3";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"3";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton4_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"4";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"4";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton5_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"5";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"5";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton6_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"6";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"6";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton7_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"7";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"7";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton8_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"8";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"8";
????????lable1.Content=number2;
??????}
????}
????privatevoidbutton9_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+"9";
????????lable1.Content=number1;
??????}
??????else
??????{
????????number2=number2+"9";
????????lable1.Content=number2;
??????}
????}
????privatevoidbuttonb_Click(objectsender,RoutedEventArgse)
????{
??????flag="-";
????}
????privatevoidbuttona_Click(objectsender,RoutedEventArgse)
????{
??????flag="+";
????}
????privatevoidbuttone_Click(objectsender,RoutedEventArgse)
????{
??????flag="%";
????}
????privatevoidbuttond_Click(objectsender,RoutedEventArgse)
????{
??????flag="/";
????}
????privatevoidbuttonc_Click(objectsender,RoutedEventArgse)
????{
??????flag="*";
????}
????privatevoidbuttoncc_Click(objectsender,RoutedEventArgse)
????{
??????this.lable1.Content="";
????}
????privatevoidbuttongg_Click(objectsender,RoutedEventArgse)
????{
??????if(flag==null)
??????{
????????number1=number1+".";
????????lable1.Content=number1;
??????}
??????else{
????????number2=number2+".";
????????lable1.Content=number2;
???????
??????}
????}
????privatevoidbuttonf_Click(objectsender,RoutedEventArgse)
????{
??????switch(flag){
????????case"+":lable1.Content=Convert.ToString(Convert.ToDouble(number1)+Convert.ToDouble(number2));
??????????break;
????????case"-":lable1.Content=Convert.ToString(Convert.ToDouble(number1)-Convert.ToDouble(number2));
??????????break;
????????case"*":lable1.Content=Convert.ToString(Convert.ToDouble(number1)*Convert.ToDouble(number2));
??????????break;
????????case"/":lable1.Content=Convert.ToString(Convert.ToDouble(number1)/Convert.ToDouble(number2));
??????????break;
????????case"%":lable1.Content=Convert.ToString(Convert.ToDouble(number1)%Convert.ToDouble(number2));
??????????break;
??????}
??????number1=null;
??????number2=null;
??????flag=null;
????}
??}
}
12:还有一种方法写!注意这个方法的是用textbox,不是用lable。
13:下面是这个的全部代码!
????xmlns="" ????xmlns:x="" ????Title="为什么我写的计算器这么多bug"Height="350"MaxHeight="350"MinHeight="350"Width="525"MaxWidth="525"MinWidth="525" ????> ?? ????? ???? ?????? ?????? ?????? ?????? ?????? ?????? ???? ???? ?????? ?????? ?????? ?????? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ????? ??
namespaceWpfApplication1
{
??///
??///MainWindow.xaml的交互逻辑
??///
??publicpartialclassMainWindow:Window
??{
????doublea;//定义为存储结果。
????doubleb;//定义为第一位操作符。
????boolc=false;
????stringd;//定义为操作运算符号。
????publicMainWindow左边()右边
????{
??????InitializeComponent左边()右边;
????}
????privatevoidbuttonCC_Click(objectsender,RoutedEventArgse)
????{
??????textbox1.Text="";
????}
????privatevoidbutton0_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true){
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="0";
????}
????privatevoidbutton1_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true){
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="1";
????}
????privatevoidbutton2_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="2";
????}
????privatevoidbutton3_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="3";
????}
????privatevoidbutton4_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="4";
????}
????privatevoidbutton5_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="5";
????}
????privatevoidbutton6_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="6";
????}
????privatevoidbutton7_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="7";
????}
????privatevoidbutton8_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="8";
????}
????privatevoidbutton9_Click(objectsender,RoutedEventArgse)
????{
??????if(c==true)
??????{
????????textbox1.Text="";
????????c=false;
??????}
??????textbox1.Text+="9";
????}
????privatevoidbuttonc_Click(objectsender,RoutedEventArgse)
????{
??????c=true;
??????b=double.Parse(textbox1.Text);
??????d="*";
????}
????privatevoidbuttona_Click(objectsender,RoutedEventArgse)
????{
??????c=true;
??????b=double.Parse(textbox1.Text);
??????d="+";
????}
????privatevoidbuttonb_Click(objectsender,RoutedEventArgse)
????{
??????c=true;
??????b=double.Parse(textbox1.Text);
??????d="-";
????}
????privatevoidbuttond_Click(objectsender,RoutedEventArgse)
????{
??????c=true;
??????b=double.Parse(textbox1.Text);
??????d="/";
????}
????privatevoidbuttonf_Click(objectsender,RoutedEventArgse)
????{
??????c=true;
??????b=double.Parse(textbox1.Text);
??????d="%";
????}
????privatevoidbuttone_Click(objectsender,RoutedEventArgse)
????{
??????switch(d){
????????case"+":a=b+double.Parse(textbox1.Text);
??????????break;
????????case"-":a=b-double.Parse(textbox1.Text);
??????????break;
????????case"*":a=b*double.Parse(textbox1.Text);
??????????break;
????????case"/":a=b/double.Parse(textbox1.Text);
??????????break;
????????case"%":a=b%double.Parse(textbox1.Text);
??????????break;
??????}
??????textbox1.Text=a+"";
??????c=true;
????}
??}
}
根据大家的需求。以前写的计算器是不可以连续计算的。现在已经更改可以连续计算了。只是添加了几个判断。很简单的。
这个是代码,有注释哦。
第一:在计算器的数字进行判断。
第二:在等号的方法进行判断。超级简单易懂。
有需要源码的也可以联系我。有一次满足的大众的需求。哈哈哈哈哈~~~~
这个是项目的源码下载地:
如果有不足的地方欢迎多多指教!记得我是一个热爱编程的男孩子!有错误的地方多多指点!我是一个刚刚毕业入门的应届生!希望我们一起进步!有问题可以咨询我!欢迎给我留言!
声明:本站部分内容和图片来源于互联网,经本站整理和编辑,版权归原作者所有,本站转载出于传递更多信息、交流和学习之目的,不做商用不拥有所有权,不承担相关法律责任。若有来源标注存在错误或侵犯到您的权益,烦请告知网站管理员,将于第一时间整改处理。管理员邮箱:y569#qq.com(#转@)
相关内容
- OPPO开40万年薪 招应届生芯片人才 芯片涨价带火了芯片人才
- 函授本科对于入户有什么帮助吗?
- 纯积分入户:应届生积分不够如何落户上海
- 2022年应届毕业生,拒签三方协议,还有毕业的希望吗?
- 高考也考不上清北考上了清北也没上海户口有了落户上海也买不起房
- 上海博士落户需要应届毕业生吗?
- 2016年应届生落户上海通过以后的手续:一些信息和疑问
- 应届毕业生上海落户积分计算表(共3页)
- 2020应届毕业生进沪就业,满72分可落户上海!积分标准和申请指南详见→
- 上海应届生落户两年内离职,被公司索赔10万,合理吗?
热门文章
- 应届生刚毕业究竟选择国企还是私企?
- 应届生落户办理哪里好
- 应届生落户政策小科普(北上广深、其他热门城市)
- 2019上海市选调应届优秀大学毕业生公告?汇总进入阅读模式
- 上海应届生落户新政策2022:至少需要72分以上,你能打几分?
- 企业校招青睐应届,择业期毕业生怎么办?省教育厅这样说……
- @高校应届毕业生,上海地铁等你来
- 大厂都裁员了,应届生入职该怎么选!千万别浪费你的“应届特权”
- 校招还是社招?应届毕业生该怎么选?这三点可以防忽悠,值得收藏
- 2022年沪应届毕业生落户流程
最新文章
- 厉害了!千万别小看深户,在深圳没它真不行!
- 2022年!落户深圳还有机会吗?
- 深圳福田区支持私募政策调整:最高千万元落户支持等暂缓执行
- 一篇文让你彻底搞懂香港身份&内地户口的关系!
- 好消息!深圳全市政务服务自助终端可以提取公积金啦
- 办理深圳户口最爱问的8个问题
- 入户深圳福利来了,你确定不领取吗?
- 深圳先进院、深理工这些学子就业去向明确 近半毕业生进入世界500强
- 深圳成人教育学历对入户的帮助
- 42位人才入围!深圳今年第三批次领军人才住房补贴名单出炉
推荐资讯
- 没有社保能落户深圳吗?有学历也不行,入户对社保要求真的很高
- 40岁以下,没有学历怎么快速入深户?
- 深圳户口在哪个区好办理?2022落户深圳,选择哪个区比较好?
- 深圳公租房怎么申请,流程是什么
- 重磅!非深户也能个人缴社保了
- 深圳南山公安推出24小时全新户政咨询服务
- 深圳市网约车:驾驶员需有深户或居住证 取消纯电动车续航限制
- 深圳入户人才引进补贴申请流程,最高六万!一次性到账
- 五一致敬劳动者!养老金精华帖请收藏!如非深户能在深办退休吗?
- 2022年深圳积分入户要求!你知道了吗