无法修改表达式,因为它不是变量

Vya*_*ava 6 c# user-controls winforms

我试图在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();
}
Run Code Online (Sandbox Code Playgroud)

现在用户控件具有以下代码:

//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);
}
Run Code Online (Sandbox Code Playgroud)

我试过了

grdNameValueProperties.Size.Width = this.Width - 8;
grdNameValueProperties.Size.Height = this.Height -8;
Run Code Online (Sandbox Code Playgroud)

它给了我"无法修改表达式,因为它不是变量"错误...我错过了什么?

附加信息:

我正在使用SetParent()Windows调用将UserControl移动/缩放到另一个窗体(ZoomForm). Anchor似乎不适用于使用SetParent()移动的控件...更确切地说,它可能正在工作,但我重新绘制了问题.

我让Anchor/Dock对工作没有重新绘制问题[我删除了调整大小事件连线并调整了Dock to Fill]

ZoomForm最初没有控件.Usercontrol动态添加到ParentForm.

目前,我可以使用上面的代码使缩放形式更大,但不能更小.

Bri*_*ink 7

grdNameValueProperties.Size.Width = this.Width - 8;
grdNameValueProperties.Size.Height = this.Height -8;
Run Code Online (Sandbox Code Playgroud)

该代码给出错误,因为Size是值类型,而不是引用类型.阅读http://www.yoda.arachsys.com/csharp/parameters.html可能有助于解释值类型和引用类型之间的区别.


Mat*_*ton 1

您不能直接更改Size.WidthUserControl 上的属性,因为该Size属性是值类型,因此更改其宽度实际上会覆盖整个Size属性。相反,WinForms 中的控件提供自己的 Width 和 Height 属性,因此以下代码应该可以工作:

grdNameValueProperties.Width = this.Width - 8;
grdNameValueProperties.Height = this.Height - 8;
Run Code Online (Sandbox Code Playgroud)

话虽如此,我同意 @recursive 的评论,您可能应该使用 UserControl 的Anchor属性来使其“自动”调整大小。