11 Replies - 1126 Views - Last Post: 08 March 2013 - 06:31 AM Rate Topic: -----

#1 Adqusit   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 176
  • Joined: 02-March 13

When to use the following events

Posted 06 March 2013 - 07:07 PM

Hi. I've been using got focus and lost focus events for my controls since long and never tried Enter event or Leave event. Could you tell me exactly that when to use Got and Lost focus events and When to use Enter and Leave event.

Secondly, Does the Enter and Leave evnets, have superiority over Got and Lost Focus Events.?

Assist me... Thank you so much.
Is This A Good Question/Topic? 0
  • +

Replies To: When to use the following events

#2 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1498
  • View blog
  • Posts: 3,796
  • Joined: 01-February 13

Re: When to use the following events

Posted 06 March 2013 - 07:47 PM

Hi Adqusit,
Have you checked out the msdn page. There is some useful info on the two events in this link. Read the (Remarks) section and the (Notes) section. It tells you what order the events happen.

msdn

msdn2

This post has been edited by IronRazer: 06 March 2013 - 07:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 Adqusit   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 176
  • Joined: 02-March 13

Re: When to use the following events

Posted 07 March 2013 - 03:22 AM

View PostIronRazer, on 06 March 2013 - 07:47 PM, said:

Hi Adqusit,
Have you checked out the msdn page. There is some useful info on the two events in this link. Read the (Remarks) section and the (Notes) section. It tells you what order the events happen.

msdn

msdn2


Yes Iron, I've visited them earlier, but i didn't understand the meaning and purpose. If someone use his/ own words so its so easy to understand.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,553
  • Joined: 12-December 12

Re: When to use the following events

Posted 07 March 2013 - 04:10 AM

The linked (MSDN) pages recommend the use of Enter and Leave in preference to the (low-level) events Got/LostFocus. Unless you have a specific/technical reason to use Focus then I would just follow this advice.

(Any attempt I might make to describe the difference between these events would only be repeating the information in those linked pages :dontgetit: )

Quote

..Instead the Enter and Leave events should be used for all controls except the Form class

Was This Post Helpful? 0
  • +
  • -

#5 Adqusit   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 176
  • Joined: 02-March 13

Re: When to use the following events

Posted 07 March 2013 - 05:52 AM

View Postandrewsw, on 07 March 2013 - 04:10 AM, said:

The linked (MSDN) pages recommend the use of Enter and Leave in preference to the (low-level) events Got/LostFocus. Unless you have a specific/technical reason to use Focus then I would just follow this advice.

(Any attempt I might make to describe the difference between these events would only be repeating the information in those linked pages :dontgetit:/> )

Quote

..Instead the Enter and Leave events should be used for all controls except the Form class


So let me clear myself. Does this all mean in simple words that for all controls, except form, The Enter and The Leave Events are more preferable than Got and Lost Focus events?

Am I right with this?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,553
  • Joined: 12-December 12

Re: When to use the following events

Posted 07 March 2013 - 06:07 AM

View PostAdqusit, on 07 March 2013 - 05:52 AM, said:

So let me clear myself. Does this all mean in simple words that for all controls, except form, The Enter and The Leave Events are more preferable than Got and Lost Focus events?

Am I right with this?

Yes.
Was This Post Helpful? 0
  • +
  • -

#7 Adqusit   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 176
  • Joined: 02-March 13

Re: When to use the following events

Posted 07 March 2013 - 08:51 AM

View Postandrewsw, on 07 March 2013 - 06:07 AM, said:

View PostAdqusit, on 07 March 2013 - 05:52 AM, said:

So let me clear myself. Does this all mean in simple words that for all controls, except form, The Enter and The Leave Events are more preferable than Got and Lost Focus events?

Am I right with this?

Yes.


Thank you very much andrewsw. I'm so glad for the enhancement of my knowledge.
I visited the MSDN link again and again but could understand the meaning of the following, in that link. Could you please tell me that what is the meaning of the following?

Quote

When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

Enter

GotFocus

Leave

Validating

Validated

LostFocus

I can't get the idea in simple meaning? Please guide me further for this... :(:(
Was This Post Helpful? 0
  • +
  • -

#8 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: When to use the following events

Posted 07 March 2013 - 09:42 AM

View PostAdqusit, on 07 March 2013 - 09:51 AM, said:

