将 ToolStripMenuItem 动态添加到 MenuStrip (C#/ Winforms)
我已经实施了我的解决方案(基本解决方案),我很高兴。
问题是,当我使用“Add”方法向 ToolStripItemCollection 添加新项目时,我得到了一些重载……有意义的重载是字符串参数、图像参数和 EventHandler 参数。
因为我的下拉列表将在运行时充当动态历史记录,这意味着它在编译时将为空。这意味着我无法通过使用设计器界面的标准途径(单击时)添加事件处理程序。我被迫使用上面描述的过载。
我的图像对我来说没有用,但动态添加事件处理程序是我感兴趣的并且需要帮助。
URL: http://msdn.microsoft.com/en-us/library/bxdt0s8t .aspx
没有其他重载可以帮助我,所以我必须使用 Image ...任何人都有任何想法来解决这个问题并向我展示如何完全满足 add 方法的这个重载版本。
TIA。
更新:我在当前项目中再次执行了此操作,但使用了更流畅的代码,但原理是相同的,在运行时动态添加事件处理程序。当我回家时,我会用一些示例代码更新它。
I have my solution implemented (basic solution) and I'm happy.
Problem is when I add new items to a ToolStripItemCollection using the 'Add' method, I get a few overloads ... the meaningful one being a string parameter, an image parameter and an EventHandler parameter.
Because my drop down list is going to be serving as a dynamic history at RunTime, means it going to be empty at compile time. This means I can't add an event handler through the standard route of using the designer surface (On click). I am forced to use the overload described above.
I image is of no use to me but adding the event handler dynamically is what I am interested in and need help with.
URL: http://msdn.microsoft.com/en-us/library/bxdt0s8t.aspx
There is no other overload to help me, so I have to use an Image ... anyone got any ideas to get around this and show me how to fully satisfy this overloaded version of the add method.
TIA.
UPDATE: I re did this again in a current project but using more slicker code but the principle is the same, add event handlers dynamically at run time. I will update this with some sample code when I get home.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的方法是创建一个
ToolStripMenuItems
数组,并用我要添加的项目填充该数组。我创建了一种方法来处理单击事件,并让它检查我在运行时创建的每个项目的独特之处。您可以尝试使用每个ToolStripMenuItem
的Name
或Tag
属性。然后在要添加的菜单中使用AddRange
。所以你的代码可能看起来像这样:The way I do it is to create an array of
ToolStripMenuItems
and populate that array with the items I'm adding. I create one method to handle the click events and have it check something unique about each item I create at run-time. You might try using theName
orTag
properties of eachToolStripMenuItem
. Then useAddRange
on the spot in the menu you're adding to. So your code might look something like this:有什么问题:
我错过了什么吗?
What is wrong with:
Am I missing something?
您只需为图像传递 null 即可。
You can simply pass null for the image.
我和菲利普·华莱士也遇到过类似的问题。请务必注意 ToolStripItem 和 ToolStripMenuItem 之间的区别。我将 ToolStripItems 添加到 ToolStripMenuItem 的 DropDownItems 中,它们会显示,并且所有属性设置正确,并且可以在代码中访问,但它们不会显示任何文本!切换到 ToolStripMenuItem 解决了这个问题。
关于最初的问题,我一直在使用空构造函数,并设置我需要的字段。 (我在使用 .net 4.0 的 vb.net 中,它不会让我调用
New ToolStripMenuItem()
因为它有一个MustInherit
标记,所以我做了这个班级:I was having similar issues as Philip Wallace. It's important to note the difference between a ToolStripItem, and a ToolStripMenuItem. I was adding ToolStripItems to a ToolStripMenuItem's DropDownItems, and they would show up, and have all properties set correctly, and accessible in code, but they would not show any text! Switching to a ToolStripMenuItem solved this.
In regards to the original question, I've been using the empty constructor, and setting the fields I needed. (I'm in vb.net with .net 4.0, and it won't let me call
New ToolStripMenuItem()
since it has aMustInherit
tag, so I made this class: