簡單計算器設計(WPF)


要求:

文本框居中,用戶不能修改運算結果 當用戶選擇不同的運算類型時 下方GroupBox的標題與所選運算類型相對應 且文本框數字立即清空 單擊【計算】按鈕時 如果文本框輸入的內容非法 結果文本框顯示問號

運行效果:

XAML:

 

 

后台代碼:

 1 namespace A._2._2
 2 {
 3     /// <summary>
 4     /// MainWindow.xaml 的交互邏輯
 5     /// </summary>
 6     public partial class MainWindow : Window
 7     {
 8         public MainWindow()
 9         {
10             InitializeComponent();
11         }
12 
13         private void Btn_Click(object sender, RoutedEventArgs e)
14         {
15             if(!int.TryParse(tb1.Text,out int a) || !int.TryParse(tb2.Text,out int b))
16             {
17                 tb3.Text = "?";
18             }else if (addbtn.IsChecked == true)
19             {
20                 tb3.Text = int.Parse(tb1.Text) + int.Parse(tb2.Text)+"";
21             }
22             else if (subbtn.IsChecked == true)
23             {
24                 tb3.Text = int.Parse(tb1.Text) - int.Parse(tb2.Text)+"";
25             }
26             else if (mulbtn.IsChecked == true)
27             {
28                 tb3.Text = int.Parse(tb1.Text) * int.Parse(tb2.Text)+"";
29             }
30             else if (divbtn.IsChecked == true)
31             {
32                 tb3.Text = int.Parse(tb1.Text) / int.Parse(tb2.Text)+"";
33             }
34             else if (delbtn.IsChecked == true)
35             {
36                 tb3.Text = int.Parse(tb1.Text) % int.Parse(tb2.Text)+"";
37             }
38         }
39 
40         private void Radiobtn_Click(object sender, RoutedEventArgs e)
41         {
42             if (addbtn.IsChecked == true)
43             {
44                 tbox.Text = "加法";
45                 lb1.Content = "+";
46                 tb1.Clear();
47                 tb2.Clear();
48                 tb3.Clear();
49             }
50             else if (subbtn.IsChecked == true)
51             {
52                 tbox.Text = "減法";
53                 lb1.Content = "-";
54                 tb1.Clear();
55                 tb2.Clear();
56                 tb3.Clear();
57             }
58             else if (mulbtn.IsChecked == true)
59             {
60                 tbox.Text = "乘法";
61                 lb1.Content = "*";
62                 tb1.Clear();
63                 tb2.Clear();
64                 tb3.Clear();
65             }
66             else if (divbtn.IsChecked == true)
67             {
68                 tbox.Text = "除法";
69                 lb1.Content = "/";
70                 tb1.Clear();
71                 tb2.Clear();
72                 tb3.Clear();
73             }
74             else if (delbtn.IsChecked == true)
75             {
76                 tbox.Text = "取模";
77                 lb1.Content = "%";
78                 tb1.Clear();
79                 tb2.Clear();
80                 tb3.Clear();
81             }
82         }
83     }
84 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM