Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a ToolStripComboBox to fill all the space available on a ToolStrip?

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.

like image 309
Ivan Avatar asked May 04 '10 21:05

Ivan


People also ask

How do I set up Toolstripitem?

Creating Menu Strips and Tool Strip Menu Items You can create a MenuStrip at design time in the same way that you create any control: by dragging it from the Toolbox onto the design surface. Once it has been added to the design surface, an interface for creating tool strip menu items appears.

What is Tool Strip?

toolstrip (plural toolstrips) (graphical user interface) A toolbar capable of displaying buttons and other elements such as dropdown menus.

What is ToolStrip C#?

A ToolStrip control is nothing but a container without adding its child controls. A ToolStrip control is capable of hosting Button, Label, SplitButton, DropDownButton, Separator, ComboBox, TextBox and ProgressBar controls.


1 Answers

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.

like image 90
Martin Avatar answered Oct 23 '22 12:10

Martin