How do you get the selected ContextMenu item?

D

DrPete

Hi

I have a contextmenu that is created by looping through a datarow view
and in the foreach loop I have this:

ToolStripMenuItem menuItem = new ToolStripMenuItem();
menuItem.Text = row["Name"].ToString();
menuItem.Name = row["StockCategoryId"].ToString();
menuItem.Click += new EventHandler(MoveTo_Click);
moveToMenuItem.DropDownItems.Add(menuItem);

The MoveTo_Click event performs something based on what
ToolStripMenuItem was chosen.

How do I get what this chosen item was?

Thanks for your time
Peter
 
A

Aboulfazl Hadi

Hi Peter
You can use sender parameter of MoveTo_Click :

void MoveTo_Click(object sender, EventArgs e)
{
ToolStripMenuItem m = (ToolStripMenuItem)sender;

MessageBox.Show(m.Text);

}
You can use Tag property of ToolStripMenuItem to store RowIndex or
PrimaryKey to better finding current row.

A.Hadi
 
D

DrPete

Thanks for the reply Abu

Never realised the object could be casted like that!

The tag property was going to be my second question :) Thanks

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