如何使我的WinForms程序中的“计算器”按钮正常工作?

时间:2010-03-15 12:25:44

标签: c#

我使用C#和WinForms编写了一个简单的计算器程序。但它并没有真正做任何有用的事情。我该如何处理计算器按钮?

我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lab8ass1
{
    public partial class Form1 : Form
    {

    public Form1()
    {
        InitializeComponent();
    }

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

    private void btnone_Click(object sender, EventArgs e)
    {
        textBox1.Text += "1";
    }

    private void btnrwo_Click(object sender, EventArgs e)
    {
        textBox1.Text += "2";
    }

    private void btnthree_Click(object sender, EventArgs e)
    {
        textBox1.Text += "3";
    }

    private void btnfour_Click(object sender, EventArgs e)
    {
        textBox1.Text += "4";
    }

    private void btnfive_Click(object sender, EventArgs e)
    {
        textBox1.Text += "5";
    }

    private void btnsix_Click(object sender, EventArgs e)
    {
        textBox1.Text += "6";
    }

    private void btnseven_Click(object sender, EventArgs e)
    {
        textBox1.Text += "7";
    }

    private void btneight_Click(object sender, EventArgs e)
    {
        textBox1.Text += "8";
    }

    private void btnnine_Click(object sender, EventArgs e)
    {
        textBox1.Text += "9";
    }

    private void btnzero_Click(object sender, EventArgs e)
    {
        textBox1.Text += "0";
    }
    string s1,s;
    private void btnplus_Click(object sender, EventArgs e)
    {
        s1 = textBox1.Text;
        textBox1.Text = "";
        s = textBox1.Text;

        //textBox1.Text
    }


    private void btnequal_Click(object sender, EventArgs e)
    {

    }


  }

}

1 个答案:

答案 0 :(得分:7)

您可以使用:

private void allButtons_Click(object sender, EventArgs e)
{
    Button b = sender as Button;
    textBox1.Text += b.Text;
}

并将此处理程序用于所有按钮(其文本要附加到textBox1)