Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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

 

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

'code here

End Sub

Guest mutant
Posted

Use the sender that is passed into that function:

For ex.

If sender is Button1 Then
      MessageBox.show("Button1 was clicked")
End If

  • *Experts*
Posted

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 want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

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.

Posted

Thanks Cassio, you da man

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]

Posted

When I use multiple handlers I use code like this to determine which control called the routine:

 


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

My website
Posted

You may also add the handlers dynamically:

 

'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

.nerd

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...