Mouse feedback

Dave S

Newcomer
Joined
Apr 4, 2003
Messages
9
I was wondering if there is a way to determine the mouse position when a button is pressed, without using the button click event. Imagine a grid of 100 buttons, one of them is pressed.
Do I need to add code to every button click event?

Thanks
Dave
 
I dont know if this is what you mean, but if want to use the same code for every button you can do this:

Visual Basic:
Private Sub ClickButtons(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click

'code here

End Sub
 
Use the sender that is passed into that function:
For ex.
Visual Basic:
If sender is Button1 Then
       MessageBox.show("Button1 was clicked")
End If
 
Then I am better off using the click event for each button. I was hoping to condense the code a bit.
 
Depending on what you're trying to accomplish, there could be an easier solution. Maybe each button has some data associated to it. You could put the data in the Tag property of each button and using the sender parameter, pull out the data of whatever button was clicked. If you tell us more about what you're trying to do, specifically, with each button's click event, we can help more.

-Ner
 
I am im the process of programming a minesweeper type game for a final exam. I don't want a lot of help, but I am thinking there must be a elegant way to determine which button is pressed without adding code to each of the 100 buttons.
Maybe this .net isn't as powerful as I thought it was.
 
Thanks Cassio, you da man
Visual Basic:
Private Sub btn1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btn1.MouseDown, btn2.MouseDown, btn3.MouseDown
        Dim pintTemp As System.Object
        pintTemp = sender
        If e.Button = MouseButtons.Right Then
            If pintTemp.ImageIndex = 2 Then
                pintTemp.ImageIndex = 0
            Else
                pintTemp.ImageIndex += 1
            End If
        End If
        'left click stuff here
    End Sub
[CODE=visualbasic]
 
When I use multiple handlers I use code like this to determine which control called the routine:

Visual Basic:
Private Sub ClickButtons(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click

Dim ctlSender as Button = DirectCast(sender, Button)

Select Case ctlSender.Name

   Case "Button1"

       ' code

   Case "Button"
    
       ' code

End Select

End Sub
 
You may also add the handlers dynamically:

Visual Basic:
'P S E U D O C O D E

Private Sub AddHandlers()
Dim btnType As Type = GetType(System.Windows.Forms.Button)
Dim aCtl as Control

For each aCtl in me.Contols
   If btnType.IsInstanceOfType(aCtl) Then
      AddHandler aCtl.ButtonClick, AddressOf me.ButtonClick
    end if
Next aCtl

End sub

Private Sub ButtonClick (Sender as Object, e as System.EventArgs)
'Here you can possibly determine the x/y coordinates of the mousepointer! This will probably make your code much more straightforward
DoStuff
End sub
 
Back
Top