Question

I am using Textbox_Validating event to validate a textbox on one of my window form. I wanted to validate the textbox, if the text of the texbox changes. So I called the validating event from the textbox_Texchanged event. But it has a sideffect as soon as I press a key it fires the textchanged event and hence the text_Validating event. I want to block the call to Text_Validating if the text property has changed due to keypress. But if someone assings txtbox.Text="asdf";, I want to fire validating event in this case.

private void txt8Ydere1_TextChanged(object sender, EventArgs e)
    {
        txt8Ydere1_Validating(sender,null);
    }
Was it helpful?

Solution

I would use the LostFocus event for the textbox. You could run your validation from that event.

See the msdn for more info.

OTHER TIPS

you can block the call to validating event by doing this

txt8Ydere1.Validating-=txt8Ydere1_Validating;

place this code in keydown,keyUp events...

Check for txtbox.Text equals "asdf" and if so you can directly call the Validating method without resubscribing to the event:

txt8Ydere1_Validating(sender,null);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top