ToolStripMenuItem中的鼠标按钮

时间:2009-02-06 14:54:41

标签: c# winforms mouse toolstrip

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

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

我的问题是,当我使用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()));
}

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

2 个答案:

答案 0 :(得分: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());
    }

这应该有助于你开始。

RE:你正在编辑:

问题是,你只是说新的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.
    }

答案 1 :(得分:0)

您可以访问子MenuItem吗?如果是这样,您可以处理子项上的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;
}