Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capture which control user clicked on with MouseDown event? 1

Status
Not open for further replies.

marcarls

Programmer
Feb 7, 2002
40
SE
Hi,

I need to find out which button in my toolbar the user clicks on, and I can not use the ButtonClicked event. Here's why, I'm having a toolbarbutton called "rewind" that will rewind the time when the user holds the button down, just like any music player. Because of that I can not use ButtonClicked event that will only trigger when the user clicks the button, not holds it down.

I am trying to use MouseDown event, but it only gives me which button on the mouse that was pressed and the x & y cordinates. I need to know if the user pressed my rewindbutton or any other button. Can anyone help me? The only solution I can come up with is to find out x & y of my rewindbutton and compare with the events x&y to see if the user clicked in that range, but it feels like there should be a simple solution to this?

Thanx in advande :)

Maria
 
The MouseDown event gives you more than just the coordinates. Use one event handler to handle the MouseDown of all the controls you want to test against. Inside the code for your event, the object sender will be the control that fired the event. You will have to test and see which control it is.
 
Oh I didn't even think about that argument. :) But the object sender this time only states that it is the object toolbar that is beeing hit, I want to know exactly what part of the toolbar. Perhaps I am missing out on something more...
 
Sorry about that. I was incorrectly considering your ToolBarButtons as equivalent to that of a regular Button. Anyways, this code should do it for you:

Code:
Private Sub ToolBar1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ToolBar1.MouseDown
   Dim tbb As ToolBarButton
   For Each tbb In ToolBar1.Buttons
       If e.X >= tbb.Rectangle.Left AndAlso e.X <= tbb.Rectangle.Right AndAlso e.Y >= tbb.Rectangle.Top AndAlso e.Y <= tbb.Rectangle.Bottom Then
           'The Button Has Been Found
           Me.Text = tbb.Text
           Exit For
       End If
   Next
End Sub

It will change your form's title to that of the button's text, for demonstration.
 
That was really fast answer, thanks a lot, your the greatest :) The code works fine, I will adjust it to fit my needs.
Now I do not have to skip the coffebreak ...nice ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top