ToolStripMenuItem中的鼠标按钮 [英] Mouse button in ToolStripMenuItem

查看:232
本文介绍了ToolStripMenuItem中的鼠标按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些项目的上下文菜单.其中一项包含一个子菜单(或称其为子菜单),其中包含若干项(取决于找到的文件).

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.

我的问题是,当我使用filesToolStripMenuItem_DropDownItemClicked时,参数中没有任何MouseEventArgs,因此我无法找到用于单击该项目的鼠标按钮.

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.

有人知道我该如何解决吗?因此,我可以找出用来单击子项目(即ToolStripMenuItem)的鼠标按钮了吗?

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)?

谢谢

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

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

推荐答案

 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:您正在

问题是,您只是在说新的ToolStripMenuItem(IpA [i] .ToString())而没有对其进行引用.这是您需要执行的操作:

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.
    }

这篇关于ToolStripMenuItem中的鼠标按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