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開發視頻教程

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章