windows窗体输入不断显示

本文关键字:显示 输入 窗体 windows | 更新日期: 2023-09-27 18:29:47

我一辈子都想不出如何连续显示数字的输入和输出。

如何将数字存储在某个位置,然后显示我绕了那么多圈子,把自己弄糊涂了。我知道需要做什么,但不知道如何做,也不知道具体在哪里做?

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;
        }
    }
}

}

windows窗体输入不断显示

我会让你把它连接起来,但这是我拼凑的。

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值和事件处理程序。

Operators应该是这样的(这是add,注意标签和Click Event处理程序):

        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的真知灼见