Button control event choreography

ideprize

Well-known member
Joined
Oct 19, 2011
Messages
96
Programming Experience
10+
Hi All
Still learning this rather large paradigm - vb.NET. I have discovered that the button control event sequence is not the same as the textbox control. The enter event is not fired when the button is clicked and the validated event is not fired following the click event. I wrote a simple program with one button (no other controls). The button's enter event is fired when the form opens. The button's click event is fired when the button is clicked. The button's validated event is fired when the form is closed. I normally set the backcolor of the control during the enter event and set the "next control focus" with the .select method in the control's validated event. Is it standard practice that all of this must be done in the click event itself and if that is the case why is the button control so different from the textbox? Any enlightenment would be greatly appreciated.
Respectfully,
Ideprize
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+
Textbox is an input control, button is not. Typically only input is validated. If you want to start validation with a button click perhaps you can use the forms Validate/ValidateChildren methods.
If you use tab key to select next control the Enter event will be raised for button, but if you need something to happen when you enter a button with mouse cursor you use MouseEnter event.
 

ideprize

Well-known member
Joined
Oct 19, 2011
Messages
96
Programming Experience
10+
Thank you JohnH for the very informative response. My dilemma is knowing from the textbox control that a click event is what caused it's validated event to fire. In other words the user typed in the required text to "feed" the "button click event" and clicked the button immediately. In general I need to know when the validated event of the textbox was entered what caused it, i.e by the tab/enter key or by the click of the mouse on the button control. The reason for this is to control the textbox's validated event's exit logic. If it was the tab/enter key then .select the next textbox if it was the button click then .select the "first" textbox. I think I have found a work around by using the "causingvalidation" property on the button to be set to false; in this way I am able to take control BEFORE the textbox validation event is entered. I have also looked at the "sender" object in hopes it would identify the button click event when entering the textbox validated event but it did not or at least what I was able to extract from it - did not. Again thanks JohnH you did indeed provide enlightenment. If you know of a simpler method to identify the cause of entry into the textbox's validated event I would greatly appreciate it.
 

ideprize

Well-known member
Joined
Oct 19, 2011
Messages
96
Programming Experience
10+
Found a work around and I would like to mark this thread as resolved but it seems that the method I used before is no longer functional - click the title and click the resolved option. Has this changed?
The work around was to set a boolean variable in the click event of the button (that took focus) and then check the status of that variable in the "next" textbox's validated event and depending on it's status (true) cycle back to the "first" textbox or (false) continue to the next textbox. Thanks again JohnH, your "enlightenment" sent me on this trip; it also made me aware of the unnecessary validated event on the button control. If someone can show me how to mark this thread resolved I will do so.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
Found a work around and I would like to mark this thread as resolved but it seems that the method I used before is no longer functional - click the title and click the resolved option. Has this changed?
I'm not sure that that has ever been possible but, regardless, I just clicked the Edit option on your first post and I was able to set the Prefix. Among the options are Answered and Resolved.
 

ideprize

Well-known member
Joined
Oct 19, 2011
Messages
96
Programming Experience
10+
Thanks Jim. It is a real strong possibility that I got the steps wrong - gee, imagine that!
Respectfully,
Ideprize
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
If you frequent a number of forum and Q&A sites, it is certainly easy to get their procedures mixed up, especially now that this site has changed so much with the switch from vBulletin to Xenforo.
 
Top Bottom