We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to restrict the user to enter only valid time in Masked EditBox?

 

We can restrict the user to enter only valid time using CustomValidate event of MaskedEditBox. This event is raised when typing each character and validates the input.

C#

private void maskedEditBox1_MaskCustomValidate(object sender,

Syncfusion.Windows.Forms.Tools.MaskCustomValidateArgs e)

{

currentText.Remove(0, this.currentText.Length);

currentText.Append(this.maskedEditBox1.Text);

if (e.CurrentIndex ==0)

{

currentText[e.CurrentIndex] = e.CurrentCharacter;

if (e.CurrentCharacter <= '0' || e.CurrentCharacter > '2')

{

e.Handled = true;

e.Accepted = false;

}

}

else if (e.CurrentIndex ==1)

{

currentText[e.CurrentIndex] = e.CurrentCharacter;

if (currentText[0] > '1')

{

if (e.CurrentCharacter >= '5')

{

e.Handled = true;

e.Accepted = false;

}

}

}

else if (e.CurrentIndex ==3)

{

currentText[e.CurrentIndex] = e.CurrentCharacter;

if (e.CurrentCharacter > '5')

{

e.Handled = true;

e.Accepted = false;

}

}

else if (e.CurrentIndex == 4)

{

currentText[e.CurrentIndex] = e.CurrentCharacter;

}

}

VB

Private Sub maskedEditBox1_MaskCustomValidate(ByVal sender As Object, ByVal e As

Syncfusion.Windows.Forms.Tools.MaskCustomValidateArgs) Handles

maskedEditBox1.MaskCustomValidate

currentText.Remove(0, Me.currentText.Length)

currentText.Append(Me.maskedEditBox1.Text)

If e.CurrentIndex =0 Then

currentText(e.CurrentIndex) = e.CurrentCharacter

If e.CurrentCharacter <= "0"c OrElse e.CurrentCharacter > "2"c Then

e.Handled = True

e.Accepted = False

End If

ElseIf e.CurrentIndex =1 Then

currentText(e.CurrentIndex) = e.CurrentCharacter

If currentText(0) > "1"c Then

If e.CurrentCharacter >= "5"c Then

e.Handled = True

e.Accepted = False

End If

End If

ElseIf e.CurrentIndex =3 Then

currentText(e.CurrentIndex) = e.CurrentCharacter

If e.CurrentCharacter > "5"c Then

e.Handled = True

e.Accepted = False

End If

ElseIf e.CurrentIndex = 4 Then

currentText(e.CurrentIndex) = e.CurrentCharacter

End If

End Sub

Sample :

http://help.syncfusion.com/support/samples/kb/tools.windows/kb_maskededitbox/Tools_WF_KB_MaskedEditBox.zip

Article ID: Published Date: Last Revised Date: Platform: Control:
1780 04/30/2011 07/10/2014 Windows Forms MaskedEditBox
Tags:
Did you find this information helpful?
Comments
Bijeshnath Ravindranath Nov 14, 2017

Link to the sample appears to be broken

Reply
Add Comment
You must log in to leave a comment

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon