PC Review


Reply
Thread Tools Rate Thread

Click event in dynamic menu

 
 
Christian Ahrenkiel
Guest
Posts: n/a
 
      16th Sep 2003
Hi!

I've got a submenu in my ContextMenu, which is generated during
runtime.
I'm not sure about handling the Click-Event in dynamic menues.

I'm abled to fire the Click-Event for all MenuItems, but cannot
distinguish which MenuItem was clicked.

This is just an example how I'm handling it (very simplyfied for this
posting - i hope there are no confusing errors):

// Having an array with strings
for(int i=0; i < myarray.Length; i++)
{
addSubmenu(myarray[i]);
}

// Each element shall be a MenuItem
private void addSubmenu(string menu)
{
// Creating a new temporarily MenuItem
MenuItem mniTemp = new MenuItem();
mniTemp.Text = menu;
// Adding this MenuItem to my menu
this.mniMenu.MenuItems.Add(mniTemp);
// Adding an EventHandler
mniTemp.Click += new EventHandler(tmp_Click);
}

// The EventHandler - it fires for all generated MenuItems
private void tmp_Click(object sender, EventArgs e)
{
// How can I distinguish _here_ _which_ MenuItem was hit?
}

Are there any other possibilities? Maybe with ArrayLists (analogous to
ArrayLists and Comboboxes)?

Christian
 
Reply With Quote
 
 
 
 
Peter Foot [MVP]
Guest
Posts: n/a
 
      16th Sep 2003
The Click event handler has a sender argument - this will be the control
which generated the event - e.g. menu item:-

// The EventHandler - it fires for all generated MenuItems
private void tmp_Click(object sender, EventArgs e)
{
// How can I distinguish _here_ _which_ MenuItem was hit?

// sender
MenuItem mnusender = (MenuItem)sender;

//switch action based on text of control
switch(ctlsender.Text)
{
case "Open":
//do something here
break;

//etc
}
}


Peter

--
Peter Foot
Windows Embedded MVP

www.inthehand.com | www.opennetcf.org

"Christian Ahrenkiel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi!
>
> I've got a submenu in my ContextMenu, which is generated during
> runtime.
> I'm not sure about handling the Click-Event in dynamic menues.
>
> I'm abled to fire the Click-Event for all MenuItems, but cannot
> distinguish which MenuItem was clicked.
>
> This is just an example how I'm handling it (very simplyfied for this
> posting - i hope there are no confusing errors):
>
> // Having an array with strings
> for(int i=0; i < myarray.Length; i++)
> {
> addSubmenu(myarray[i]);
> }
>
> // Each element shall be a MenuItem
> private void addSubmenu(string menu)
> {
> // Creating a new temporarily MenuItem
> MenuItem mniTemp = new MenuItem();
> mniTemp.Text = menu;
> // Adding this MenuItem to my menu
> this.mniMenu.MenuItems.Add(mniTemp);
> // Adding an EventHandler
> mniTemp.Click += new EventHandler(tmp_Click);
> }
>
> // The EventHandler - it fires for all generated MenuItems
> private void tmp_Click(object sender, EventArgs e)
> {
> // How can I distinguish _here_ _which_ MenuItem was hit?
> }
>
> Are there any other possibilities? Maybe with ArrayLists (analogous to
> ArrayLists and Comboboxes)?
>
> Christian



 
Reply With Quote
 
Brian Burgess
Guest
Posts: n/a
 
      14th Oct 2003
Hi Peter,

Is there a way to do this if there is no text on the control? I have
a set of buttons firing one click event handler. But these buttons
do not have any text on them, they have an image painted in them
instead. Any way you can think of to distinguish which button fired
the handler?

Thanks in advance..

-BB


On Tue, 16 Sep 2003 14:10:43 +0100, "Peter Foot [MVP]"
<(E-Mail Removed)> wrote:

>The Click event handler has a sender argument - this will be the control
>which generated the event - e.g. menu item:-
>
>// The EventHandler - it fires for all generated MenuItems
>private void tmp_Click(object sender, EventArgs e)
>{
> // How can I distinguish _here_ _which_ MenuItem was hit?
>
> // sender
> MenuItem mnusender = (MenuItem)sender;
>
> //switch action based on text of control
> switch(ctlsender.Text)
> {
> case "Open":
> //do something here
> break;
>
> //etc
> }
>}
>
>
>Peter


 
Reply With Quote
 
Serg Kuryata [MS]
Guest
Posts: n/a
 
      14th Oct 2003
Hello,

Here is a sample code that distinguishes between 2 different buttons:

private void button_Click(object sender, System.EventArgs e)
{
Button button;

button = (Button)sender;
if (button == this.button1)
MessageBox.Show("Button 1");
else if (button == this.button2)
MessageBox.Show("Button 2");
}

You also can add property to your button class that you can preset and then
use to distinguish the buttons.

Hope this helps.
Thank you,
Sergiy.


This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| From: Brian Burgess <(E-Mail Removed)>
| Subject: Re: Click event in dynamic menu
| Date: Tue, 14 Oct 2003 16:54:30 +0800
| Message-ID: <(E-Mail Removed)>
| References: <(E-Mail Removed)>
<#(E-Mail Removed)>
| X-Newsreader: Forte Free Agent 1.92/32.572
| MIME-Version: 1.0
| Content-Type: text/plain; charset=us-ascii
| Content-Transfer-Encoding: 7bit
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: 210.17.157.96
| Lines: 1
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.compactframework:35901
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Hi Peter,
|
| Is there a way to do this if there is no text on the control? I have
| a set of buttons firing one click event handler. But these buttons
| do not have any text on them, they have an image painted in them
| instead. Any way you can think of to distinguish which button fired
| the handler?
|
| Thanks in advance..
|
| -BB
|
|
| On Tue, 16 Sep 2003 14:10:43 +0100, "Peter Foot [MVP]"
| <(E-Mail Removed)> wrote:
|
| >The Click event handler has a sender argument - this will be the control
| >which generated the event - e.g. menu item:-
| >
| >// The EventHandler - it fires for all generated MenuItems
| >private void tmp_Click(object sender, EventArgs e)
| >{
| > // How can I distinguish _here_ _which_ MenuItem was hit?
| >
| > // sender
| > MenuItem mnusender = (MenuItem)sender;
| >
| > //switch action based on text of control
| > switch(ctlsender.Text)
| > {
| > case "Open":
| > //do something here
| > break;
| >
| > //etc
| > }
| >}
| >
| >
| >Peter
|
|

 
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
Dynamic Menus + Click Event jonmiller1125@gmail.com Microsoft Dot NET Framework Forms 3 29th Feb 2008 01:51 AM
Click event on menu item is lost after first time firing of the event mayuresh.kasture@gmail.com Microsoft Excel Programming 1 2nd Apr 2007 01:25 PM
Context menu click event only works on second click =?Utf-8?B?QW5kcmV3IEUgQ2hhcG1hbg==?= Microsoft VB .NET 0 25th Feb 2006 10:41 AM
Dynamic LinkButton and Click event =?Utf-8?B?Q29kZVJhem9y?= Microsoft C# .NET 3 30th Jun 2005 11:10 AM
Generic menu event for a menu with dynamic items Mac via DotNetMonster.com Microsoft VB .NET 2 26th May 2005 01:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:15 PM.