Reputation: 6510
I'm trying to get an UserControl (which has a grid on it) on a Windows Form to resize. The below code is what I have in the Form. The behavior I'm getting is that the control is resized when I make it big. But it does not shrink. What am I doing wrong (or) What am I missing?
private void AdjustGrid()
{
ZoomControl.Location = new Point(5, 5);
ZoomControl.Size = new Size(this.Width - 15, this.Height - 75);
}
void zoomform_Resize(object sender, EventArgs e)
{
AdjustGrid();
}
Now the user control has the following code:
//Resize the grid that the UserControl has on it
private void NameValuePropertyBag_Resize(object sender, EventArgs e)
{
grdNameValueProperties.Location = new Point(4,4);
grdNameValueProperties.Size = new Size(this.Width - 8, this.Height - 8);
}
I tried
grdNameValueProperties.Size.Width = this.Width - 8;
grdNameValueProperties.Size.Height = this.Height -8;
It gives me "Cannot modify expression because it is not a variable" error... What am I missing?
Additional Info:
I'm using SetParent() Windows call to move/zoom an UserControl to another Form (ZoomForm).
Anchor doesn't seem to work for controls moved with SetParent()... More precisely, it may be working but I have repainting problems.
I got Anchor/Dock pair to working without repaint issues [I removed the resize event wireup and adjusted Dock to Fill]
The ZoomForm initally has no controls. The Usercontrol is added to the ParentForm dynamically.
Currently, I'm able to make the zoom form bigger with the above code but not smaller.
Upvotes: 6
Views: 18919
Reputation: 11218
grdNameValueProperties.Size.Width = this.Width - 8;
grdNameValueProperties.Size.Height = this.Height - 8;
That code gives the error because Size
is a value type, not a reference type. Reading this may help explain the difference between value types and reference types.
Upvotes: 7
Reputation: 45127
Currently, I'm able to make the zoom form bigger with the above code but not smaller.
Some controls have a MinSize (or similar) property on them. Do you have any of those set such that you can't resize smaller?
Upvotes: 1
Reputation: 564641
For the first portion -
First off, I'd recommend using the Anchor property on UserControl instead of trying to size this yourself. It works very simply, and very reliably, for handling window resizing.
If you want to do this, you should probably look at chaining off this.ClientSize instead of this.Height and this.Width. You're probably setting your control too large, and that's unachoring the panel or other thing you're sitting on, which causes all sorts of issues.
The second part is due to the fact that gridNameValue Properties.Size.Width is a member of a struct.
When you call gridNameValueProperties.Size, you're returning a Size struct, then trying to set the Width on the returned struct (not the original). This is why you need to set the entire Size valuetype in one shot. Creating a new Size() and setting it to gridNameValueProperties.Size is the only way to make that work.
Upvotes: 0
Reputation: 59675
As recursive commented, you should just use the Anchor property.
The error occurse because the Size property exposes a struct and not a reference type. The Size property returns a copy of the size object of the control. Writing to the properties Width and Hight of this copy makes no sense because it is just a temporary copy and not backed by memory anywhere.
Upvotes: 4
Reputation: 204229
You can't directly change the Size.Width
property on a UserControl, because the Size
property is a value type, so changing its width would essentially be overwriting the entire Size
property. Instead, controls in WinForms provide their own Width and Height properties, so this code should work:
grdNameValueProperties.Width = this.Width - 8;
grdNameValueProperties.Height = this.Height - 8;
Having said that, I agree with @recursive's comment that you should probably just use the UserControl's Anchor
property to make it "automatically" resize.
Upvotes: 1