2

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);
    }
Vaibhav Jain
  • 33,887
  • 46
  • 110
  • 163

2 Answers2

1

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);
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • But if I call txt8Ydere1_Validating(sender,null); in TextChanged event, keydown event will result in text changed and simultaneously it will call txt8Ydere1_Validating(sender,null) – Vaibhav Jain Apr 06 '12 at 07:09
0

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

See the msdn for more info.

ImGreg
  • 2,946
  • 16
  • 43
  • 65