How Can I Tell Which Button Was Right-Clicked?

  • Thread starter Thread starter Patrick A
  • Start date Start date
P

Patrick A

All,

I am displaying a context menu when the user right-clicks one of 20
buttons that are created in code (code below).

I would like

What's the simplest, most accurate way to pass an attribute of the
button (like.name) that the user right=clicked to the context menu?

Thanks,

Patrick


Code:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Me.QRY_ButtonsTableAdapter.Fill(Me.SRTimerDataSet.QRY_Buttons)
Dim Row As Integer = 0
Dim ButLbls = SRTimerDataSet.Tables
("QRY_Buttons").AsEnumerable
()
Dim sortedButLbls = From tp In ButLbls _
Select tp _
Order By tp("TimerPos")
For Each tp In sortedButLbls
Row = Row + 1 'Increment up.
Dim NewButton As New MyButton() ' Sets a new button
NewButton.Width = 123 ' Sets the width.
NewButton.Height = 42 ' Sets the height.
NewButton.Top = 1 + (42 * Row - 1) ' Positions the top
of the button.
Me.Controls.Add(NewButton) 'Adds the new button to the
form.
NewButton.Name = (tp!TimerPos)
NewButton.Text = (tp!butLabel)
AddHandler NewButton.Click, AddressOf ButtonResponder
oTimeSliceCollection.Add(NewButton, tp
("TimerPos").ToString)
Next
 
Create a common event handler that handles the mousedown event for each
button that has a context menu.

In that event handler, simply cast the sender argument to the button type
and you will be able to use sender as a reference to the actual button that
fired the mousedown event in the first place. You can then access the Name
property of the button via the sender reference and use that in a Select
(VB) or switch (C#) block to act accordingly.

-Scott
 
Patrick said:
All,

I am displaying a context menu when the user right-clicks one of 20
buttons that are created in code (code below).

I would like

What's the simplest, most accurate way to pass an attribute of the
button (like.name) that the user right=clicked to the context menu?

Thanks,

Patrick


Code:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Me.QRY_ButtonsTableAdapter.Fill(Me.SRTimerDataSet.QRY_Buttons)
Dim Row As Integer = 0
Dim ButLbls = SRTimerDataSet.Tables
("QRY_Buttons").AsEnumerable
()
Dim sortedButLbls = From tp In ButLbls _
Select tp _
Order By tp("TimerPos")
For Each tp In sortedButLbls
Row = Row + 1 'Increment up.
Dim NewButton As New MyButton() ' Sets a new button
NewButton.Width = 123 ' Sets the width.
NewButton.Height = 42 ' Sets the height.
NewButton.Top = 1 + (42 * Row - 1) ' Positions the top
of the button.
Me.Controls.Add(NewButton) 'Adds the new button to the
form.
NewButton.Name = (tp!TimerPos)
NewButton.Text = (tp!butLabel)
AddHandler NewButton.Click, AddressOf ButtonResponder
oTimeSliceCollection.Add(NewButton, tp
("TimerPos").ToString)
Next

If I understand your question, the first argument of ButtonResponder
(sender as object), is the button that was clicked. Cast it to a button
and you have what you need.
 
You don't want to handle the "Click" event, you want to handle the MouseDown
event, when displaying context menus.

-Scott
 
Hello, Patrick,

I don't see in your code where you are adding a ContextMenu to the buttons,
so I guess that you must somehow be simulating a context menu in the
ButtonResponder routine. If that's the case, using the MouseDown event as
described is the way to go.

But why not just add a ContextMenu to the buttons, and then use its
SourceControl property to determine which button was used to invoke it.
Something like:

. . . .
NewButton.ContextMenu = Me.SomePredefinedContextMenu
. . . .

Private Sub ButtonMenuHnadler(ByVal sender As Object, ByVal e As
System.EventArgs) Handles SomePredefinedContextMenu.Click ' (or perhaps
..PopUp)
Dim ctlSource As Control =
Me.SomePredefinedContextMenu.SourceControl
. . . .


Cheers,
Randy
 
Back
Top