Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

c# - Cannot modify expression because it is not a variable

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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 http://www.yoda.arachsys.com/csharp/parameters.html may help explain the difference between value types and reference types.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...