Why my textbox TextChanged event gets fired after I enter “only” one character in my text box?

醉酒当歌 提交于 2019-12-19 07:48:23

问题


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.


回答1:


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;
}



回答2:


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.



来源:https://stackoverflow.com/questions/16619225/why-my-textbox-textchanged-event-gets-fired-after-i-enter-only-one-character-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!