How to add a MouseEnter event?

I've been very frustrated the last couple of hours trying to figure out how to add a mouse enter and mouse leave event to my program and it is killing me.

I've added a reference to System.Windows.Forms, imported the System.Windows.Forms namespace above my Form1 class, added:

PublicEvent MouseEnterAs MouseEventHandler

PublicEvent MouseLeaveAs MouseEventHandler

beneath the Form1 class, and added a private sub for both the MouseEnter and MouseLeave events I'm trying to do, but apparently I'm missing a step or two somewhere. Every example I have been able to find glosses over the details as if I'm supposed to have been born with the knowledge or something.

All I want to do is change the color of a button when my users hover their mouses over it. Can someone please help me?

[1202 byte] By [shemtheo] at [2008-1-7]
# 1
Here's a step-by-step list to how its done:

  1. Select the button
  2. Go to the events list
  3. Double click the MouseHover event
  4. Add this code: button1.backgroundimage = (from a location on computer) image.fromfile("hoverimagefilepath") or (from resources) my.resources.hoverimage
  5. Go back to the events list
  6. Double click the MouseLeave event
  7. Add this code: button1.backgroundimage = image.fromfile("normalimagepath") or my.resources.normalimage

You probably will realize this but please note you use the from file one if its a file saved on the computer, resources if its in the programs resources. And replace the file names with the names of your images.
Chris at 2007-10-2 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

Chris, I appreciate your help however, I'm still missing a step.

If I click on the button and then go to the event list, there is no event handler for MouseEnter or MouseLeave. That is my question! How do I get the event handler set up?

shemtheo at 2007-10-2 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

This is already set up for you in the designer

Appearance section

FlatAppearance

MouseOverBackColor

Why you don't see the those events is odd though

What events do you see?

js06 at 2007-10-2 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4

Maybe he could find the events more easily by pressing the Lightning symbols on the Properties box?

JohnWein at 2007-10-2 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5

I'm thinking what is missing is that you haven't selected the button in the code window

try double clicking the button on your form, then in the code window you will see a combobox at the top that has your button name in it.

Directly over to the right you will see the word click in another combobox

drop the box down and then select the event

or what john said and then double click the event in the properties window

js06 at 2007-10-2 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 6

Try adding the code below and just change the name of

you button:

Public Class Form1

Private Sub Button1_MouseEnter(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.MouseEnter

Button1.BackColor = Color.Blue

End Sub

Private Sub Button1_MouseLeave(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.MouseLeave

Button1.BackColor = Color.IndianRed

End Sub

End Class

TallDude at 2007-10-2 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 7

Yes, that's the code but why don't you have the events in your list?

If they really aren't there then you have problems

js06 at 2007-10-2 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 8

js06:

Should be another lesson learned. Don't try to teach them, give them code.

JohnWein at 2007-10-2 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 9

FYI

A moderator checked the answer, not the OP.

The OP may be back with more info / questions.

TallDude at 2007-10-2 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 10

I guess the problem is that I'm working with VB code inside ASP.NET (Visual Web Developer), I guess not all VB features are available.

***!

shemtheo at 2007-10-2 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 11
Next time you have a question dealing with VB code in Visual Web Developer you should probably mention it's with Visual Web Developer and Not Visual Basic Express.
Chris at 2007-10-2 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...