我想将文本按钮添加到WinForms工具栏上(工具栏上的标准按钮只能包含图像)。工具栏上的文本按钮
一个文本框,一个组合框可以很容易地添加到工具栏上,但没有文本按钮的选项。
这怎么能实现?
UPD按'文本按钮'我的意思是标准的Windows按钮。
我想将文本按钮添加到WinForms工具栏上(工具栏上的标准按钮只能包含图像)。工具栏上的文本按钮
一个文本框,一个组合框可以很容易地添加到工具栏上,但没有文本按钮的选项。
这怎么能实现?
UPD按'文本按钮'我的意思是标准的Windows按钮。
可以使用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
相同的效果。
设置ToolStripItem.DisplayStyle到Text:
var toolStripButton = new ToolStripButton();
toolStripButton.DisplayStyle = ToolStripItemDisplayStyle.Text;
它不工作,'Add'方法采用'ToolBarButton' ,而不是'Button'。另外''ToolBar'已经过时了,'ToolStrip'已经取代了它,'ToolStrip'也没有一个简单按钮的方法。 – Centro
我的意思是'toolbar.Controls.Add(button)'。 –
@Centro你不是指[System.Windows.Forms.ToolBar](http://msdn.microsoft.com/en-us/library/system.windows.forms.toolbar.aspx)? –