Restrict adding controls to ToolStrip

313 views Asked by At

I am designing a series of ToolStrip controls for specific jobs eg Font ToolStrip. What I need to do is stop user and developers from adding or removing controls from my ToolStrips.

Any help will be gratefully accepted Danny

1

There are 1 answers

0
Reza Aghaei On BEST ANSWER

To create such custom ToolStrip you should perform this steps:

  1. Create a class and inherit from ToolStrip.
  2. Disable default designer of ToolStrip.
  3. Hide Items property in property grid.

Here is detailed descriptions about steps:

  • Create a class and inherit from ToolStrip.

    This way you can add some items to your custom toolstrip at designer. To do so, after creating your class open it in design mode (it may take some times to visual studio detect it can open the file in designer). Then go to properties window and using Items property add some items. You can also select items from dropdown above properties window and in events tab, handle Click event and add some logic.

    Note: After adding items, go to code view and call InitializeComponent(); is constructor.

  • Disable default designer of ToolStrip

    You should disable default designer of ToolStrip to prevent changing items at design-time, so users can not use designer versb or design surface to change items. To do so, it's enough to decorate your custom component class with [Designer(typeof(ControlDesigner))] attribute.

    [Designer(typeof(ControlDesigner))]
    public class MyToolStrip : ToolStrip
    {
        //...
    }
    
  • Hide Items property in property grid.

    You should hide items property aslo in property grid to prevent changing items through property grid. To do so, it's enough to override Items property and decorate it with [Browsable(false)].

    [Browsable(false)]
    public override ToolStripItemCollection Items
    {
        get
        {
            return base.Items;
        }
    }