我可以将 ComboBox 和简单的 Button 嵌入到 WinForms 的 StatusStrip 中吗?

发布于 2024-10-17 03:23:54 字数 148 浏览 2 评论 0原文

默认情况下,ComboBox 和 Button 元素不属于 WinForms 设计器提供的添加到 StatusStrip 中的元素(而 DropDownButton 和 SplitButton 则属于)。有没有办法将它们添加到那里?据我所知,任何控件都可以嵌入其中,但是如何嵌入呢?

By default ComboBox and Button elements are not among those offered to add into a StatusStrip by WinForms designer (while DropDownButton and SplitButton are). Is there a way to add them there? As far as I've heard any control can be embedded there, but how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

开始看清了 2024-10-24 03:23:54

更容易的是,您可以剪切通过 ToolStrip 中的菜单创建的 ToolStripComboBox,并将其粘贴到 StatusStrip 中。
没有写任何代码行...并且它可以工作;-)

More easily, you can cut a ToolStripComboBox created via the menu in a ToolStrip and paste it in the StatusStrip.
No lines of code written... and it works ;-)

月野兔 2024-10-24 03:23:54

您可以轻松实现从 ToolStripControlHost 继承:

[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.MenuStrip |
                                       ToolStripItemDesignerAvailability.ContextMenuStrip | 
                                       ToolStripItemDesignerAvailability.StatusStrip)]
    public class ComboStripItem : ToolStripControlHost
    {
        private ComboBox combo;

        public ComboStripItem()
            : base(new ComboBox())
        {
            this.combo = this.Control as ComboBox;
        }

        // Add properties, events etc. you want to expose...
    }

重建解决方案后,您甚至可以在设计器中看到该项目:

表单设计器中的ComboStripItem

PS
该项目也可在 ContextMenuStripMenuStrip 中使用。

编辑:

要设置自定义图标,请使用 ToolboxBitmapAttribute

但是,我注意到实际上有一个名为 ToolStripComboBox 的内置组合框工具条项。
它对 StatusStrip 没有设计者可见性,但可以通过代码轻松地将其添加到 StatusStrip,或者,如果您愿意,您可以扩展它以提供完整的可见性:

 [ToolboxBitmapAttribute("image path or use another overload..."),
  ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.MenuStrip |
                                   ToolStripItemDesignerAvailability.ContextMenuStrip |
                                   ToolStripItemDesignerAvailability.StatusStrip)]
 public class ComboBoxItem : ToolStripComboBox
 {
 }

You can implement easily inheriting from ToolStripControlHost:

[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.MenuStrip |
                                       ToolStripItemDesignerAvailability.ContextMenuStrip | 
                                       ToolStripItemDesignerAvailability.StatusStrip)]
    public class ComboStripItem : ToolStripControlHost
    {
        private ComboBox combo;

        public ComboStripItem()
            : base(new ComboBox())
        {
            this.combo = this.Control as ComboBox;
        }

        // Add properties, events etc. you want to expose...
    }

After rebuilding your solution you will able to see the item even in the designer:

ComboStripItem in forms designer

P.S.
this item will be usable also in ContextMenuStrip and in MenuStrip.

EDIT:

To set a custom icon use ToolboxBitmapAttribute.

However, I noticed that actually there's a built-in combobox toolstrip item called ToolStripComboBox.
It has just no designer visibility for the StatusStrip , but it can be easily added to a StatusStrip by code, or, if you prefer, you can extend it giving the complete visibility:

 [ToolboxBitmapAttribute("image path or use another overload..."),
  ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.MenuStrip |
                                   ToolStripItemDesignerAvailability.ContextMenuStrip |
                                   ToolStripItemDesignerAvailability.StatusStrip)]
 public class ComboBoxItem : ToolStripComboBox
 {
 }
于我来说 2024-10-24 03:23:54

如果您想向 StatusStrip 添加简单按钮,可以使用设计器来实现。

首先,添加一个 DropDownButton。然后,在 DropDownButton 属性窗口中,将 ShowDropDownArrow 属性设置为 False

对您想要在 StatusStrip 中显示的每个其他简单按钮重复此操作。

If you want to add a simple button to your StatusStrip, you can do so using the Designer.

First, add a DropDownButton. Then, in the DropDownButton properties window, set the ShowDropDownArrow property to False.

Repeat for each additional simple button that you want to show in your StatusStrip.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文