简单计算器设计(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 }

相关文章