用C#创建计算器

时间:2014-04-19 10:16:10

标签: c#

我目前正在开发一个触摸屏程序来计算付款。

enter image description here

我想要完成的计算是:剩余=总计 - 付费

当用户输入时,如何自动将剩余字段与结果一起填充到此计算中?

我希望它实时发生,并且当用户按下“付款”按钮时。

    void TextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
        {
            e.Handled = true;
        }
    }

    void totalBtn_activate()
    {
        totalTxt.Select();
        totalTxt.BackColor = Color.FromArgb(245, 244, 162);
        totalBtn.ForeColor = Color.Red;

        paidTxt.BackColor = Color.White;
        paidBtn.ForeColor = Color.Black;

        remainTxt.BackColor = Color.White;
        remainBtn.ForeColor = Color.Black;
    }

    private void paidBtn_Click(object sender, EventArgs e)
    {
        paidBtn_activate();
    }

    private void paidTxt_Enter(object sender, EventArgs e)
    {
        paidBtn_activate();
    }

    void paidBtn_activate()
    {
        paidTxt.Select();
        paidTxt.BackColor = Color.FromArgb(245, 244, 162);
        paidBtn.ForeColor = Color.Red;

        totalTxt.BackColor = Color.White;
        totalBtn.ForeColor = Color.Black;

        remainTxt.BackColor = Color.White;
        remainBtn.ForeColor = Color.Black;
    }

    private void remainBtn_Click(object sender, EventArgs e)
    {
        remainBtn_activate();
    }

    private void remainTxt_Enter(object sender, EventArgs e)
    {
        remainBtn_activate();
    }

    private void totalBtn_Click(object sender, EventArgs e)
    {
        totalBtn_activate();
    }

    private void totalTxt_Enter(object sender, EventArgs e)
    {
        totalBtn_activate();
    }

    void remainBtn_activate()
    {
        remainTxt.Select();
        remainTxt.BackColor = Color.FromArgb(245, 244, 162);
        remainBtn.ForeColor = Color.Red;

        totalTxt.BackColor = Color.White;
        totalBtn.ForeColor = Color.Black;

        paidTxt.BackColor = Color.White;
        paidBtn.ForeColor = Color.Black;
    }

    private void viewDrivers_Click(object sender, EventArgs e)
    {
        var form1 = new Form1();
        form1.Show();
        this.Close();
    }

    private void AppendValue(string valueToAppend)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.AppendText(valueToAppend);
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.AppendText(valueToAppend);
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.AppendText(valueToAppend);
        }
    }

    bool dotControl = false;
    int count = 0;

    private void btn1_Click(object sender, EventArgs e)
    {
        if (dotControl && count == 0)
        {
            AppendValue("1");
            count++;
        }
        else if (count > 0)
        {
        }
        else
        {
            AppendValue("1");
        }
    }

    private void btn2_Click(object sender, EventArgs e)
    {
        if (dotControl && count == 0)
        {
            AppendValue("2");
            count++;
        }
        else if (count > 0)
        {
        }
        else
        {
            AppendValue("2");
        }
    }

    private void btn3_Click(object sender, EventArgs e)
    {
        if (dotControl && count == 0)
        {
            AppendValue("3");
            count++;
        }
        else if (count > 0)
        {
        }
        else
        {
            AppendValue("3");
        }
    }

    private void btn4_Click(object sender, EventArgs e)
    {
        if (dotControl && count == 0)
        {
            AppendValue("4");
            count++;
        }
        else if (count > 0)
        {
        }
        else
        {
            AppendValue("4");
        }
    }

    private void btn5_Click(object sender, EventArgs e)
    {
        if (dotControl && count == 0)
        {
            AppendValue("5");
            count++;
        }
        else if (count > 0)
        {
        }
        else
        {
            AppendValue("5");
        }
    }

    private void btn6_Click(object sender, EventArgs e)
    {
        if (dotControl && count == 0)
        {
            AppendValue("6");
            count++;
        }
        else if (count > 0)
        {
        }
        else
        {
            AppendValue("6");
        }
    }

    private void btn7_Click(object sender, EventArgs e)
    {
        if (dotControl && count == 0)
        {
            AppendValue("7");
            count++;
        }
        else if (count > 0)
        {
        }
        else
        {
            AppendValue("7");
        }
    }

    private void btn8_Click(object sender, EventArgs e)
    {
        if (dotControl && count == 0)
        {
            AppendValue("8");
            count++;
        }
        else if (count > 0)
        {
        }
        else
        {
            AppendValue("8");
        }
    }

    private void btn9_Click(object sender, EventArgs e)
    {
        if (dotControl && count == 0)
        {
            AppendValue("9");
            count++;
        }
        else if (count > 0)
        {
        }
        else
        {
            AppendValue("9");
        }
    }

    private void btn0_Click(object sender, EventArgs e)
    {
        if (dotControl && count == 0)
        {
            AppendValue("0");
            count++;
        }
        else if (count > 0)
        {
        }
        else
        {
            AppendValue("0");
        }
    }

    private void btndot_Click(object sender, EventArgs e)
    {
        if (!dotControl)
        {
            AppendValue(".");
            dotControl = true;
        }
    }

    private void RemoveLast(TextBox tb)
    {
        if (tb.Text.Length > 0)
        {
            if (char.IsDigit(tb.Text[tb.Text.Length - 1])) count = 0;
            else
            {
                dotControl = false;
                count = 0;
            }
            tb.Text = tb.Text.Remove(tb.Text.Length - 1, 1);
        }
    }

    private void btnback_Click(object sender, EventArgs e)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            RemoveLast(remainTxt);
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            RemoveLast(totalTxt);
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            RemoveLast(paidTxt);
        }
    }

    private void btnreset_Click(object sender, EventArgs e)
    {
        dotControl = false;
        count = 0;

        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.Text = "";
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.Text = "";
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.Text = "";
        }
    }

    private void btnpay_Click(object sender, EventArgs e)
    {

    }

1 个答案:

答案 0 :(得分:1)

使用TextBoxes的TextChanged事件。以下内容可行,但是,您需要进行一些额外的错误处理:

totalTxt.TextChanged += new EventHandler(TextChanged);
paidTxt.TextChanged += new EventHander(TextChanged);

void TextChanged (object sender, EventArgs e)
{
    remainTxt.Text = Convert.ToString(int.Parse(totalTxt.Text) - int.Parse(paidTxt.Text));
}