问题
I have a context menu with a few items. One of the items has a submenu (or whatever it's called) with a few items (depends on what files it finds).
What I want to do is when I left click one of the sub-items I want one thing to happen, and when I right click I want another thing to happen.
My problem is that when I use the filesToolStripMenuItem_DropDownItemClicked, I don't get any MouseEventArgs in the parameter, so I can't find out which mouse button was used to click the item.
I tried adding it myself in the parameter but I get some error then.
Does anyone know how I can fix this? So I can find out what mouse button was used to click the sub-item (which is a ToolStripMenuItem)?
Thanks
edit: here is the code I use to create the sub items:
IPHostEntry ipE = Dns.GetHostEntry(Dns.GetHostName());
IPAddress[] IpA = ipE.AddressList;
for (int i = 0; i < IpA.Length; i++)
{
if (!IpA[i].ToString().Contains(":"))
cxItems.Items.Add(new ToolStripMenuItem(IpA[i].ToString()));
}
And for those items I want to be able to do different things depending on which mouse button I use
回答1:
private void button2_Click(object sender, EventArgs e)
{
ToolStripMenuItem item1 = new ToolStripMenuItem("Menu1");
ToolStripMenuItem subMenuitem1 = new ToolStripMenuItem("SubMenu");
item1.DropDownItems.Add(subMenuitem1);
this.contextMenuStrip1.Items.Add(item1);
subMenuitem1.MouseDown += new MouseEventHandler(subMenuitem1_MouseDown);
this.contextMenuStrip1.Show(this.button2,new Point(0,0));
}
void subMenuitem1_MouseDown(object sender, MouseEventArgs e)
{
//e.Button will determine which button was clicked.
MessageBox.Show(e.Button.ToString());
}
That should help get you started.
RE: You're edit:
The problem is, you're just saying new ToolStripMenuItem(IpA[i].ToString()) without keep a reference to it. Here's how you need to do it:
IPHostEntry ipE = Dns.GetHostEntry(Dns.GetHostName());
IPAddress[] IpA = ipE.AddressList;
for (int i = 0; i < IpA.Length; i++)
{
if (!IpA[i].ToString().Contains(":"))
{
ToolStripMenuItem subItem = new ToolStripMenuItem(IpA[i].ToString());
subItem.MouseDown += new MouseEventHandler(subItem_MouseDown);
cxItems.Items.Add(subItem);
}
}
void subMenuitem1_MouseDown(object sender, MouseEventArgs e)
{
//get a reference to the menu that was clicked
ToolStripMenuItem clickedMenu = sender as ToolStripMenuItem;
//e.Button will tell you which button was clicked.
}
回答2:
Can you get access to the sub MenuItem? If so you can handle the MouseDown event on the sub item.
private System.Windows.Forms.ToolStripMenuItem optionToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem subitemToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem subItem2ToolStripMenuItem;
public void init()
{
//
// optionToolStripMenuItem
//
this.optionToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.subitemToolStripMenuItem,
this.subItem2ToolStripMenuItem});
this.optionToolStripMenuItem.Name = "optionToolStripMenuItem";
this.optionToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.optionToolStripMenuItem.Text = "Option";
//
// subitemToolStripMenuItem
//
this.subitemToolStripMenuItem.Name = "subitemToolStripMenuItem";
this.subitemToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.subitemToolStripMenuItem.Text = "Subitem";
//
// subItem2ToolStripMenuItem
//
this.subItem2ToolStripMenuItem.Name = "subItem2ToolStripMenuItem";
this.subItem2ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.subItem2ToolStripMenuItem.Text = "SubItem2";
this.subItem2ToolStripMenuItem.MouseDown += new System.Windows.Forms.MouseEventHandler(this.subItem2ToolStripMenuItem_MouseDown);
this.subItem2ToolStripMenuItem.Click += new System.EventHandler(this.subItem2ToolStripMenuItem_Click);
}
private bool wasRightClicked = false;
private void subItem2ToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
wasRightClicked = true;
}
来源:https://stackoverflow.com/questions/520573/mouse-button-in-toolstripmenuitem