I'm resizing my custom control using the following code:
private void Form1_Resize_1(object sender, EventArgs e)
{
textBox1.Text = this.Width.ToString();
textBox2.Text = (this.Height - 200).ToString();
canvas21.Size = new System.Drawing.Size(this.ClientSize.Width, this.ClientSize.Height - this.Top - 15);
canvas21.Invalidate();
}
I just want the top left corner of the custom control(0, 105) to stay in place, and the control to resize along with the form, but for some reason this does not work. When I resize the control stays in place, but automatically resizes to fill out the rest of the form.
Is there any way to get a list of everything that affects the size of a usercontrol. In order to search for other places the size i set, which i might have overlooked?
To resize a control In Visual Studio, select the control to be resized and drag one of the eight sizing handles.
If you want your control to always resize with the form, you can use the Anchor property to set it to be anchored to one or more sides of the form so that you don't have to have your own resizing code (assuming that the standard resizing functionality you get with this fits your needs).
You have to make sure that the Dock property isn't set though, otherwise it might fill the whole form (or one side of the form depending on the setting).
There's really no reason why setting the height and then anchoring to every side (with docking set to none) shouldn't work.
However, I did notice an error in the logic of your provided code. You have:
canvas21.Size = new System.Drawing.Size(this.ClientSize.Width, this.ClientSize.Height - this.Top - 15);
When it should actually be:
canvas21.Size = new System.Drawing.Size(this.ClientSize.Width, this.ClientSize.Height - this.canvas21.Top - 15);
You were just taking the top of the form, rather than the top of canvas21, which is what you need.
That seems to do exactly what you want, at least from my standpoint. If it doesn't quite work, is it simply not resizing at all for you or is it resizing to the wrong size?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With