Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How Can I Tell Which Button Was Right-Clicked?

0 views
Skip to first unread message

Patrick A

unread,
Nov 12, 2009, 5:38:02 PM11/12/09
to
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

Scott M.

unread,
Nov 12, 2009, 6:58:28 PM11/12/09
to
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 A" <par...@stradley.com> wrote in message
news:4031893c-c5f2-4bdb...@d10g2000yqh.googlegroups.com...

Family Tree Mike

unread,
Nov 12, 2009, 7:00:18 PM11/12/09
to

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.

--
Mike

Scott M.

unread,
Nov 12, 2009, 9:40:58 PM11/12/09
to
You don't want to handle the "Click" event, you want to handle the MouseDown
event, when displaying context menus.

-Scott


"Patrick A" <par...@stradley.com> wrote in message
news:4031893c-c5f2-4bdb...@d10g2000yqh.googlegroups.com...

OmegaSquared

unread,
Nov 17, 2009, 4:24:01 PM11/17/09
to
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

0 new messages