resizing

S

Saurabh

Hi All,

I have in my windows form, a set of controls which is Label, Edit, Label and
another edit. These are lined up horizontally. When I resize the form
horizontally, I want both the edit controls to grow by an equal amount.
which means
label 1 stays where it is
edit1 stays where it is but increases in width
label 2 moves horizontally but retains the size
edit 2 moves horizontally and increases in width.

Can this be easily achieved in the designer or do I have to write code for
it ?

TIA,

--Saurabh
 
M

Morten Wennevik

Hi Saurabh,

The anchor property could be used to fix controls. Fixing a control both
left and right will cause it to change size according to the new width.
However, since you want two controls on the same line to be resized they
will eventually overlap, which I doubt you would want.

I fear you will need to handle the resizing manually, but this is easy
enough. If you put each pair of label/textbox in two panels of equal
size. Set the label's anchor to top left, and the textbox to top left
right. Then in the sizechanged event you can do something like this:

private void Form1_SizeChanged(object sender, EventArgs e)
{
panel2.Width = panel2.Left = panel1.Width = this.ClientSize.Width/ 2;
}

This will set each panel to be exactly half the size of the form with
panel2 starting where panel1 ends. The controls inside will adjust
themselves.


Happy coding!
Morten Wennevik [C# MVP]
 
S

Saurabh

Thanks Morten,
that does work and give desired effect. I was thinking on the lines of
having a flow layout and fill the 2 panels across the width or something on
those lines. Your suggestion works and I can now have the flexibility of
changing the panel size with different proportion. Lets say I want to grow
the first set by 40% and the second by 60 percent, I can do that in the
event.

--Saurabh

Hi Saurabh,

The anchor property could be used to fix controls. Fixing a control both
left and right will cause it to change size according to the new width.
However, since you want two controls on the same line to be resized they
will eventually overlap, which I doubt you would want.

I fear you will need to handle the resizing manually, but this is easy
enough. If you put each pair of label/textbox in two panels of equal
size. Set the label's anchor to top left, and the textbox to top left
right. Then in the sizechanged event you can do something like this:

private void Form1_SizeChanged(object sender, EventArgs e)
{
panel2.Width = panel2.Left = panel1.Width = this.ClientSize.Width / 2;
}

This will set each panel to be exactly half the size of the form with
panel2 starting where panel1 ends. The controls inside will adjust
themselves.


Happy coding!
Morten Wennevik [C# MVP]
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi guys,

Just for information. VS.NET 2005 will introduce flow layout control. Till
then anchors are the easiest way to do that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top