Can I embed a ComboBox and a simple Button into a StatusStrip in WinForms?
Asked Answered
F

3

11

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?

Forman answered 12/2, 2011 at 15:4 Comment(2)
Why are not you satisfied with the proposed ones?Kaela
Proposed what? There is no ComboBox analogue offered for embedding into StatusStrip (a ComboBox is a drop-down list with a user-editable text field - I need a user to be able to enter text there), neither is TextBox (which I could use instead by combining it with a DropDownButton).Forman
F
15

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
 {
 }
Forgave answered 12/2, 2011 at 16:17 Comment(1)
Damn - this is golded. Been stumbling around in this for a few days and those attribute tags are a godsend!Ferreous
V
22

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 ;-)

Vannavannatta answered 29/3, 2011 at 16:59 Comment(0)
F
15

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
 {
 }
Forgave answered 12/2, 2011 at 16:17 Comment(1)
Damn - this is golded. Been stumbling around in this for a few days and those attribute tags are a godsend!Ferreous
S
1

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.

Sunnisunnite answered 20/7, 2016 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.