How to add any control to menu, toolbar and other DotNetBar controls

Adding any .NET control to DotNetBar is very easy with the help of ControlContainerItem object. Please note that this process is completely automatic using DotNetBar Windows Forms designer and you do not have to write any code if you do not have special requirements. To add the any control at design-time to toolbar, ribbonbar or other controls follow these steps:

Add control to the form first

Drag & drop control from the form onto the toolbar, ribbon bar or other DotNetBar control. The designer will automatically create the ControlContainerItem for you so process below is completely transparent.

Using code follow these steps to add any .NET control to your menu or toolbar:

Add control that you want hosted to the form and set all relevant properties and events that you want handled

Open DotNetBar designer and create new ControlContainerItem at the position where you want to host the control.

Add event handler for ContainerLoadControl event on DotNetBarManager or you can assign the ControlContainerItem.Control property in code as well.

In that event, recognize your ControlContainerItem created in step 2 by checking the name property and set the Control property to the control created in step 1

This sample creates new control in-line and sets its properties and events as opposed to the method that is described in instructions above. Both methods are valid and matter of personal preference. This is a sample code from the Notepad DotNetBarManager.ContainerLoadControl event.

If you are creating toolbars and menus from code we still recommend assigning the Control property of the ControlContainerItem from DotNetBarManager.ContainerLoadControl. That is important if you are saving the DotNetBar definition on end-user machine to preserve the toolbar layouts and/or any custom toolbars end-user has defined. If you are not saving the DotNetBar definitions on end-user machine then you can assign the Control property immediately after creating the ControlContainerItem object.

Note on disposing controls

If you are using ControlContainerItem to display Windows Forms controls on the popup menus or popup toolbars, note that you need to dispose controls you assign to ControlContainerItem by calling Dispose() method on control when form is disposed.