User Control Events

J

johnson.bj

I have a relatively minor problem that has been bugging me for a long
time. I have created a user control which consists of a few panels
and labels. What I want to do with this user control is to change its
back color when the mouse hovers over it and to change the back color
to its previous color when the mouse leaves the control creating the
illusion of the control being highlighted.

Now I assumed that after the user control was created and I plopped it
on a form, the panels and labels contained in the control we be
considered as part of the control. So inside of the user control I
handled the Me.MouseHover and Me.MouseLeave events to change the back
color. Similar code to what I had is shown below.

Private Sub myControl_MouseHover(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.MouseHover
Me.BackColor = SomeColor
End Sub

Private Sub myControl_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.MouseLeave
Me.BackColor = SomeColor2
End Sub

However I found out that when you plop a user control on a form, the
panels and labels (and other components) inside of the user control
are still considered separate from "Me", if you will. This means that
the Me.MouseHover event is only triggered when the mouse is over a
portion of the control that isn't covered by a panel, label, etc.
Moreover, this means that Me.MouseLeave is triggered when moving from
a "blank" portion of the control (a portion with no windows controls
on it) to a portion with a windows control on it (a panel or label in
my case).

I have a work around in which I handle all MouseHover and MouseLeave
events of all the components. Similar code to what I have is shown
below.

Private Sub myControl_MouseHover(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.MouseHover, Panel1.MouseHover,
Label1.MouseHover
Me.BackColor = SomeColor
End Sub

Private Sub myControl_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.MouseLeave, Panel1.MouseLeave,
Label1.MouseLeave
Me.BackColor = SomeColor2
End Sub

The problem with this is that I get a flickering effect when the mouse
leaves Panel1, for example, and enters "Me" (the empty part of the
control) or Label1. I've played around with the Modifiers property on
the labels and panels but I've had no luck. So I guess the question
inside of this long winded post is: Is there a way to get predefined
windows controls to act like they are a part of a user defined
control? I realize that one way to fix this is to just draw on the
control itself in the OnPaint and ditch the panels and labels, but it
was easy to just dock and anchor the panels and labels to
automatically resize instead of manipulating coordinates of drawn
shapes.

Any help on this is appreciated.

Thanks,
BJ
 
C

Chris Dunaway

I have a relatively minor problem that has been bugging me for a long
time. I have created a user control which consists of a few panels
and labels. What I want to do with this user control is to change its
back color when the mouse hovers over it and to change the back color
to its previous color when the mouse leaves the control creating the
illusion of the control being highlighted.

Now I assumed that after the user control was created and I plopped it
on a form, the panels and labels contained in the control we be
considered as part of the control. So inside of the user control I
handled the Me.MouseHover and Me.MouseLeave events to change the back
color. Similar code to what I had is shown below.

Private Sub myControl_MouseHover(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.MouseHover
Me.BackColor = SomeColor
End Sub

Private Sub myControl_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.MouseLeave
Me.BackColor = SomeColor2
End Sub

However I found out that when you plop a user control on a form, the
panels and labels (and other components) inside of the user control
are still considered separate from "Me", if you will. This means that
the Me.MouseHover event is only triggered when the mouse is over a
portion of the control that isn't covered by a panel, label, etc.
Moreover, this means that Me.MouseLeave is triggered when moving from
a "blank" portion of the control (a portion with no windows controls
on it) to a portion with a windows control on it (a panel or label in
my case).

I have a work around in which I handle all MouseHover and MouseLeave
events of all the components. Similar code to what I have is shown
below.

Private Sub myControl_MouseHover(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.MouseHover, Panel1.MouseHover,
Label1.MouseHover
Me.BackColor = SomeColor
End Sub

Private Sub myControl_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.MouseLeave, Panel1.MouseLeave,
Label1.MouseLeave
Me.BackColor = SomeColor2
End Sub

The problem with this is that I get a flickering effect when the mouse
leaves Panel1, for example, and enters "Me" (the empty part of the
control) or Label1. I've played around with the Modifiers property on
the labels and panels but I've had no luck. So I guess the question
inside of this long winded post is: Is there a way to get predefined
windows controls to act like they are a part of a user defined
control? I realize that one way to fix this is to just draw on the
control itself in the OnPaint and ditch the panels and labels, but it
was easy to just dock and anchor the panels and labels to
automatically resize instead of manipulating coordinates of drawn
shapes.

Any help on this is appreciated.

Thanks,
BJ

You could try overriding the WndProc, or any of the various Process*
and PreProcess* methods of UserControl. That should allow you to
intercept the messages to your UserControl and act accordingly.

Just a suggestion,

Chris
 

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