Dave S Posted April 21, 2003 Posted April 21, 2003 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 Quote
Cassio Posted April 21, 2003 Posted April 21, 2003 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 Quote Stream of Consciousness (My blog)
Dave S Posted April 21, 2003 Author Posted April 21, 2003 That is cool, but I also need feedback so I can figure out which button was pressed. Quote
Guest mutant Posted April 21, 2003 Posted April 21, 2003 Use the sender that is passed into that function: For ex. If sender is Button1 Then MessageBox.show("Button1 was clicked") End If Quote
Dave S Posted April 21, 2003 Author Posted April 21, 2003 Then I am better off using the click event for each button. I was hoping to condense the code a bit. Quote
*Experts* Nerseus Posted April 21, 2003 *Experts* Posted April 21, 2003 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 Quote "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
Dave S Posted April 21, 2003 Author Posted April 21, 2003 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. Quote
Dave S Posted April 22, 2003 Author Posted April 22, 2003 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] Quote
hog Posted April 23, 2003 Posted April 23, 2003 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 Quote My website
Heiko Posted April 23, 2003 Posted April 23, 2003 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 Quote .nerd
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.