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.

How do I dynamically validate text using the TextChanged event in the EditControl?

 

This can be easily accomplished using the TextChanged event and a timer.

The validation routine is invoked in response to a brief pause in typing by the user.

Refer to the code snippets and the sample attached to know more about it.

C#

private void editControl1_TextChanged(object sender, System.EventArgs e)

{

// Do not start the timer as long as characters are being typed.

if (this.timer1.Enabled == true)

this.timer1.Stop();

this.timer1.Start();

}

private void timer1_Tick(object sender, System.EventArgs e)

{

this.ValidateText();

this.timer1.Stop();

}

private void ValidateText()

{

// Perform your validation logic here.

Console.WriteLine("Text validated");

}

VB

Private Sub editControl1_TextChanged(sender As Object, e As System.EventArgs) Handles

editControl1.TextChanged

' Do not start the timer as long as characters are being typed.

If Me.timer1.Enabled = True Then

Me.timer1.Stop()

End If

Me.timer1.Start()

End Sub 'editControl1_TextChanged

Private Sub timer1_Tick(sender As Object, e As System.EventArgs) Handles timer1.Tick

Me.ValidateText()

Me.timer1.Stop()

End Sub 'timer1_Tick

Private Sub ValidateText()

' Perform your validation logic here.

MessageBox.Show("Text validated")

End Sub 'ValidateText

Article ID: Published Date: Last Revised Date: Platform: Control:
352 04/30/2011 07/10/2014 Windows Forms EditControl
Tags:
Did you find this information helpful?
Add Comment
You must log in to leave a comment

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.