0

I'm making a calculator for my C# class. I'm not allowed to use buttons for operands. There's an operand text box in which the user is supposed to enter "+" for addition, "-" for subtraction, "/" for division, or "*" for multiplication.

I'm trying to find out how to make it where you can only enter one of those four characters, but it also only lets you input one at a time.

I'm pretty new to C#.

  • 1
    use `MaxLength` and see this answer for validation https://stackoverflow.com/questions/12607087/only-allow-specific-characters-in-textbox – TheGeneral Oct 03 '19 at 21:14
  • To give directions we need to know where you're starting from. Please [post your code](https://idownvotedbecau.se/nocode/) where you “enter one of those four characters”. – Dour High Arch Oct 03 '19 at 21:15
  • I didn't have any code to post yet. I wasn't sure how to start coding if I didn't have validation for the operator. – GrandSalamancer Oct 03 '19 at 21:28

1 Answers1

0

Thank you to "TheGeneral" for helping me figure this out, by giving me this link.

I'm not sure why I couldn't find this question on Google.

This is how I did it:

private void TxtOperator_TextChanged(object sender, EventArgs e)
    {
        Regex regex = new Regex(@"[^+^\-^\/^\*^\(^\)]");
        MatchCollection matches = regex.Matches(txtOperator.Text);
        if (matches.Count > 0)
        {
            MessageBox.Show("Please input an operator.");
            txtOperator.Text = "";
        }
    }