Enter and Leave events

B

BobRoyAce

I am trying to understand Enter and Leave events for a user control. I
am using Visual Studio 2005, using VB, for a Windows Forms application.
I am showing and hiding (by changing visible property) user controls on
my main form (within a panel) based on NavBar links being clicked on. I
put code in the Enter and Leave events to simply show a message. Here's
what happens...

The first time the contol is shown, the Enter event fires.
The first time I click on another NavBarItem, the Leave event fires.
However, the control associated with the clicked on NavBarItem doesn't
show.
After that, the Enter and Leave events no longer fire when control's
visibility is changed.

So, I guess it's not the change in visibility that fires these events.
Then what does cause them to fire?

What if I want to check for certain conditions being met before I allow
a user control to be hidden (Visible=False) and, if they're not met, I
disallow the change (i.e. keep it visible and don't show the new user
control)?
 
G

GAZ

Enter and Leave events are just that. They will fire when the cursor Enters
or Leaves the control. For example, if you tab through controls on a form
the Enter event will fire each time your cursor enters a textbox, and the
leave event will fire each time when you tab out of a textbox (same goes if
you click into a control with a mouse).

Simplistic but you can get the jist of it.

BR,

GAZ
 
B

BobRoyAce

I understand what you're saying as it relates to text boxes and such.
However, what I don't understand is why the Enter and Leave events
aren't firing for the user controls that I'm hiding (see original post)
when I show another (only fire the first time).

Any ideas? Any ideas about the last part of my post?
 
G

GAZ

Because in order to show or hide controls you do not have to enter and/or
leave them (controls neither get nor lose focus). In order for required
events to fire you'd have to give focus to the control in question. For
example, if you had a hidden textbox and you want both to show it and to
fire the enter event you'd use this code:

TextBox1.show
TextBox1.Focus

This code will show the textbox and give it focus (i.e. fire the enter
event).

BR,

GAZ
 
B

BobRoyAce

I understand...guess I don't fully know why the events fire once except
that the first time they fire is after the control is created. After
that, it's not created again...just hidden and shown. Also, wouldn't
setting focus on any other control cause the Leave event to fire on the
control that before that had focus?

Am I correct to understand that you don't have any ideas about the last
part of my oringinal post? I could really use some help there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top