C# 不能修改表达式,因为它不是变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/749869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot modify expression because it is not a variable
提问by Vyas Bharghava
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?
我正在尝试在 Windows 窗体上获取一个 UserControl(上面有一个网格)来调整大小。下面的代码是我在表单中的代码。我得到的行为是当我把它变大时会调整控件的大小。但它不会缩小。我做错了什么(或)我错过了什么?
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;
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.
我正在使用 SetParent() Windows 调用将 UserControl 移动/缩放到另一个表单 (ZoomForm)。
锚点似乎不适用于使用 SetParent() 移动的控件......更准确地说,它可能有效,但我有重绘问题。
I got Anchor/Dock pair to working without repaint issues [I removed the resize event wireup and adjusted Dock to Fill]
我让 Anchor/Dock 对正常工作而没有重绘问题 [我删除了调整大小事件接线并调整了 Dock 以填充]
The ZoomForm initally has no controls. The Usercontrol is added to the ParentForm dynamically.
ZoomForm 最初没有控件。用户控件被动态添加到 ParentForm。
Currently, I'm able to make the zoom form bigger with the above codebut not smaller.
目前,我可以使用上面的代码使缩放表单更大,但不能更小。
回答by Matt Hamilton
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:
您不能直接更改Size.Width
UserControl 上的属性,因为该Size
属性是值类型,因此更改其宽度实际上会覆盖整个Size
属性。相反,WinForms 中的控件提供了它们自己的 Width 和 Height 属性,所以这段代码应该可以工作:
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.
话虽如此,我同意@recursive 的评论,即您可能应该只使用 UserControl 的Anchor
属性来使其“自动”调整大小。
回答by Brian Ensink
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.htmlmay help explain the difference between value types and reference types.
该代码给出了错误,因为 Size 是值类型,而不是引用类型。阅读这个http://www.yoda.arachsys.com/csharp/parameters.html可能有助于解释值类型和引用类型之间的区别。
回答by Daniel Brückner
As recursive commented, you should just use the Anchor property.
正如递归评论的那样,您应该只使用 Anchor 属性。
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.
发生错误是因为 Size 属性公开了结构而不是引用类型。Size 属性返回控件的大小对象的副本。写入此副本的 Width 和 Hight 属性没有意义,因为它只是一个临时副本,没有任何地方的内存支持。
回答by Reed Copsey
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.
首先,我建议在 UserControl 上使用 Anchor 属性,而不是尝试自己调整大小。它的工作非常简单,非常可靠,用于处理窗口大小调整。
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.
如果你想这样做,你应该考虑链接 this.ClientSize 而不是 this.Height 和 this.Width。您可能将控件设置得过大,这会使面板或您所坐的其他东西脱离锚定,从而导致各种问题。
The second part is due to the fact that gridNameValue Properties.Size.Width is a member of a struct.
第二部分是由于 gridNameValue Properties.Size.Width 是结构的成员。
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.
当您调用 gridNameValueProperties.Size 时,您将返回一个 Size 结构,然后尝试在返回的结构(而不是原始结构)上设置 Width。这就是您需要一次性设置整个 Size 值类型的原因。创建一个新的 Size() 并将其设置为 gridNameValueProperties.Size 是实现此目的的唯一方法。
回答by JP Alioto
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?
某些控件具有 MinSize(或类似)属性。你有任何这样的设置,你不能调整得更小?