I have a basic window that has 20 buttons (One, Two,.....,Twenty).
Currently if you click on a button its background color changes, and changes back on next click.
I'm looking for a way to make it so when the mouse button is held down I can drag across the screen and it will change every button I hit.
Edited: Maybe I missed the obvious and/or someone knows a better way?.
' TEST: Using four buttons with their default names...
Code:
Public Class Form1
Private Btns As Button()
Private btnDown As Boolean
' form load/setup
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' buttons to watch (for mouse)
Btns = New Button() {Button1, Button2, Button3, Button4}
End Sub
' handle multiple buttons - MouseDown event
Private Sub Button1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseDown, Button2.MouseDown, Button3.MouseDown, Button4.MouseDown
' is left mouse button ?
If e.Button = Windows.Forms.MouseButtons.Left Then
' is selected button already colored?
If DirectCast(sender, Button).BackColor <> Color.Lime Then
DirectCast(sender, Button).BackColor = Color.Lime
' set flag mouse is down
btnDown = True
Else
' change back selected button color to default color/style
DirectCast(sender, Button).BackColor = Control.DefaultBackColor
DirectCast(sender, Button).UseVisualStyleBackColor = True
End If
End If
End Sub
' handle multiple buttons - MouseMove event
Private Sub Button1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseMove, Button2.MouseMove, Button3.MouseMove, Button4.MouseMove
If btnDown = True Then
For i As Integer = 0 To Btns.Count - 1
' is mouse over a button?
If RectangleToScreen(Btns(i).Bounds).Contains(Cursor.Position) Then
' color it
Btns(i).BackColor = Color.Lime
EXIT FOR ' button found so exit loop!
End If
Next
End If
End Sub
' handle multiple buttons - MouseUp event
Private Sub Button1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseUp, Button2.MouseUp, Button3.MouseUp, Button4.MouseUp
' reset flag
btnDown = False
End Sub
End Class
Last edited by Edgemeal; Jul 27th, 2011 at 02:10 PM.
Reason: add, button found so exit loop!
You might want to think about using CheckBoxes instead of Buttons. You can set their Appearance property to Button and then they look like regular Buttons. The difference is that when you click them once they remain depressed and then when you click them again they pop back up. You use the Checked property to determine whether they are depressed, just as you would with any other CheckBox. All that changes is the way they look.
Edited: Maybe I missed the obvious and/or someone knows a better way?.
' TEST: Using four buttons with their default names...
Code:
Public Class Form1
Private Btns As Button()
Private btnDown As Boolean
' form load/setup
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' buttons to watch (for mouse)
Btns = New Button() {Button1, Button2, Button3, Button4}
End Sub
' handle multiple buttons - MouseDown event
Private Sub Button1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseDown, Button2.MouseDown, Button3.MouseDown, Button4.MouseDown
' is left mouse button ?
If e.Button = Windows.Forms.MouseButtons.Left Then
' is selected button already colored?
If DirectCast(sender, Button).BackColor <> Color.Lime Then
DirectCast(sender, Button).BackColor = Color.Lime
' set flag mouse is down
btnDown = True
Else
' change back selected button color to default color/style
DirectCast(sender, Button).BackColor = Control.DefaultBackColor
DirectCast(sender, Button).UseVisualStyleBackColor = True
End If
End If
End Sub
' handle multiple buttons - MouseMove event
Private Sub Button1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseMove, Button2.MouseMove, Button3.MouseMove, Button4.MouseMove
If btnDown = True Then
For i As Integer = 0 To Btns.Count - 1
' is mouse over a button?
If RectangleToScreen(Btns(i).Bounds).Contains(Cursor.Position) Then
' color it
Btns(i).BackColor = Color.Lime
End If
Next
End If
End Sub
' handle multiple buttons - MouseUp event
Private Sub Button1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
Handles Button1.MouseUp, Button2.MouseUp, Button3.MouseUp, Button4.MouseUp
' reset flag
btnDown = False
End Sub
End Class