win8计算器

1.首先布局计算器页面,用grid stackpanel 布局,并绘制一个4*4的表格,用来放动态生成的控件.

<StackPanel Background="Gray">
            <TextBox TextWrapping="Wrap" Height="120" Margin="5,5,5,5" Name="txtInformation" KeyDown="txtInformation_KeyDown" FontSize="30" FontWeight="Bold" >
               
                </TextBox>
            <StackPanel Orientation="Horizontal">
            <Button Name="btnClear" Content="清零" Height="40" FontSize="20" Width="100" Margin="60,0,60,0" Click="btnClear_Click"></Button>
            <Button Name="btnBack" Content="BackSpace" FontSize="20" Click="btnBack_Click"></Button>
            </StackPanel>
            <Grid Name="keyHome" Loaded="keyHome_Loaded" >
             
                <Grid.Background>
                    <RadialGradientBrush>
                        <GradientStop Color="Black" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    </RadialGradientBrush>
                </Grid.Background>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
              
            </Grid>
        </StackPanel>


    2.禁用传统键盘上字符的响应

 private void txtInformation_KeyDown(object sender, KeyRoutedEventArgs e) //禁用传统键盘
        {
            if (e.Key < VirtualKey.Number0|| e.Key > VirtualKey.Number9)
            {
                if (e.Key < VirtualKey.NumberPad0 || e.Key > VirtualKey.NumberPad9)
                {
                    if (e.Key != VirtualKey.Back)
                    {
                        e.Handled = true;
                    }
                }
            }
        }

3.动态生成按钮

 private void keyHome_Loaded(object sender, RoutedEventArgs e)
        {
            txtInformation.Text = calculator.Sum.ToString();
            char[] keyName = new char[] { '7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '.', '=', '+' };
            for (int i = 0; i < keyHome.RowDefinitions.Count; i++)
            {
                for (int j = 0; j < keyHome.ColumnDefinitions.Count; j++)
                {
                    int numKey = ((i) * 4) + (j + 1) - 1;
                    Button bt = new Button
                    {

                        Content = keyName[numKey],
                        Height = 100,
                        Width=150,
                        FontSize = 50
                    };
                    if (bt.Content.ToString() == "+" || bt.Content.ToString() == "-" || bt.Content.ToString() == "*" || bt.Content.ToString() == "/")
                    {
                        bt.Click += new RoutedEventHandler(btn_add_Click);
                    }
                    else if (bt.Content.ToString() == "=")
                    {
                        bt.Click += new RoutedEventHandler(btn_an_Click);
                    }
                    else
                    {
                        bt.Click += new RoutedEventHandler(Button0_Click);
                    }
                    Grid.SetRow(bt, i);
                    Grid.SetColumn(bt, j);
                    keyHome.Children.Add(bt);


                }
            }
        }

 

4.各按钮的响应事件

 private void btn_an_Click(object sender, RoutedEventArgs e)
        {


            string dbSecond = txtInformation.Text;
            string[] dbnum = dbSecond.Split(calculator.StrOper);
            if (dbnum.Length > 1)
            {
                try
                {
                    calculator.Num1 = Convert.ToDouble(dbnum[0]);
                    if (dbnum[1] == "")
                    {
                        calculator.Num2 = -Convert.ToDouble(dbnum[2]);
                    }
                    calculator.Num2 = Convert.ToDouble(dbnum[1]);
                }
                catch
                {
                }
            }
            //calculator.Num2 = dbSecond;

            switch (calculator.StrOper) //输入了第二运算数,按上次记录的运算符号运算
            {
                case '+':
                    calculator.Sum = calculator.Num1 + calculator.Num2;
                    break;
                case '-':
                    calculator.Sum = calculator.Num1 - calculator.Num2;
                    break;
                case '*':
                    calculator.Sum = calculator.Num1 * calculator.Num2;
                    break;
                case '/':
                    if (calculator.Num2 != 0)
                    {
                        calculator.Sum = calculator.Num1 / calculator.Num2;
                    }
                    else
                    {
                        MessageDialog message = new MessageDialog("老师说了,零不能作为被除数");
                        message.ShowAsync();
                    }
                    break;
                default:
                    calculator.Sum = calculator.Num2;
                    break;
            }
            if (calculator.Sum != calculator.Num2)
            {
                string s = calculator.StrOper.ToString(); ;
                txtInformation.Text = calculator.Sum.ToString();
            }
            calculator.Num1 = calculator.Sum;
        }
        private void btn_click(object sender, RoutedEventArgs e)
        {
            Button b1 = (Button)sender;


            if (b1.Content.ToString() == "+" || b1.Content.ToString() == "-" || b1.Content.ToString() == "*" || b1.Content.ToString() == "/")
            {
                //MessageBox.Show("ok");
            }

        }
        private void btn_add_Click(object sender, RoutedEventArgs e)//运算符号按钮单击事件
        {
            //记录此次运算符号
            Button btn = (Button)sender;
            if (!txtInformation.Text.Contains("+") && !txtInformation.Text.Contains("--") && !txtInformation.Text.Contains("*") && !txtInformation.Text.Contains("/"))
            {
                calculator.StrOper = (char)btn.Content;
                txtInformation.Text += calculator.StrOper;

            }
            calculator.PointNum = 0;
        }

        private void Button0_Click(object sender, RoutedEventArgs e)  //按钮0-9单击事件及小数点
        {

            Button b1 = (Button)sender;
            if (b1.Content.ToString() == ".")
            {
                calculator.PointNum += 1;
            }
            if (calculator.PointNum > 1)
            {
                if (b1.Content.ToString() != ".")
                {
                    txtInformation.Text += b1.Content;
                }
            }
            else if (txtInformation.Text != "0")
            {
                txtInformation.Text += b1.Content;

            }
            else
            {
                txtInformation.Text = b1.Content.ToString();
            }
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            if (txtInformation.Text != "0")
            {
                txtInformation.Text = "0";
                calculator.PointNum = 0;
                calculator.Num1 = 0;
                calculator.Num2 = 0;
            }
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            if (txtInformation.Text.Length > 0 && txtInformation.Text != "0")
            {
                txtInformation.Text = txtInformation.Text.Substring(0, txtInformation.Text.Length - 1);
            }
        }

 

本作品参照:传智播客.net培训Windows 8开发视频教程

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值