Changing a Label control's text when moving a mouse over a button

S

siliconpi

I'm looking for the simplest and cleanest way of having 10 buttons on
a form, on which if I move my mouse over, a label's text changes as
specified.

I'm using Visual Basic .NET and I'm not too familiar with its
intricacies, so I'm getting a bunch of errors on certain scenarios.

The code below works fine but breaks if I click on Button1 a few times
without moving the program window
----------

Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
Dim x = System.Windows.Forms.Control.MousePosition.X()
Dim y = System.Windows.Forms.Control.MousePosition.Y()

Dim tF1x = Form1.ActiveForm.Left()
Dim tF1y = Form1.ActiveForm.Top()

If (tF1x + Button1.Location.Y() < x And x < tF1x +
Button1.Location.X() + Button1.Size.Width()) Then
' If (tF1y + Button1.Location.Y() < y And y < tF1y +
Button1.Location.Y() + Button1.Size.Height()) Then
Label1.Text = Button1Text
' End If
End If
End Sub
 
C

Cor Ligthert

Siliconpi,

See this sample that I once made,

You only need a form and paste this in the code, if you want to make the
buttons dynamicly. You can as well use the buttons on the form, set them in
an array and add than the handlers or just add that one by one from your
buttons.

I hope this helps?

Cor
\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim start As Integer = 4
Dim top As Integer = 25
Dim i As Integer
Dim nowdate As DateTime = DateTime.Now
Dim mybutton(System.DateTime.DaysInMonth _
(nowdate.Year, nowdate.Month)) As Button
For i = 0 To System.DateTime.DaysInMonth _
(nowdate.Year, nowdate.Month) - 1
mybutton(i) = New Button
mybutton(i).TextAlign = ContentAlignment.MiddleCenter
mybutton(i).Width = 40
mybutton(i).Height = 20
mybutton(i).FlatStyle = FlatStyle.Flat
mybutton(i).BackColor = Drawing.Color.AntiqueWhite
mybutton(i).Location = New System.Drawing.Point(start, top)
mybutton(i).Text = (i + 1).ToString
mybutton(i).Cursor = Cursors.Hand
Me.Controls.Add(mybutton(i))
AddHandler mybutton(i).Click, AddressOf mybutton_Click
AddHandler mybutton(i).MouseHover, AddressOf mybutton_Hoover
AddHandler mybutton(i).MouseLeave, AddressOf mybutton_Leave
start = start + 40
If (i + 1) Mod 5 = 0 Then
top = top + 20
start = 4
End If
Next
End Sub
Private Sub mybutton_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
MessageBox.Show("The day is: " & thisbutton.Text)
End Sub
Private Sub mybutton_Hoover _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
thisbutton.BackColor = Drawing.Color.AliceBlue
End Sub
Private Sub mybutton_Leave _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
thisbutton.BackColor = Drawing.Color.AntiqueWhite
End Sub
///
 

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