简体   繁体   中英

Creating a calculator in C#

I am currently working on a touchscreen program to calculate payments being made.

在此处输入图片说明

The calculation I am trying to accomplish is: Remaining = Total - Paid .

How do I auto populate the Remaining field with the result to this calculation as the user types it in?

I want it to happen in real time, and when the user presses the Pay button.

    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)
    {

    }

Use the TextChanged event of the TextBoxes. Something like the following would work, however, you'd need to put in some additional error handling:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM