C#窗体标准计算器(下) 初级新手请多担待。

之前看到C#窗体标准计算器(上) 初级新手请多担待。

如果没看到过点击上面蓝色字跳转查看前一步。

我们先打开程序的代码开始添加事件


首先我们定义全局变量

 double c, d;
 string m;

然后我们回到窗体中双击按钮添加事件

 private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text += button1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text += button2.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text += button3.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text += button4.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text += button5.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text += button6.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text += button7.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text += button8.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text += button9.Text;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            textBox1.Text += button10.Text;
        }

下面是·的代码与1到0一致。

 private void button12_Click(object sender, EventArgs e)
        {
            textBox1.Text += button12.Text;
        }

这个是按钮1到0在textbox1 中显示如果点击按钮1到0之前任何一个数字将显示


数字的事件添加完了我们给等号添加一个事件

 private void button16_Click(object sender, EventArgs e)
        {
            d= Convert.ToDouble(textBox1.Text);
            switch (m)
            {
                case ("+"):
                    textBox1.Text = Convert.ToString(c + d);
                    break;
                case ("-"):
                    textBox1.Text = Convert.ToString(c - d);
                    break;
                case ("*"):
                    textBox1.Text = Convert.ToString(c * d);
                    break;
                case ("/"):
                    textBox1.Text = Convert.ToString(c / d);
                    break;
            }
            textBox2.Text = " ";
        }
然后给加减乘除添加事件让等号可以运算
  {
            c = Convert.ToDouble(textBox1.Text);
            textBox1.Text = " ";
            m = button13.Text;
            textBox2.Text += c + m;
        }

        private void button20_Click(object sender, EventArgs e)
        {
            c = Convert.ToDouble(textBox1.Text);
            textBox1.Text = " ";
            m = button20.Text;
            textBox2.Text += c + m;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            c = Convert.ToDouble(textBox1.Text);
            textBox1.Text = " ";
            m = button14.Text;
            textBox2.Text += c + m;
        }

        private void button15_Click(object sender, EventArgs e)
        {
            c = Convert.ToDouble(textBox1.Text);
            textBox1.Text = " ";
            m = button15.Text;
            textBox2.Text += c + m;
        }
然后我们给C和CE的按钮添加清除
  private void button17_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void button18_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }
关闭按钮添加事件
private void button11_Click(object sender, EventArgs e)
        {
            this.Close();
        }

回退按钮添加事件(←这个符号的按钮事件)注:这个是删除一个数字

 private void button19_Click(object sender, EventArgs e)
        {
            try
            {
                textBox1.Text = textBox1.Text.Substring(0, textBox1.TextLength - 1);
            }
            catch
            {
 
            }
        }

然后我们保存一下我们的程序也就写完了。

演示:


作者新手请多多担待。

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是一个简单的C#窗体面积计算器的示例代码: ```csharp using System; using System.Windows.Forms; namespace AreaCalculator { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCalculate_Click(object sender, EventArgs e) { if (cmbShape.SelectedIndex == 0) // 计算圆面积 { double radius; if (double.TryParse(txtRadius.Text, out radius)) { double area = Math.PI * radius * radius; txtResult.Text = area.ToString("f2"); } else { MessageBox.Show("输入有效的半径!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); txtRadius.Focus(); } } else if (cmbShape.SelectedIndex == 1) // 计算球表面积 { double radius; if (double.TryParse(txtRadius.Text, out radius)) { double area = 4 * Math.PI * radius * radius; txtResult.Text = area.ToString("f2"); } else { MessageBox.Show("输入有效的半径!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); txtRadius.Focus(); } } } } } ``` 这是一个简单的面积计算器,支持计算圆和球的面积。在窗体上放置了一个组合框(用于选择要计算的形状)、两个文本框(用于输入半径和显示结果)和一个计算按钮。当用户点击计算按钮时,程序会根据选择的形状计算相应的面积,并将结果显示在文本框中。如果用户没有输入有效的半径,程序会弹出一个错误提示框,并将焦点设置到半径文本框中。 希望这个示例代码能够帮助到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值