How to limit values ​​in Winforms MaskedTextbox

My requirement is that the user enters the time ("HH: mm: ss") in a disguised text field and based on that time I perform some functions. My problem is that I can mask the time, but I cannot prevent the user from entering up to 23 hours, 59 minutes and 59 seconds. How to fix it.

C # code

private void Form1_Load(object sender, EventArgs e) { maskTxtAlert1.Mask = "00:00:00"; maskTxtAlert1.CutCopyMaskFormat = MaskFormat.ExcludePromptAndLiterals; } private void maskTxtAlert1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) { if (e.Position == maskTxtAlert1.Mask.Length) { string errorMessage = "You cannot add extra characters"; toolTip1.ToolTipTitle = "Input Rejected - No more inputs allowed"; toolTip1.Show(errorMessage, maskTxtAlert1, 12, 24, 2000); errorProvider1.BlinkStyle = ErrorBlinkStyle.AlwaysBlink; errorProvider1.SetError(maskTxtAlert1, errorMessage); } else { toolTip1.ToolTipTitle = "Input Rejected"; string errorMessage = "You can only add numeric characters (0-9)."; toolTip1.Show(errorMessage, maskTxtAlert1, 12, 24, 2000); errorProvider1.BlinkStyle = ErrorBlinkStyle.AlwaysBlink; errorProvider1.SetError(maskTxtAlert1, errorMessage); } } private void maskTxtAlert1_TypeValidationCompleted(object sender, TypeValidationEventArgs e) { MessageBox.Show("Enter Valid as One"); } 
+4
source share
4 answers

I think it is better to use DateTimePicker as a namesake and does not require validation

 dateTimePicker1.Format = DateTimePickerFormat.Custom; //For 24 H format dateTimePicker1.CustomFormat = "HH:mm:ss"; //For 12 H format dateTimePicker1.CustomFormat = "hh:mm:ss tt"; dateTimePicker1.ShowUpDown = true; 
+4
source

if you don’t need to use maskedTextBox , as the namesake said, but it may be less convenient. I would choose a simpler way: instead of inserting a messageBox into the TypeValidationCompleted , you can change the invalid user values. if the user wrote a value above 23 in hours, for example, change it to 23

0
source

The MaskedTextbox class is not designed to handle this type of validation, and you will need to add some manual validation to the Validation Complete event.

eg.

 string[] timedetails = FormattedDate.Split(':'); int hours = Int32.Parse(timedetails[0].Trim()); int minutes = Int32.Parse(timedetails[1].Trim()); int seconds = Int32.Parse(timedetails[2].Trim()); if(hours > 23) { //error message here, etc. } 

However, you might be much better off using a DateTime formatted control, possibly set to long time format. See the full list of formats on MSDN .

0
source

Using DateTimePickerFormat with .CustomFormat = "HH:mm:ss" is perhaps the best out-of-the-box approach, but can be restrictive in terms of interacting with the user interface.

For people coming here to add confirmation to MaskedTextBox , there are some good options.

Type check

If you still need automatic parsing, you can add a ValidatingType and click the TypeValidationCompleted Event like this:

 Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load MaskedTextBox1.Mask = "00:00" MaskedTextBox1.ValidatingType = GetType(DateTime) End Sub Private Sub MaskedTextBox1_TypeValidationCompleted(sender As Object, e As TypeValidationEventArgs) _ Handles MaskedTextBox1.TypeValidationCompleted If Not e.IsValidInput Then Me.validationToolTip.ToolTipTitle = "Invalid Time" Me.validationToolTip.Show("The time you supplied must be a valid time in the format HH:mm", sender) End If End Sub 

ValidatingType will use the public static Object Parse(string) method for each type passed to it. You can even pass your own custom classes if they implement this signature

User check:

To provide your own validation, you can click the Validating Event like this:

 Private Sub MaskedTextBox1_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) _ Handles MaskedTextBox1.Validating Dim senderAsMask = DirectCast(sender, MaskedTextBox) Dim value = senderAsMask.Text Dim parsable = Date.TryParse(value, New Date) If Not parsable Then e.Cancel = True Me.validationToolTip.ToolTipTitle = "Invalid Time" Me.validationToolTip.Show("The time you supplied must be a valid time in the format HH:mm", sender) End If End Sub 

In this case, the verification implementation is reliable, but you can implement any logic that you want to define.

Notes :
If the CausesValidation property CausesValidation set to false , the Validating and Validated events are suppressed.
If the Cancel property of the CancelEventArgs parameter CancelEventArgs set to true in the deletion of the validation event, all events that usually occur after the Validating event are suppressed.

For bonus points, hide the tooltip when the user starts typing again:

 Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) _ Handles MaskedTextBox1.KeyDown Me.validationToolTip.Hide(sender) End Sub 
0
source

Source: https://habr.com/ru/post/1490652/


All Articles