I'm programmatically adding ToolStripButton items to a context menu.
我是以编程方式将ToolStripButton项添加到上下文菜单中。
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.
我该怎么做呢?处理点击的方法还需要接收某种与用户点击的特定ToolStripButton相关的id或对象。
2 个解决方案
#1
3
Couldn't you just subscribe to the Click event? Something like this:
难道你不能订阅Click事件吗?像这样的东西:
ToolStripButton btn = new ToolStripButton("The text on the item.");
this.tsmiDelete.DropDownItems.Add(btn);
btn.Click += new EventHandler(OnBtnClicked);
And OnBtnClicked would be declared like this:
并且OnBtnClicked将被声明为:
private void OnBtnClicked(object sender, EventArgs e)
{
ToolStripButton btn = sender as ToolStripButton;
// handle the button click
}
The sender should be the ToolStripButton, so you can cast it and do whatever you need to do with it.
发件人应该是ToolStripButton,因此您可以投射它并做任何您需要做的事情。
#2
0
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.
谢谢你对安迪的帮助。我现在唯一的问题是AutoSize不能处理我正在添加的ToolStripButtons!他们太狭隘了。
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:
更新:对于以编程方式创建的ToolStripButtons,AutoSize肯定存在问题。但是,我找到了一个解决方案:
- Create the ToolStripButton.
- Create a label control and set the font properties to match your button.
- Set the text of the label to match your button.
- Set the label to AutoSize.
- Read the width of the label and use that to set the width of the ToolStripButton.
创建ToolStripButton。
创建标签控件并设置字体属性以匹配您的按钮。
设置标签的文本以匹配您的按钮。
将标签设置为AutoSize。
读取标签的宽度并使用它来设置ToolStripButton的宽度。
It's hacky, but it works.
这很hacky,但它确实有效。
#1
3
Couldn't you just subscribe to the Click event? Something like this:
难道你不能订阅Click事件吗?像这样的东西:
ToolStripButton btn = new ToolStripButton("The text on the item.");
this.tsmiDelete.DropDownItems.Add(btn);
btn.Click += new EventHandler(OnBtnClicked);
And OnBtnClicked would be declared like this:
并且OnBtnClicked将被声明为:
private void OnBtnClicked(object sender, EventArgs e)
{
ToolStripButton btn = sender as ToolStripButton;
// handle the button click
}
The sender should be the ToolStripButton, so you can cast it and do whatever you need to do with it.
发件人应该是ToolStripButton,因此您可以投射它并做任何您需要做的事情。
#2
0
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.
谢谢你对安迪的帮助。我现在唯一的问题是AutoSize不能处理我正在添加的ToolStripButtons!他们太狭隘了。
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:
更新:对于以编程方式创建的ToolStripButtons,AutoSize肯定存在问题。但是,我找到了一个解决方案:
- Create the ToolStripButton.
- Create a label control and set the font properties to match your button.
- Set the text of the label to match your button.
- Set the label to AutoSize.
- Read the width of the label and use that to set the width of the ToolStripButton.
创建ToolStripButton。
创建标签控件并设置字体属性以匹配您的按钮。
设置标签的文本以匹配您的按钮。
将标签设置为AutoSize。
读取标签的宽度并使用它来设置ToolStripButton的宽度。
It's hacky, but it works.
这很hacky,但它确实有效。