Click event in dynamic menu

  • Thread starter Christian Ahrenkiel
  • Start date
C

Christian Ahrenkiel

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);
}

// 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
 
P

Peter Foot [MVP]

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
 
B

Brian Burgess

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
 
S

Serg Kuryata [MS]

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 <[email protected]>
| Subject: Re: Click event in dynamic menu
| Date: Tue, 14 Oct 2003 16:54:30 +0800
| Message-ID: <[email protected]>
| References: <[email protected]>
<#[email protected]>
| 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]"
|
| >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
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top