Search code examples
c#.netwindows-forms-designer

C# Validating Inputted Characters of a MaskedTextBox


New to C# and I'm struggling with validating a users inputted time into a masked text box to make sure it fits within a 24hr standard time format. I initially tried just to get the program to print the first character inputted to find out how to properly pick out the first and second characters the numbers to later on do validation with them but I am struggling with even that. Just hoping for any guidance on how to validate the time properly.

Here is what I have currently.

        public void maskedTextBox1_Validating(object sender, CancelEventArgs e)
        {
            int hour = maskedTextBox1.Text[1];
            MessageBox.Show(hour.ToString());

When I input "12:00" into my masked text box the MessageBox comes up with 50 and I am really not sure why it comes to this answer when I want the program to be coming up with a MessageBox saying "2" .


Solution

  • just edit this lines of code:

    //int hour = maskedTextBox1.Text[1];
    //MessageBox.Show(hour.ToString());
    
    string hour = maskedTextBox1.Text[1].ToString();
    MessageBox.Show(hour);