窗口输入不断显示

时间:2013-09-02 02:20:52

标签: c# windows

我不能为我的生活弄清楚如何连续显示输入然后输出数字。

你怎么做才能将数字存储在某个地方然后显示出来?我已经进入了这么多圈子,我已经把自己弄糊涂了。 我知道需要做什么,但不知道如何或确切地去做什么?

public partial class Form1 : Form
{
    char c;
    double num1;
    double num2;

    public Form1()
    {
        InitializeComponent();
    }
    private void btn0_Click(object sender, EventArgs e)
    {
        txtBox.Text += 0;
    }
    private void btn1_Click(object sender, EventArgs e)
    {
        txtBox.Text += 1;
    }
    private void btn2_Click(object sender, EventArgs e)
    {
        txtBox.Text += 2;
    }
    private void btn3_Click(object sender, EventArgs e)
    {
        txtBox.Text += 3;
    }
    private void btn4_Click(object sender, EventArgs e)
    {
        txtBox.Text += 4;
    }
    private void btn5_Click(object sender, EventArgs e)
    {
        txtBox.Text += 5;
    }
    private void btn6_Click(object sender, EventArgs e)
    {
        txtBox.Text += 6;
    }
    private void btn7_Click(object sender, EventArgs e)
    {
        txtBox.Text += 7;
    }
    private void btn8_Click(object sender, EventArgs e)
    {
        txtBox.Text += 8;
    }
    private void btn9_Click(object sender, EventArgs e)
    {
        txtBox.Text += 9;
    }
    private void btnDecimal_Click(object sender, EventArgs e)
    {
        if (!txtBox.Text.Contains('.'))
            txtBox.Text += '.';
    }
    private void btnAddition_Click(object sender, EventArgs e)
    {
        c = '+';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnSubtraction_Click(object sender, EventArgs e)
    {
        c = '-';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnMultiplication_Click(object sender, EventArgs e)
    {
        c = '*';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnDivision_Click(object sender, EventArgs e)
    {
        c = '/';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnClear_Click(object sender, EventArgs e)
    {
        txtBox.Clear();
    }
    private void btnEqual_Click(object sender, EventArgs e)
    {

        num2 = double.Parse(txtBox.Text);
        double result;
        switch (c)
        {
            case '+':
                result = num1 + num2;
                txtBox.Text = result.ToString();
                break;
            case '-':
                result = num1 - num2;
                txtBox.Text = result.ToString();
                break;
            case '/':
                if (num2 != 0)
                {
                    result = num1 / num2;
                    txtBox.Text = result.ToString();
                }
                else
                {
                    txtBox.Text = "You can't divide by zero... sign up for Math 100 please =)";
                }
                break;
            case '*':
                result = num1 * num2;
                txtBox.Text = result.ToString();
                break;
        }
    }
}

}

1 个答案:

答案 0 :(得分:2)

我会让你把它连接起来,但这就是我扔在一起的东西。

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private string op;
    private string num1;
    private string num2;

    private void handleNumberButtonClick(object sender, System.EventArgs e)
    {
        var num = Convert.ToInt16(((Button)sender).Tag);
        if (op == null)
            num1 += num;
        else
            num2 += num;
        PrintEquation(num1, op, num2);
    }

    private void PrintEquation(string first, string oper = null, string second = null, string equals = null, string result = null)
    {
        txtBox.Text = first + oper + second + equals + result;
    }

    private void handleOperatorButtonClick(object sender, System.EventArgs e)
    {
        op = ((Button)sender).Tag.ToString();
        PrintEquation(num1, op);
    }

    private void btnDecimal_Click(object sender, EventArgs e)
    {
        if (op == null && !num1.Contains(".")) num1 += ".";
        if (op != null && !num2.Contains(".")) num2 += ".";
        this.PrintEquation(num1, op, num2);
    }
    private void btnClear_Click(object sender, EventArgs e)
    {
        txtBox.Clear();
        op = null;
        num1 = null;
        num2 = null;
    }
    private void btnEqual_Click(object sender, EventArgs e)
    {
        double result = 0;
        var first = Convert.ToDouble(num1);
        var second = Convert.ToDouble(num2);
        switch (op)
        {
            case "+":
                result = first + second;
                break;
            case "-":
                result = first - second;
                break;
            case "/":
                if (second != 0)
                {
                    result = first / second;
                }
                else
                {
                    errorLbl.Text = "You can't divide by zero... sign up for Math 100 please =)";
                }
                break;
            case "*":
                result = first * second;
                break;
        }
        this.PrintEquation(num1, op, num2, "=", result.ToString());
    }
}

为了让您了解事情是如何结合在一起的,这就是.designer.cs文件中按钮的样子:

        this.btn0.Location = new System.Drawing.Point(12, 60);
        this.btn0.Name = "btn0";
        this.btn0.Size = new System.Drawing.Size(75, 23);
        this.btn0.TabIndex = 1;
        this.btn0.Tag = "0";
        this.btn0.Text = "btn0";
        this.btn0.UseVisualStyleBackColor = true;
        this.btn0.Click += new System.EventHandler(this.handleNumberButtonClick);

注意.Tag值和事件处理程序。

运算符应该如下所示(这是添加,注意标记和Click事件处理程序):

        this.addButton.Location = new System.Drawing.Point(178, 60);
        this.addButton.Name = "addButton";
        this.addButton.Size = new System.Drawing.Size(75, 23);
        this.addButton.TabIndex = 11;
        this.addButton.Tag = "+";
        this.addButton.Text = "add";
        this.addButton.UseVisualStyleBackColor = true;
        this.addButton.Click += new System.EventHandler(this.handleOperatorButtonClick);

小数,如下:

        this.button4.Location = new System.Drawing.Point(178, 176);
        this.button4.Name = "button4";
        this.button4.Size = new System.Drawing.Size(75, 23);
        this.button4.TabIndex = 15;
        this.button4.Tag = ".";
        this.button4.Text = "decimal";
        this.button4.UseVisualStyleBackColor = true;
        this.button4.Click += new System.EventHandler(this.btnDecimal_Click);

哦,把它扔进你清晰的按钮定义

        this.button5.Click += new System.EventHandler(this.btnClear_Click);

特别感谢@Mark Hall@Martin James了解他们的见解