【问题标题】:Text button on toolbar工具栏上的文本按钮
【发布时间】:2011-07-30 22:23:21
【问题描述】:

我想在 WinForms 工具栏上添加一个文本按钮(工具栏上的标准按钮只能包含图像)。

文本框、组合框可以轻松添加到工具栏上,但没有文本按钮选项。

如何实现?

UPD “文本按钮”是指标准的 Windows 按钮。

【问题讨论】:

    标签: .net winforms toolbar


    【解决方案1】:

    ToolStripItem.DisplayStyle 设置为Text

    var toolStripButton = new ToolStripButton();
    toolStripButton.DisplayStyle = ToolStripItemDisplayStyle.Text;
    

    【讨论】:

      【解决方案2】:

      您可以使用ToolStripControlHost 在工具条中嵌入任何控件:

      Button button = new Button();
      button.Text = "Standard Windows Button";
      yourToolStrip.Items.Add(new ToolStripControlHost(button));
      

      【讨论】:

        【解决方案3】:

        我假设您指的是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)
        • @Centro 你不是说System.Windows.Forms.ToolBar吗?
        • 它没有按预期工作,它在现有工具栏按钮的工具栏上添加了一个按钮,因此必须将按钮位置设置为不太优雅的位置。而且它仅适用于ToolBar 工具栏,不适用于ToolStrip,因为它没有属性Controls
        • 在我的问题中,我的意思是应该在 WinForms 中使用的 ToolStrip 类而不是旧类 ToolBar
        猜你喜欢
        • 1970-01-01
        • 2013-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-30
        • 2015-03-20
        • 1970-01-01
        相关资源
        最近更新 更多