0

我有一个包含一些项目的上下文菜单。其中一个项目有一个子菜单(或其他任何名称),其中包含一些项目(取决于它找到的文件)。

我想要做的是,当我左键单击其中一个子项时,我希望发生一件事,而当我右键单击时,我希望发生另一件事。

我的问题是,当我使用 filesToolStripMenuItem_DropDownItemClicked 时,我没有在参数中得到任何 MouseEventArgs,所以我无法找出是哪个鼠标按钮用于单击该项目。

我尝试自己将其添加到参数中,但随后出现了一些错误。

有谁知道我该如何解决这个问题?所以我可以找出是用什么鼠标按钮来单击子项(这是一个 ToolStripMenuItem)?

谢谢

编辑:这是我用来创建子项目的代码:

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

对于这些项目,我希望能够根据我使用的鼠标按钮做不同的事情

4

2 回答 2

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

这应该可以帮助您入门。

回复:您正在编辑:

问题是,您只是在说 new ToolStripMenuItem(IpA[i].ToString()) 而没有保留对它的引用。以下是您需要执行的操作:

 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.
    }
于 2009-02-06T15:04:47.597 回答
0

您可以访问子菜单项吗?如果是这样,您可以处理子项目上的 MouseDown 事件。

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;
}
于 2009-02-06T15:11:06.493 回答