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

Go Back  Xtreme .NET Talk > .NET > Windows Forms > Mouse feedback


Reply
 
Thread Tools Display Modes
  #1  
Old 04-21-2003, 12:15 PM
Dave S Dave S is offline
Newcomer

Preferred language:
vb.net
 
Join Date: Apr 2003
Posts: 9
Dave S is on a distinguished road
Unhappy 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
Reply With Quote
  #2  
Old 04-21-2003, 03:43 PM
Cassio's Avatar
Cassio Cassio is offline
Junior Contributor

Preferred language:
VB
 
Join Date: Nov 2002
Location: Rio de Janeiro
Posts: 276
Cassio is on a distinguished road
Default

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
__________________
Stream of Consciousness (My blog)
Reply With Quote
  #3  
Old 04-21-2003, 04:38 PM
Dave S Dave S is offline
Newcomer

Preferred language:
vb.net
 
Join Date: Apr 2003
Posts: 9
Dave S is on a distinguished road
Default

That is cool, but I also need feedback so I can figure out which button was pressed.
Reply With Quote
  #4  
Old 04-21-2003, 04:42 PM
mutant
Guest
 
Posts: n/a
Default

Use the sender that is passed into that function:
For ex.
Code:
If sender is Button1 Then
       MessageBox.show("Button1 was clicked")
End If
Reply With Quote
  #5  
Old 04-21-2003, 04:50 PM
Dave S Dave S is offline
Newcomer

Preferred language:
vb.net
 
Join Date: Apr 2003
Posts: 9
Dave S is on a distinguished road
Default

Then I am better off using the click event for each button. I was hoping to condense the code a bit.
Reply With Quote
  #6  
Old 04-21-2003, 05:39 PM
Nerseus's Avatar
Nerseus Nerseus is offline
Danner

Preferred language:
C#
 
Join Date: Oct 2002
Location: Arizona, USA
Posts: 2,547
Nerseus is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 04-21-2003, 06:42 PM
Dave S Dave S is offline
Newcomer

Preferred language:
vb.net
 
Join Date: Apr 2003
Posts: 9
Dave S is on a distinguished road
Default

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.
Reply With Quote
  #8  
Old 04-21-2003, 09:28 PM
Dave S Dave S is offline
Newcomer

Preferred language:
vb.net
 
Join Date: Apr 2003
Posts: 9
Dave S is on a distinguished road
Default

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]
Reply With Quote
  #9  
Old 04-23-2003, 05:31 AM
hog's Avatar
hog hog is offline
Senior Contributor

Preferred language:
VB .Net
 
Join Date: Mar 2003
Location: UK
Posts: 984
hog is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 04-23-2003, 05:42 AM
Heiko's Avatar
Heiko Heiko is offline
Contributor
 
Join Date: Feb 2003
Location: Holstein, Germany.
Posts: 430
Heiko is on a distinguished road
Default

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
Reply With Quote
  #11  
Old 04-23-2003, 09:16 AM
Dave S Dave S is offline
Newcomer

Preferred language:
vb.net
 
Join Date: Apr 2003
Posts: 9
Dave S is on a distinguished road
Default

Thanks for the great ideas!
Dave
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
datagrid new-row-edit-event? & dialogue with multiple combo-box feedback? Beomer Windows Forms 0 03-24-2006 05:32 AM
Mouse NegativeZero Windows Forms 2 01-29-2005 11:53 AM
Feedback on web site tonyf Random Thoughts 6 01-27-2004 09:51 AM
Move the Mouse and Click the Mouse Diablicolic Windows Forms 5 08-03-2003 06:39 PM
Sending feedback to ASP .NET page from Code Behind deanne_d ASP.NET 0 10-02-2002 08:36 PM

Advertisement:

Powered by liquidweb