在 C# 中动态过滤菜单 (ContextMenuStrip) 而不重新创建菜单?
这可能吗?
我计划有 10 个菜单项,其中将有子菜单项(仅 1 级深)。我希望能够在用户在 TextBox
控件中键入内容时过滤它们。我知道我可以在第一次打开菜单时过滤项目,但我希望能够在用户键入时不断过滤它,并在类别菜单项没有适用于当前过滤器的子项目时动态隐藏类别(通过名称过滤)。
有什么想法吗?
Is this possible?
I plan to have 10 menu items where these are going to have sub-menu items (1 level deep only). I want to be able to filter them when the user types into my TextBox
control. I know I can filter items upon opening the menu for the first time, but I want to be able to continually filter it as the user types and hide categories on the fly when the category menu item has no subitems applicable for the current filter (by name filtering).
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我添加了一个上下文菜单条(menuStrip1)。为此,我添加了以下内容:
然后添加了一个文本框 (FilterMenuText),并在 OnTextChanged 事件中执行以下操作:
这将根据子菜单项的内容适当隐藏和显示顶级菜单项。如果您的菜单有不止一级的下拉菜单,请将 foreach 放入递归函数中,例如:
I added a context menu strip (menuStrip1). To this I added the following:
I then added a text box (FilterMenuText), and, on the OnTextChanged event, do the following:
This will hide and show the top level MenuItems as appropriate based on the content of the child menu items. If your menu has more than one level of drop down, put the foreach into a recursive function, like:
这就是我用来有条件地在上下文菜单上显示菜单项的方法。如果您使用相同的菜单并且不想显示它,您只需将同一循环中的每个项目设置为 true 即可;
This is what I used for conditionaly showing menu items on a context menu. If you use the same menu and don't want to show it, you merely set each item in the same loop to true;
当您不希望工具条对象出现时,将其 Visible 属性设置为 false。
Set the toolstrip object's Visible property to false when you don't want it to appear.
出于示例目的,我使用网络表单应用程序完成了此操作
for example purposes I have done this using a web forms application