Results 1 to 6 of 6

Thread: Mouse Events

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    6

    Mouse Events

    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.

    Disregard the "Shut Down", "Restart", "LogOff", "Clear", and "Invert" buttons. This idea doesn't apply to them.

    Thanks in advance.
    Attached Images Attached Images  

  2. #2

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    6

    Re: Mouse Events

    Bump

  3. #3
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Mouse Events

    No need for a 1 hour bump.

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Mouse Events

    Quote Originally Posted by VBNube View Post
    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!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Mouse Events

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    6

    Re: Mouse Events

    Quote Originally Posted by Edgemeal View Post
    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
    Thank you!

    This is exactly what I needed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width