Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How can I add a User Control to a ToolStrip?

ToolStripControlHost is the base class for our ToolStripTextBox, ToolStripComboBox, ToolStripProgressBar, and can wrap any user control to be hosted in the ToolStrip. There are multiple ways to use ToolStripControlHost:: Inherit from ToolStripControlHost or use ToolStripControlHost directly.

toolStrip1.Items.Add(new ToolStripControlHost(new TrackBar()));
Sample showing a user control in a dropdown -
  private void button1_Click(object sender, EventArgs e)
  {
   ToolStripDropDown toolStripDropDown = new ToolStripDropDown();
   // Create some user control
   UserControl1 uc = new UserControl1();
   uc.Margin = Padding.Empty;
   toolStripDropDown.SuspendLayout();

   // create the control host to host the user control - make sure it has no margin
   ToolStripControlHost host = new ToolStripControlHost(uc);
   host.Margin = Padding.Empty;

   // add the control host to the toolstripdropdown
   toolStripDropDown.Items.Add(host);

   // set the padding of the toolstripdropdown to be empty
   toolStripDropDown.Padding = Padding.Empty;

   // show no borders
   toolStripDropDown.Renderer = new BorderlessRenderer();

   toolStripDropDown.ResumeLayout();

   toolStripDropDown.Show(this.button1, 10, 10);

  }
  private class BorderlessRenderer : ToolStripProfessionalRenderer
  {
   protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
   {
  // do nothing  
   }

  }
Creating a wrapper for your ToolStripControlHost

The following sample shows how to wrap a TrackBar by inheriting from ToolStripControlHost.  It is possible to just host the trackbar by these four lines of code:

TrackBar t = new TrackBar();
t.AutoSize = false; // the TrackBar wants to be 45px high by default, turning AutoSize = false fixes this
t.Height = 16;
toolStrip.Items.Add(new ToolStripControlHost(t));

However, if you want to use the TrackBar in the designer, you'll have to create a wrapper around the ToolStripControlHost class.  The following sample shows how to wrap a property (TrackBar.Value), and an event (TrackBar.ValueChanged).

 [System.ComponentModel.DesignerCategory("code")]
 [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]
 public partial class ToolStripTrackBar : ToolStripControlHost {
  public ToolStripTrackBar() : base(CreateControlInstance()) {
  
  }
  /// <summary>
  /// Create a strongly typed property called TrackBar - handy to prevent casting everywhere.
  /// </summary>
  public TrackBar TrackBar {
   get {
    return Control as TrackBar;
   }
  }
  /// <summary>
  /// Create the actual control, note this is static so it can be called from the
  /// constructor.
  /// 
  /// </summary>
  /// <returns></returns>
  private static Control CreateControlInstance() {
   TrackBar t = new TrackBar();
   t.AutoSize = false;
   t.Height = 16;
   // Add other initialization code here.
   return t;
  }
  [DefaultValue(0)]
  public int Value {
   get { return TrackBar.Value; }
   set { TrackBar.Value = value; }
  }

  /// <summary>
  /// Attach to events we want to re-wrap
  /// </summary>
  /// <param name="control"></param>
  protected override void OnSubscribeControlEvents(Control control) {
   base.OnSubscribeControlEvents(control);
   TrackBar trackBar = control as TrackBar;
   trackBar.ValueChanged += new EventHandler(trackBar_ValueChanged);
  }
  /// <summary>
  /// Detach from events.
  /// </summary>
  /// <param name="control"></param>
  protected override void OnUnsubscribeControlEvents(Control control) {
   base.OnUnsubscribeControlEvents(control);
   TrackBar trackBar = control as TrackBar;
   trackBar.ValueChanged -= new EventHandler(trackBar_ValueChanged);
  }

  /// <summary>
  /// Routing for event
  /// TrackBar.ValueChanged -> ToolStripTrackBar.ValueChanged
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  void trackBar_ValueChanged(object sender, EventArgs e) {
   // when the trackbar value changes, fire an event.
   if (this.ValueChanged != null) {
    ValueChanged(sender, e);
   }
  }
  // add an event that is subscribable from the designer.
  public event EventHandler ValueChanged;

  // set other defaults that are interesting
  protected override Size DefaultSize {
   get {
    return new Size(200, 16);
   }
  }
  
 }


Page view counter