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:

  1. Add control to the form first
  2. 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:

  1. Add control that you want hosted to the form and set all relevant properties and events that you want handled
  2. Open DotNetBar designer and create new ControlContainerItem at the position where you want to host the control.
  3. Add event handler for ContainerLoadControl event on DotNetBarManager or you can assign the ControlContainerItem.Control property in code as well.
  4. 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.

C#

      private void ContainerLoadControl(object sender, System.EventArgs e)
      {
         BaseItem item=sender as BaseItem;
         if(item==null)
            return;
         if(item.Name=="bChangeStyle")
         {
            ControlContainerItem containerItem=item as ControlContainerItem;
            CheckBox box=new CheckBox();
            box.Text="OfficeXP Style";
            box.FlatStyle=FlatStyle.Flat;
            box.Size = new System.Drawing.Size(95, 17);
            box.TabStop=false;
            box.Checked=true;
            box.CheckedChanged+=new EventHandler(this.ChangeDotNetBarStyle);
            containerItem.Control=box;
         }
         .
         .
         .
		}

VB

      Private Sub ContainerLoadControl(ByVal sender As Object, ByVal e As System.EventArgs)
         Dim item As BaseItem=CType(sender,BaseItem)
         If item Is Nothing Then Exit Sub
 
         If item.Name="bChangeStyle" Then
            Dim containerItem As ControlContainerItem=CType(item,ControlContainerItem)
            Dim box As CheckBox=new CheckBox()
            box.Text="OfficeXP Style"
            box.FlatStyle=FlatStyle.Flat
            box.Size = new System.Drawing.Size(95, 17)
            box.TabStop=false
            box.Checked=true
            containerItem.Control=box
         End If
         .
         .
         .
	  End Sub

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.

Related posts:

  1. How To Enable Control Hosting On Quick Access Toolbar Items
  2. Using same Context Menu on multiple controls
  3. How to Create Popup Menu or Toolbar Using Code
  4. How to create Excel like popup border toolbar
  5. How to Create Toolbars Using Code