PC Review


Reply
Thread Tools Rate Thread

How Can I Tell Which Button Was Right-Clicked?

 
 
Patrick A
Guest
Posts: n/a
 
      12th Nov 2009
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
 
Reply With Quote
 
 
 
 
Scott M.
Guest
Posts: n/a
 
      12th Nov 2009
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" <(E-Mail Removed)> wrote in message
news:4031893c-c5f2-4bdb-9466-(E-Mail Removed)...
> 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



 
Reply With Quote
 
Family Tree Mike
Guest
Posts: n/a
 
      13th Nov 2009
Patrick A wrote:
> 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.

--
Mike
 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      13th Nov 2009
You don't want to handle the "Click" event, you want to handle the MouseDown
event, when displaying context menus.

-Scott


"Patrick A" <(E-Mail Removed)> wrote in message
news:4031893c-c5f2-4bdb-9466-(E-Mail Removed)...
> 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



 
Reply With Quote
 
OmegaSquared
Guest
Posts: n/a
 
      17th Nov 2009
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Button Text Gets Smaller When Button Clicked On Barb Reinhardt Microsoft Excel Programming 0 18th Mar 2009 01:31 PM
Inconsistent behavior of an asp.net button performing a postback when a button is clicked. Mossman Microsoft ASP .NET 0 12th Dec 2005 02:55 AM
What Button Was Clicked ?? Charles A. Lackman Microsoft Dot NET Framework Forms 3 25th Aug 2004 06:02 AM
Pause macro, add form button to sheet, continue macro when button clicked! Flystar Microsoft Excel Programming 1 26th May 2004 09:45 AM
Button Collection for Toolbar WebControl - Determinine which button was clicked Jim Mitchell Microsoft Dot NET 0 12th Jul 2003 02:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:11 AM.