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. (Last updated on: November 16, 2018).
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?

Platform: WinForms |
Control: MaskedEditBox |
Published Date: April 30, 2011 |
Last Revised Date: July 10, 2014

 

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

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Bijeshnath Ravindranath
Nov 14, 2017

Link to the sample appears to be broken

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

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