先入正題吧!
1:創(chuàng)建wpf應(yīng)用。我用的VS是2013!
2:創(chuàng)建wpf文件并且起名字!
3:起一條虛線
3:打開我們電腦基本的計算器,然后我們模仿他來做計算器吧!
4:在工具箱找到lable,把他拖進來。并且起一個名字!設(shè)置他的屬性。
4:再然后在工具箱找到button按鈕把他拖進來。并且?guī)退鹈?,還有設(shè)置他的屬性!
5:寫完整的界面,按鈕直接復(fù)制粘貼就可以的!下一步進行后臺代碼!
6:定義三個變量存放數(shù)據(jù)!
7:設(shè)置按鈕0的點擊事件!然后數(shù)字鍵復(fù)制粘貼就可以了!
8:設(shè)置符號鍵,給他賦值!
9:設(shè)置C按鍵清除所有!這個問了很多人才找到的C按鍵清除lable!
10:設(shè)置等于號按鍵促發(fā)加減乘除!
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;//定義為存儲結(jié)果。
????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;
????}
??}
}
根據(jù)大家的需求。以前寫的計算器是不可以連續(xù)計算的?,F(xiàn)在已經(jīng)更改可以連續(xù)計算了。只是添加了幾個判斷。很簡單的。
這個是代碼,有注釋哦。
第一:在計算器的數(shù)字進行判斷。
第二:在等號的方法進行判斷。超級簡單易懂。
有需要源碼的也可以聯(lián)系我。有一次滿足的大眾的需求。哈哈哈哈哈~~~~
這個是項目的源碼下載地:
如果有不足的地方歡迎多多指教!記得我是一個熱愛編程的男孩子!有錯誤的地方多多指點!我是一個剛剛畢業(yè)入門的應(yīng)屆生!希望我們一起進步!有問題可以咨詢我!歡迎給我留言!