How Do I? Determine Which Button was Right-Clicked, Pass to ContextMenu

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

Patrick A

All,

I am using the code below (started from a sample from this group,
I think) to programmatically create a series of buttons in a vertical
strip on a form on Form Open.

I have a context menu which appears when the user right-clicks any
button on the form.

How do I "tell" the context menu (really a function on the context
menu) which button was right-clicked? I know how to get button
attributes when the user left-clicks...but that's not what I need...

Is there a way to do this?

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
 
As explained in the dotnet.framework group reply:

Create one common event handler that handles the mousedown (not click) event
for each
of the buttons 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
 
Back
Top