【发布时间】:2011-07-30 22:23:21
【问题描述】:
我想在 WinForms 工具栏上添加一个文本按钮(工具栏上的标准按钮只能包含图像)。
文本框、组合框可以轻松添加到工具栏上,但没有文本按钮选项。
如何实现?
UPD “文本按钮”是指标准的 Windows 按钮。
【问题讨论】:
我想在 WinForms 工具栏上添加一个文本按钮(工具栏上的标准按钮只能包含图像)。
文本框、组合框可以轻松添加到工具栏上,但没有文本按钮选项。
如何实现?
UPD “文本按钮”是指标准的 Windows 按钮。
【问题讨论】:
将ToolStripItem.DisplayStyle 设置为Text:
var toolStripButton = new ToolStripButton();
toolStripButton.DisplayStyle = ToolStripItemDisplayStyle.Text;
【讨论】:
您可以使用ToolStripControlHost 在工具条中嵌入任何控件:
Button button = new Button();
button.Text = "Standard Windows Button";
yourToolStrip.Items.Add(new ToolStripControlHost(button));
【讨论】:
我假设您指的是text button 一个不仅包含图像还包含文本的按钮。所以:
ToolBar toolbar = new ToolBar();
Button button = new Button();
button.Text = "Hi!";
button.Image = Image.FromFile("Your image path");//or from resource..
toolbar.Add(button);
this.Controls.Add(toolbar);
编辑:既然你的意思是ToolStrip,你可以这样做:
string text = "Hi";
Image image = Image.FromFile("Your image path");
ToolStripButton toolButton = new ToolStripButton(text, image);
toolButton.TextImageRelation = TextImageRelation.Overlay;//or what ever you want to
toolStrip1.Items.Add(toolButton);
编辑:
看起来像一个菜单项(鼠标悬停时突出显示的标签),而不是标准按钮。
不幸的是,微软提供的。如果您不喜欢它,请继承 ToolStripItem 并设计您自己的。
还请注意,您可以使用toolButton.BackgroundImage,但它也不会给您带来与普通Button 相同的效果。
【讨论】:
Add 方法采用 ToolBarButton,而不是 Button。另外ToolBar 已经过时了,ToolStrip 已经取代了它,ToolStrip 也没有简单按钮的方法。
toolbar.Controls.Add(button)。
ToolBar 工具栏,不适用于ToolStrip,因为它没有属性Controls。
ToolStrip 类而不是旧类 ToolBar。