tags:

views:

71

answers:

6

I am drawing a rectangle on my form and on top of that i have a label that i dragged. Now i want to resize the rectangle that contains label so that it changes automatically when the size of the form is changed(maximized and all). I tried using this.label1.Size = new Size(); but this causes my label or may be rectangle to be disappeared from the form. I cannot see it in my form now. Anchor and Dock properties do not serve my purpose. I have to hard code it. Need help on how to solve this resizing problem.

A: 

Use the .Anchor property.

danbystrom
I cannot. It doesnot serve my needs. I have to hardcoded it.
ghd
A: 

Check out the Anchor and Dock properties. They should be able to provide you with the functionality that you need.

You could also look in to using the .SetBounds() method in the resize event.

Barry
A: 

You can either use Dock property of the control which allows you to make it align with the left, right, top or bottom edge of the form or fill the entire form.

Or use Anchor property, which allows you to anchor the coordinates - e.g. when you set anchor to left, right, top, bottom, it will resize with the form.

František Žiačik
I cannot use it. I have to hardcode my code according to my needs. Anchor and Dock do not serve the purpose
ghd
A: 

If Dock and Anchor are no good then just set the Width and Height properties. By creating a default Size object and assigning that to your control you are basically assigning Width and Height to 0.

Dan Iveson
A: 

You should handle the SizeChanged event:

this.SizeChanged += new EventHandler(Form1_SizeChanged);

then in the envent handler method, you must arrange Size and Location of your control:

void Form1_SizeChanged(object sender, EventArgs e)
{
   myControl.Size = new Size(w,h);  // size of the control
   myControl.Location = new Point(x,y);  //coordinates from the upperleft corner of your control's container (the form in your case)
}

N.B. AFAIK a label cannot be resized as you want (instead location is ok), because has a fixed size. Use textbox with Readonly and Multiline properties set to true

digEmAll
or just set `.AutoSize = false;` on the label...
Pondidum
@Pondidum: thanks I didn't know that trick :)
digEmAll
A: 

Try form Scale() method. But for me, it has a little strange behavior...

serhio