Why my textbox TextChanged event gets fired after I enter "only" one character in my text box?
Asked Answered
C

2

7
private void NameVal_TextChanged(object sender, EventArgs e)
    {
        String text = NameVal.Text;

    }

As soon as I enter the first letter of my Name this program gets executed . How do I make the program wait until I finish entering the whole string for the field (such as Name ex: James) is entered.

Cipango answered 17/5, 2013 at 23:28 Comment(2)
This is how TextChanged is supposed to work. What do you want to wait?Lister
I want to wait until I finish entering a String not just a characterCipango
M
10

If you want to determine when the user has finished typing, you can catch the leave event instead - this is fired when the focus is lost on that text box - ie the user clicks outside of the textbox:

private void NameVal_Leave(object sender, EventArgs e)
{
    //Do your stuff
    String text = NameVal.Text;
}
Mathildemathis answered 17/5, 2013 at 23:35 Comment(3)
Thank you for your quick and correct reply, I have been looking for this answer all over. I wish I knew how my searching on Internet could result in an answer. Finally I had to post to SO. Any hints would be appreciatedCipango
You're welcome @Cipango There's a good article on MSDN that explains the order of events in a winform - linkMathildemathis
I used this event as the sole trigger for cheking text is empty, since TextChanged event not get triggered when text box is empty. am I wrong?Gregggreggory
T
-1

If you are working with VS2019 there is a correlation between the TextChanged event and the Focus Leave event. You must have a TextChanged Event then when you search the Events and find Focus select Leave then there will be a drop down box with the TextChanged event for the same object as the Focus Event, which you will select. That will add the Leave Event to the Form1 definition area but it will not generate the code in the code area of Form1 like when you normally double click the TextBox. That you must create yourself. This article helped me do that part. Later that day. I was mistaken. You don't have to pick the TextChanged event. It was the only one available in the drop down list at the time. I had to add both events again in the form1 definition area since I had deleted the TextChanged event and manuallu added the Focus_Leave event, then went back in to add the TextChanged Event. I'm experimenting here. But when I deleted them both and re-entered the events again in the form1.design file there was a possibility to pick the correct one. I had another bug, but now they are in sync again. It's all the code that is generated behind the scene with MS VS that makes it difficult to change this area. And of course they do tell you not to change it, but what do you do if you make a mistake like this, you don't start over, you dive right in and keep going and learn how they are doing what they do.

Trawler answered 8/9, 2019 at 23:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.