我想將文本按鈕添加到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)? –