How to automatically resize toolstripcombobox when form is resized?

Hi!

I have a combo box on a toolstrip - toolstripcombobox - how can I automatically resize the combobox when the form is resized?

Matt

[161 byte] By [MateuszRajca] at [2007-12-25]
# 1
help!
MateuszRajca at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

One way of doing it would be to add an event handler into the Form's Resize event.

At that stage you know that you are resizing the Form so you need to resize the combobox too.

Jero

JeroGrav at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3

Whats the code?

Cant figure it out :-(

Matt

MateuszRajca at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4

This should do it:

protected override void OnResize(EventArgs e)
{
base.OnResize(e);

ToolStripComboBox1.Width = this.Width / 2;
}

Put that in your Form.

Edit:

Sorry I forgot to give a description. When the Form is reasized for any reason, it will call the Resize event which will get into the function above. The code above thensets the width of ToolStripCombobox1 to the half the form's width.

JeroGrav at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5

You can see the combobox resizing when you drag the corner of the form but once you stop, the combobox is back in place?

Matt

MateuszRajca at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6

ah ok. make sure that the AutoSize property on the combobox is false.

JeroGrav at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7

thanks but how can I make it extend on the whole toolstrip.

~Matt

MateuszRajca at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 8

All you need to do is to modify the event I posted earlier on.

protected override void ToolStrip1_Resize(Object Sender, EventArgs e)
{
base.OnResize(e);

ToolStripComboBox1.Width = ToolStripComboBox1.Parent.Width - 10;
}

Note that now the combo box has its width set relative to it's parent which is the toolstrip.

Or you could specify the toolstrip explicitly:

ToolStripComboBox1.Width = ToolStrip1.Width - 10;

Then just wire the event up to the toolstrip resize event

ToolStrip1.Resize += new EventHandler(this.Toolstrip1_Resize);

JeroGrav at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...