Which Control did I RightClick on?

hi all...

Is it possible to find out which control a user rightclicked on and show the name of the control in a messagebox

For example I have three buttons on a form and a contextmenu that is used in common with all the buttons. When a user rightclicks on a button the context menu is shown. Kind of like the "Whats This" menu in MS Apps. So instead of writing a rightclick event for all of the controls on the form, I thought that if I could find the name of the control that the user rightclicked on to open to context menu, I could show the appropriate response.

Is there a way to find if a coordinate of the mouse click falls within a control or is there an easier way for this

Any help is appreciated. Thanks in advance.



Answer this question

Which Control did I RightClick on?

  • liujun

    There are several ways to do this... if you want a common event handler

    Private Sub cbGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbGo.Click,cbStart.Click,cbStop.Click

    dim b as button = sender

    Select Case button.Name

    Case "cbGo"

    ...process go button

    Case "cbStart"

    .... Process Start button

    Case "cbStop"

    Process Stop button

    End Sub



  • Archade

    Thanks for the replies... I have tried this with the common event handler, but for that I need to actually add the name of every control after the Handles argument. Since I will probably be adding and removing controls from the form, I was wondering if there was a way to find the control that was right-clicked on (by using the form1 mouseclick event along with the coordinates clicked on) so kind of like:

    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick

    If e.Button = Windows.Forms.MouseButtons.Right Then

    If e.Location = ... Then
    [which control did I click on by location]...
    End If

    End If

    End Sub

    If I could be pointed in the right direction about this I would appreciate it. Is there a way to know which control I clicked on so I can later use a "select case" statement and show the appropriate message according to the control name.

    Thanks in advance...


  • Lelic

    I'd stick with the handles statement were I you. Almost all events in the framework take the form of (sender As Object, e As EventArgs). They do this so that events from multiple sources can be effectively handled. The sender argument though often ignored is the secret to discovering the origin of the event (either directly or indirectly). Cast sender to a ContextMenuStrip and examine it's SourceControl property as suggested. It would be better to be more specific in your app than to have complex logic to find out which control on the entire from did the click and go through a lengthy select case to act on it - you'd be better of with multiple event handlers as it would save time and coding headaches.


  • Igor_G

    There is a property on the ContextMenu called SourceControl that you can check in the Popup event of the ContextMenu. Note that this is a property on the menu, not on the event args that you get from the Popup event.

  • Which Control did I RightClick on?