English 中文(简体)
工具包
原标题:ToolStripMenuItem and KeyPress or KeyDown event
  • 时间:2011-11-14 08:39:32
  •  标签:
  • c#
  • keypress

我正在使用<代码>。 工具包:

toolStripDropDownButton1.DropDownItems.Clear();

ToolStripMenuItem item1 = new ToolStripMenuItem("Item1");
toolStripDropDownButton1.DropDownItems.Add(item1);

ToolStripMenuItem item2 = new ToolStripMenuItem("Item2");
toolStripDropDownButton1.DropDownItems.Add(item2);

我愿删除Delete钥匙受压力时选定的菜单。 但<代码> 工具包 <代码>KeyPress或KeyDown

我正在使用2010年视觉演播室和4.0.NET。 任何关于如何实现这一功能的建议?

问题回答

You can use DropDown element of your menu, then bind KeyDown event to it. Now you know which key is pressed on the menu, but you don t know which menu item is pointed with mouse cursor. Bind MouseEnter event of ToolStripMenuItems and you can now store which one is pointed. Now you know which item is pointed and which key is pressed when DropDown s KeyDown event is triggered.

It sounds as if you want to delete the selected item from a dropdown list when you click on separate delete button. Is that what you re trying to do? If so then you need to be looking at the click event of the delete button and deleting the list item based upon the current selected item. KeyPress of KeyDown aren t needed.

The enclosing ToolStrip gets the key events so you can handle it there with something like:

toolStripDropDownButton1.KeyDown += (s, e) =>
{
    if (e.KeyCode == Keys.Delete)
    {
        foreach (var item in ((ToolStrip)s).Items.OfType<ToolStripMenuItem>())
        {
            if (item.Selected)
            {
                ((ToolStrip)s).Items.Remove(item);
                break;
            }
        }
    }
};

(该法典完全没有测试)

我在活动手里使用了(ToolStrip)s,如果你想在其他地方确定一种固定的方法,可以由多个不同的工具《Strip》加以使用。 当然,在这种情况下,请将(ToolStrip)s改为toolStripDropDownButton1





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签