 |
| Windows Forms Topics include: windows forms, controls, components and designers |
 |

04-21-2003, 12:15 PM
|
|
Newcomer
Preferred language: vb.net
|
|
Join Date: Apr 2003
Posts: 9
|
|
Mouse feedback
|
|
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
|
|

04-21-2003, 03:43 PM
|
 |
Junior Contributor
Preferred language: VB
|
|
Join Date: Nov 2002
Location: Rio de Janeiro
Posts: 276
|
|
I dont know if this is what you mean, but if want to use the same code for every button you can do this:
Code:
Private Sub ClickButtons(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
'code here
End Sub
|
|

04-21-2003, 04:38 PM
|
|
Newcomer
Preferred language: vb.net
|
|
Join Date: Apr 2003
Posts: 9
|
|
|
|
That is cool, but I also need feedback so I can figure out which button was pressed.
|
|

04-21-2003, 04:42 PM
|
|
|
Use the sender that is passed into that function:
For ex.
Code:
If sender is Button1 Then MessageBox.show("Button1 was clicked") End If
|
|

04-21-2003, 04:50 PM
|
|
Newcomer
Preferred language: vb.net
|
|
Join Date: Apr 2003
Posts: 9
|
|
Then I am better off using the click event for each button. I was hoping to condense the code a bit.
|
|

04-21-2003, 05:39 PM
|
 |
Danner
Preferred language: C#
|
|
Join Date: Oct 2002
Location: Arizona, USA
Posts: 2,547
|
|
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
|

04-21-2003, 06:42 PM
|
|
Newcomer
Preferred language: vb.net
|
|
Join Date: Apr 2003
Posts: 9
|
|
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.
|
|

04-21-2003, 09:28 PM
|
|
Newcomer
Preferred language: vb.net
|
|
Join Date: Apr 2003
Posts: 9
|
|
Thanks Cassio, you da man
[vb]
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
[vb]
|
|

04-23-2003, 05:31 AM
|
 |
Senior Contributor
Preferred language: VB .Net
|
|
Join Date: Mar 2003
Location: UK
Posts: 984
|
|
When I use multiple handlers I use code like this to determine which control called the routine:
Code:
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
|

04-23-2003, 05:42 AM
|
 |
Contributor
|
|
Join Date: Feb 2003
Location: Holstein, Germany.
Posts: 430
|
|
You may also add the handlers dynamically:
Code:
'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
|

04-23-2003, 09:16 AM
|
|
Newcomer
Preferred language: vb.net
|
|
Join Date: Apr 2003
Posts: 9
|
|
Thanks for the great ideas!
Dave
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
| |
|
|
|
 |
|