简单计算器设计(WPF)
阅读原文时间:2023年07月09日阅读:2

要求:

文本框居中,用户不能修改运算结果 当用户选择不同的运算类型时 下方GroupBox的标题与所选运算类型相对应 且文本框数字立即清空 单击【计算】按钮时 如果文本框输入的内容非法 结果文本框显示问号

运行效果:

XAML:

后台代码:

namespace A._2._2
{
///

/// MainWindow.xaml 的交互逻辑 ///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

     private void Btn\_Click(object sender, RoutedEventArgs e)  
     {  
         if(!int.TryParse(tb1.Text,out int a) || !int.TryParse(tb2.Text,out int b))  
         {  
             tb3.Text = "?";  
         }else if (addbtn.IsChecked == true)  
         {  
             tb3.Text = int.Parse(tb1.Text) + int.Parse(tb2.Text)+"";  
         }  
         else if (subbtn.IsChecked == true)  
         {  
             tb3.Text = int.Parse(tb1.Text) - int.Parse(tb2.Text)+"";  
         }  
         else if (mulbtn.IsChecked == true)  
         {  
             tb3.Text = int.Parse(tb1.Text) \* int.Parse(tb2.Text)+"";  
         }  
         else if (divbtn.IsChecked == true)  
         {  
             tb3.Text = int.Parse(tb1.Text) / int.Parse(tb2.Text)+"";  
         }  
         else if (delbtn.IsChecked == true)  
         {  
             tb3.Text = int.Parse(tb1.Text) % int.Parse(tb2.Text)+"";  
         }  
     }

     private void Radiobtn\_Click(object sender, RoutedEventArgs e)  
     {  
         if (addbtn.IsChecked == true)  
         {  
             tbox.Text = "加法";  
             lb1.Content = "+";  
             tb1.Clear();  
             tb2.Clear();  
             tb3.Clear();  
         }  
         else if (subbtn.IsChecked == true)  
         {  
             tbox.Text = "减法";  
             lb1.Content = "-";  
             tb1.Clear();  
             tb2.Clear();  
             tb3.Clear();  
         }  
         else if (mulbtn.IsChecked == true)  
         {  
             tbox.Text = "乘法";  
             lb1.Content = "\*";  
             tb1.Clear();  
             tb2.Clear();  
             tb3.Clear();  
         }  
         else if (divbtn.IsChecked == true)  
         {  
             tbox.Text = "除法";  
             lb1.Content = "/";  
             tb1.Clear();  
             tb2.Clear();  
             tb3.Clear();  
         }  
         else if (delbtn.IsChecked == true)  
         {  
             tbox.Text = "取模";  
             lb1.Content = "%";  
             tb1.Clear();  
             tb2.Clear();  
             tb3.Clear();  
         }  
     }  
 }  复制

}