Dynamic Context Menu Items

W

www.dir

Hi,

I am adding dynamically context menu items like this:

MenuItem newStudent = new MenuItem( student.Name );
newStudent .Click += new System.EventHandler( this.StudentClicked );

And my method :

void StudentClicked ( object sender, System.EventArgs e )
{
}

And It is working just fine.
The problem is that I have only one method executed for every student
the User clicks on the context menu. I need to add a parameter or
something so when the StudentClicked method is executed I will know
which MenuItem ( Student ) was clicked. Normally I would add a student
number in the tag field, and when a the StudentClicked method is
executed I would get the tag, parse the number and I know thick student
was pressed on the menu. But MenuItems do not have tag.

Please advise.
 
J

Jim Hughes

Hi,

I am adding dynamically context menu items like this:

MenuItem newStudent = new MenuItem( student.Name );
newStudent .Click += new System.EventHandler( this.StudentClicked );

And my method :

void StudentClicked ( object sender, System.EventArgs e )
{
}

And It is working just fine.
The problem is that I have only one method executed for every student
the User clicks on the context menu. I need to add a parameter or
something so when the StudentClicked method is executed I will know
which MenuItem ( Student ) was clicked. Normally I would add a student
number in the tag field, and when a the StudentClicked method is
executed I would get the tag, parse the number and I know thick student
was pressed on the menu. But MenuItems do not have tag.

Please advise.

The "sender" indicates which MenuItem was clicked

void StudentClicked ( object sender, System.EventArgs e )
{
MenuItem selectedMenu = (MenuItem) sender;
// selectedMenu.Text contains the text of the selected student.Name
}

Another (and probably better) option would be to create a custom class that
inherits MenuItem and add additional properties like Tag to your custom menu
items and use that instead of the Text.

public class ExMenuItem : System.Windows.Forms.MenuItem

{

public ExMenuItem() {}

Object _Tag;

public Object Tag

{

get { return _Tag; }

set { _Tag = value; }

}

}


MenuItem newStudent = new ExMenuItem( student.Name );
newStudent .Click += new System.EventHandler( this.StudentClicked );
 
M

Mick Doherty

This is such a common request that I wrote a MenuExtender component
specifically to add a tag property to standard MenuItems. Use it in just the
same way that you would use the ToolTip Component. The MenuExtender also
automates RadioChecked menus.

You will find the sourcecode for the component on my site:
http://dotnetrix.co.uk/menus.html
 
W

www.dir

Thankes,
for the help but it seems that with creating new class the custom menu
is not working correct.

I have created a new class like this:

public class ExMenuItem : MenuItem
{
private Object _tag;
public Object MyTag
{
get
{
return _tag;
}

set
{
_tag = value;
}
}
}

and I add a dynamic sub menu to a context menu like this:

folders.MenuItems.Clear(); // this is node from my context menu

foreach( TreeNode folderNode in myTreeView.Nodes )
{
ExMenuItem folderMenuItem = new ExMenuItem();
folderMenuItem.Text = folderNode .Text ;
folderMenuItem.MyTag = folderNode .Index;
folderMenuItem.Click += new EventHandler(this.ContexMenu_Click);
folders.MenuItems.Add(folderMenuItem);
}
ContextMenu2.Show(myTreeView,pShowPoint);

And I do this on PopUp event on the context menu
But it works only the first time I show the contextMenu, every other
time teh sub menu will not show.
I seems that folders.MenuItems.Clear() can't clear the menu items
correctly and createdMenuItemsCounter is counting more and more created
Items.
I suppose that Clear() is not able to destroy the ExMenuItem object
correctly.
What do you think?
 
M

Mick Doherty

Thankes,
for the help but it seems that with creating new class the custom menu
is not working correct.

I have created a new class like this:

public class ExMenuItem : MenuItem
{
private Object _tag;
public Object MyTag
{
get
{
return _tag;
}

set
{
_tag = value;
}
}
}

and I add a dynamic sub menu to a context menu like this:

folders.MenuItems.Clear(); // this is node from my context menu

foreach( TreeNode folderNode in myTreeView.Nodes )
{
ExMenuItem folderMenuItem = new ExMenuItem();
folderMenuItem.Text = folderNode .Text ;
folderMenuItem.MyTag = folderNode .Index;
folderMenuItem.Click += new EventHandler(this.ContexMenu_Click);
folders.MenuItems.Add(folderMenuItem);
}
ContextMenu2.Show(myTreeView,pShowPoint);

And I do this on PopUp event on the context menu
But it works only the first time I show the contextMenu, every other
time teh sub menu will not show.
I seems that folders.MenuItems.Clear() can't clear the menu items
correctly and createdMenuItemsCounter is counting more and more created
Items.
I suppose that Clear() is not able to destroy the ExMenuItem object
correctly.
What do you think?

You must remove the folders menuitem, add the child items and then readd the
folders menuitem.

This only happens because you are popping up the context menu with a
ContextMenu.Show() instead of assigning the ContextMenu to the TreeView
control.
 
W

www.dir

Thankes for the answer. Removing the category Menu Item works just
fine.
I am not assigning the Context Menu to the TreeView Control becouse I
have more than one Context Menus in my TreeView. And I can assign only
one Context Menu to a control.
 

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