WPF计算器设计

今天做了一个简单的加减乘除计算器:通过comboBox来选择计算方式,把编辑框内的两个数据进行计算,并显示到最终的编辑框中。

设计代码:

 1 <Window x:Class="Calculator.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="MainWindow" Height="350" Width="500">
 5     <Grid Background="LightBlue">
 6         <TextBox x:Name="textBox1" HorizontalAlignment="Left" Margin="10,32,0,0" VerticalAlignment="Top" Width="400"  Background="BurlyWood"/>
 7         <TextBox x:Name="textBox2" HorizontalAlignment="Left" Margin="10,79,0,0"  VerticalAlignment="Top" Width="400" Background="GreenYellow"/>
 8         <TextBox x:Name="textBox3" HorizontalAlignment="Left" Margin="10,126,0,0"  VerticalAlignment="Top" Width="400" Background="Goldenrod"/>
 9         <Label Content="Data:" HorizontalAlignment="Left" Margin="10,1,0,0" VerticalAlignment="Top"/>
10         <Label Content="Data:" HorizontalAlignment="Left" Margin="10,59,0,0" VerticalAlignment="Top"/>
11         <Label Content="Result:" HorizontalAlignment="Left" Margin="10,106,0,0" VerticalAlignment="Top"/>
12         <Label Content="Method:" HorizontalAlignment="Left" Margin="10,150,0,0" VerticalAlignment="Top"/>
13         <Button Content="Calculate" HorizontalAlignment="Left" Margin="150,250,0,0" VerticalAlignment="Top" Width="80" Background="Green" Click="Click_Calculator" />
14         <Button Content="Exit" HorizontalAlignment="Left" Margin="300,250,0,0" VerticalAlignment="Top" Width="80" Background="CadetBlue" Click="Click_Exit"/>
15         <ComboBox x:Name="method" HorizontalAlignment="Left" Margin="25,171,0,0" VerticalAlignment="Top" Width="120">
16             <ComboBoxItem>Add </ComboBoxItem>
17             <ComboBoxItem>subtraction</ComboBoxItem>
18             <ComboBoxItem> multiplication </ComboBoxItem>
19             <ComboBoxItem>division</ComboBoxItem>
20         </ComboBox>
21 
22     </Grid>
23 </Window>

cs代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 using System.Windows.Controls;
 8 using System.Windows.Data;
 9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15 
16 namespace Calculator
17 {
18     /// <summary>
19     /// MainWindow.xaml 的交互逻辑
20     /// </summary>
21     public partial class MainWindow : Window
22     {
23         public MainWindow()
24         {
25             InitializeComponent();
26         }
27 
28         private void Click_Calculator(object sender, RoutedEventArgs e)
29         {
30             double fdat0,fdat1,fdat2;
31             string str0 = textBox1.Text;
32             string str1 = textBox2.Text;
33             try
34             {
35                 fdat0 = Convert.ToDouble(str0);
36                 fdat1 = Convert.ToDouble(str1);
37                 if (method.SelectedIndex == 0)///add
38                 {
39                     fdat2 = fdat0 + fdat1;
40                 }
41                 else if (method.SelectedIndex == 1)///subtraction
42                 {
43                     fdat2 = fdat0 - fdat1;
44                 }
45                 else if (method.SelectedIndex == 2)///multiplication
46                 {
47                     fdat2 = fdat0 * fdat1;
48                 }
49                 else if (method.SelectedIndex == 3)///division
50                 {
51                     fdat2 = fdat0 / fdat1;
52                 }
53                 else
54                 {
55                     fdat2 = fdat0 + fdat1;
56                 }
57                 textBox3.Text = Convert.ToString(fdat2);
58             }
59             catch (Exception ex)
60             {
61                 MessageBox.Show("A handled exception just occurred: " + ex.Message, "Exception Sample", MessageBoxButton.OK, MessageBoxImage.Warning);
62             }
63         }
64         private void Click_Exit(object sender, RoutedEventArgs e)
65         {
66             App.Current.Shutdown();
67         }
68     }
69 }

运行结果:

谢谢.

posted on 2019-10-11 11:24  Milo_lu  阅读(317)  评论(0编辑  收藏  举报

导航