6

A ToolStripComboBox is placed after a ToolStripButton and is folowed by another one, which is right-aligned. How do I best set up the ToolStripComboBox to always adjust its length to fill all the space available between the preceeding and the folowing ToolStripButtons?

In past I used to handle a parent resize event, calculate the new length to set based on neighboring elements coordinates and setting the new size. But now, as I am developing a new application, I wonder if there is no better way.

Ivan
  • 63,011
  • 101
  • 250
  • 382

3 Answers3

6

I use the following with great success:

private void toolStrip1_Layout(System.Object sender, System.Windows.Forms.LayoutEventArgs e)
{
    int width = toolStrip1.DisplayRectangle.Width;

    foreach (ToolStripItem tsi in toolStrip1.Items) {
        if (!(tsi == toolStripComboBox1)) {
            width -= tsi.Width;
            width -= tsi.Margin.Horizontal;
        }
    }

    toolStripComboBox1.Width = Math.Max(0, width - toolStripComboBox1.Margin.Horizontal);
}

The above code does not suffer from the disapearing control problem.

Martin
  • 39,569
  • 20
  • 99
  • 130
  • 2
    Using VS2010/.NET 4: If I resize the form very quickly I still get the disappearing controls problem. But it's easily solved by adding toolStrip1.PerformLayout() in the form's resize event handler. –  Feb 09 '11 at 11:00
  • This doesn't appear to work properly when using a dpi setting other than 96 dpi. The width will be scaled after setting it making it too large. – kjbartel May 01 '15 at 07:07
2

There's no automatic layout option for this. But you can easily do it by implementing the ToolStrip.Resize event. This worked well:

    private void toolStrip1_Resize(object sender, EventArgs e) {
        toolStripComboBox1.Width = toolStripComboBox2.Bounds.Left - toolStripButton1.Bounds.Right - 4;
    }
    protected override void OnLoad(EventArgs e) {
        toolStrip1_Resize(this, e);
    }

Be sure to set the TSCB's AutoResize property to False or it won't work.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Here goes a problem (I can't remember I had such in past, when I was using V2005). On each odd significant (the effect does not take place if size difference is as small as couple of pixels or so) size decrease a ToolStripComboBox disappears (appears if I decrease it once again (and is going to disappear again if I decrease it once more) or increase it). AutoResize is set to false, increasing a digit to be subtracted (which is 4 in your example) didn't help. The ToolStrip located inside a SplitContainer panel and is resized each time a mannel is resized by dragging the SplitContainer splitter. – Ivan May 05 '10 at 01:49
  • The most common occurrence case of the problem pattern I describe is restoring a form from maximized state - both ToolStripComboBoxes (in the left panel and in the right panel) disappear in this case. – Ivan May 05 '10 at 01:52
  • To prevent the `ToolStripComboBox` from dissappearing after the `User Form` was resized from maximized state I used this hack: `ToolStripComboBox.Visible = false; ToolStripComboBox.Visible = true;` inside of the `toolStrip1_Resize` event handler right after `toolStripComboBox1.Width = ...`. HTH – Daniel Dušek Sep 02 '16 at 13:26
1
ToolStrip ts = new ToolStrip();

ToolStripComboBox comboBox = new TooLStripComboBox();
comboBox.Dock = DockStyle.Fill;

ts.LayoutStyle = ToolStripLayoutStyle.Table;
((TableLayoutSettings)ts.LayoutSettings).ColumnCount = 1;
((TableLayoutSettings)ts.LayoutSettings).RowCount = 1;
((TableLayoutSettings)ts.LayoutSettings).SetColumnSpan(comboBox,1);

ts.Items.Add(comboBox);

Now the combobox will dock fill correctly. Set Column or Row span accordingly.

Fdej
  • 221
  • 2
  • 6