如何在 C# 中以编程方式连接 ToolStripButton 事件?
我以编程方式将 ToolStripButton 项添加到上下文菜单中。
那部分很容易。
this.tsmiDelete.DropDownItems.Add("The text on the item.");
但是,我还需要连接事件,以便当用户单击该项目时实际会发生一些事情!
我该怎么做呢? 处理单击的方法还需要接收与用户单击的特定 ToolStripButton 相关的某种 id 或对象。
I'm programmatically adding ToolStripButton items to a context menu.
That part is easy.
this.tsmiDelete.DropDownItems.Add("The text on the item.");
However, I also need to wire up the events so that when the user clicks the item something actually happens!
How do I do this? The method that handles the click also needs to receive some sort of id or object that relates to the particular ToolStripButton that the user clicked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不能直接订阅Click事件吗? 像这样的东西:
OnBtnClicked 将这样声明:
发送者应该是 ToolStripButton,因此您可以投射它并用它做任何您需要做的事情。
Couldn't you just subscribe to the Click event? Something like this:
And OnBtnClicked would be declared like this:
The sender should be the ToolStripButton, so you can cast it and do whatever you need to do with it.
感谢您对安迪的帮助。 我现在唯一的问题是 AutoSize 不适用于我添加的 ToolStripButtons! 他们都太狭窄了。
这很奇怪,因为它很早就开始工作了。
更新:以编程方式创建的 ToolStripButton 的 AutoSize 肯定有问题。 不过,我找到了一个解决方案:
这很hacky,但是很有效。
Thanks for your help with that Andy. My only problem now is that the AutoSize is not working on the ToolStripButtons that I'm adding! They're all too narrow.
It's rather odd because it was working earlier.
Update: There's definitely something wrong with AutoSize for programmatically created ToolStripButtons. However, I found a solution:
It's hacky, but it works.