Thank you very much andrewsw. I'm so glad for the enhancement of my knowledge.

Quote

When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

Enter

GotFocus

Leave

Validating

Validated

LostFocus

I can't get the idea in simple meaning? Please guide me further for this... :(/>/>:(/>

You do know what an event is, right? If you double-click on a Button in the Form Editor, you will be shown the code editor, and a subroutine will be generated, if one has not already been defined, called something like Button1_Click, and at the end of the line, it will have a "handles" specification. In this case, it will say Handles Button1.Click.

What that means is that Button1.Click in an event, and that every time Button1 is clicked, that subroutine will execute.

Now all those events you quoted above are just the same thing. Events that happen under particular conditions. So whenever the conditions are met to generate events, those are the events that happen, and they will happen in the order given.

So, in simple terms, the quoted text from MSDN is exactly what happens, and in exactly that order. I don't see how it can possibly be made any simpler.

It might make it more clear to you if you provide handlers for each type of event for a control, in a test program, and in each event handler, just put a line that says Debug.Print("Focus"), or Debug.Print("Enter"), or whatever is appropriate for the handler. The "Validating" and "Validated" events will not activate unless the "Causes Validation" property is set in the control's properties.

You can generate the handlers by clicking in the drop-down at the top left of the code editor and selecting the control you want to use, then clicking in the drop-down at the top right of the code editor and selecting the event. Then all you have to do is to write the Debug.Print statements within each handler, and run your program. It would be best if you made two controls (like TextBoxes, for instance), and added all the above handlers to one of them. Then you can test what hepperns when you select each one. Hint: the events happen in different orders depending on whether you use the mouse or another method to select the control, which is also covered in the link you were given.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,553
  • Joined: 12-December 12

Re: When to use the following events

Posted 07 March 2013 - 09:46 AM

Consider a button that you click on. First you move the mouse over the button, then push the mouse-button down, etc. So events may happen in the order:

Quote

MouseOver
MouseDown
Click
MouseUp
etc.

(This is possibly not the correct order, just for illustration)

A similar process happens when you click, or tab into, a control. The Enter and GotFocus events happen very quickly in succession. So, in principle, you could write code that responds to either of these two events but, in practice, it is preferable to just use Enter and Leave, and ignore the Got/LostFocus events.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,553
  • Joined: 12-December 12

Re: When to use the following events

Posted 07 March 2013 - 10:01 AM

BTW If you are just using the Leave event to validate/check the value that the user has left in the control then you should investigate the Validating Event and custom validation. This way, you can set up rules that the value(s) must meet, error messages, etc., and let VB.NET handle the validation on your behalf. This is a much more flexible (and professional) approach and means that you don't have to worry (so much..) about the specific sequence of events.
Was This Post Helpful? 0
  • +
  • -

#11 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: When to use the following events

Posted 07 March 2013 - 10:11 AM

OK, here's a neat little test.

Add 3 textboxes

the first textbox has the cursor on run.

In the enter event of textbox2 put this code
Debug.Print ("Entered TextBox2")
TextBox3.Focus


In the GotFocus event of TextBox2 put this code
Debug.Print ("TextBox2 Got Focus")


run the program and press the tab key to move to textbox2.

Notice that it only displays Entered Textbox2. That is because you entered the textbox BUT did not give it a chance to set focus because you told it to focus elsewhere. Therefore the Focus event didn't fire.

Now if you take out the line TextBox3.Focus and run the program and press the tab key you will see that you now have Entered TextBox2 and TextBox2 Got Focus in your immediate window.

So that means that you can enter a textbox and determine from say maybe it's existing contents whether you want to allow it to have focus or move the focus somewhere else.

As many have pointed out, both will appear to give the same result but behind the scenes that is the order of events.

This post has been edited by CharlieMay: 08 March 2013 - 06:48 AM

Was This Post Helpful? 0
  • +
  • -

#12 Adqusit   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 176
  • Joined: 02-March 13

Re: When to use the following events

Posted 08 March 2013 - 06:31 AM

Hi.

Ohhh, what a magnificant piece of information i have gain with this thread. Even the code which i tested, its amazingly nice example of events.


So, Let me sum-up all with, for my own understanding, that the events which are given in MSDN link, will be executed in the following order, in which they are mentioned? Am I right with this?


Secondly, its quite clear that using enter and leave event is more preferable than Using Got and Lost Focus?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1