Search code examples
c#calculatormaskedtextbox

How would I use and implement a MaskedTextBox into my Calculator application? C#


So I'm working on a calculator application in C-Sharp and I want to prevent people from entering more than 1 period/point at once. So they can't type "....." or "1..1" or "1.1.1" Really just stuff like that.... I would also like to prevent them from adding alphabetical characters in by typing them in with the keyboard, characters like "a, b, c".

I was told to use a MaskedTextBox, and I want to know if that is correct. Also, if it is correct, how would I implement it into my code? I'm a complete beginner when it comes to C#, so I would like some help (toned down for a beginner).

So far the code I have written is:

    double total1 = 0;
    double total2 = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void btnOne_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnOne.Text;
    }

    private void btnTwo_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
    }

    private void btnThree_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnThree.Text;
    }

    private void btnFour_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnFour.Text;
    }

    private void btnFive_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnFive.Text;
    }

    private void btnSix_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnSix.Text;
    }

    private void btnSeven_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnSeven.Text;
    }

    private void btnEight_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnEight.Text;
    }

    private void btnNine_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnNine.Text;
    }

    private void btnZero_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnZero.Text;
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        txtDisplay.Clear();
    }

    private void btnPlus_Click(object sender, EventArgs e)
    {
        total1 = total1 + double.Parse(txtDisplay.Text);
        txtDisplay.Clear();
    }

    private void btnEquals_Click(object sender, EventArgs e)
    {
        total2 = total1 + double.Parse(txtDisplay.Text);
        txtDisplay.Text = total2.ToString();
        total1 = 0;

    }

    private void btnPoint_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnPoint.Text;
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void txtDisplay_TextChanged(object sender, EventArgs e)
    {

    }
}

So I ask... how/where do I add in this "MaskedTextBox" - if that is correct? How do I implement it? What makes it work?

Thanks!


Solution

  • You don't need a MaskedTextBox, since you're simulating the keypad with your buttons. Just put something like this in btnPoint_Click:

    private void btnPoint_Click(object sender, EventArgs e)     
    {         
        if (!txtDisplay.Text.Contains("."))
        {
            txtDisplay.Text = txtDisplay.Text + btnPoint.Text;    
        } 
    }