locked
problem in c# & sql RRS feed

  • Question

  • hello.

    i have a problem in c#.

    i have 5 textbox,1 buttom , 1 datagridview  on form .

    how i write a code that user can't enter wrong value in textbox's on form ?

    thank you .


    • Edited by asgari Wednesday, February 28, 2018 6:11 AM
    Wednesday, February 28, 2018 6:07 AM

All replies

  • Hi asgari,

    >>how i write a code that user can't enter wrong value in textbox's on form ?

    I'm not sure what is the wrong value, maybe you allow users to enter numbers only, then you can use int.TryParse:

            private void textBox1_Validating(object sender, CancelEventArgs e)
            {
                int value;
                if (int.TryParse(textBox1.Text, out value))
                {
                    MessageBox.Show("Right value");
                }
                else
                {
                    MessageBox.Show("you just can input numbers");
                }
            }

    Or maybe you want users to write some another special values, you can use regular expression.

    For example, we allow users to enter uppercase letters only:

            private void textBox1_Validating(object sender, CancelEventArgs e)
            {
                if (Regex.IsMatch(textBox1.Text, @"^[A-Z]*$"))
                {
                    MessageBox.Show("Right value");
                }
                else
                {
                    MessageBox.Show("you just can input uppercase letters");
                }
            }

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Stanly Fan Wednesday, February 28, 2018 6:46 AM
    • Proposed as answer by Bob Ding Friday, March 2, 2018 1:40 AM
    Wednesday, February 28, 2018 6:44 AM
  • Hi asgari,

    Have you solved this problem now?

    I think the above reply can provide you with a solution, have you tried it?

    If so, Please close this thread by marking the helpful reply as answer as this will help others looking for the same or similar issues down the road.

    Best Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 5, 2018 2:00 AM