我是Visual Studio和C#的新手。我正在使用MDI表单。 (Form1 - > parent; Form2 - > Son)
我正在尝试在formSize更改时调整textBoxComponent的大小。所以我有下一个活动:
private void form2_SizeChanged(object sender, EventArgs e)
{
textBox1.Width = this.Size.Width - 16;
textBox1.Height = this.Size.Height - 39;
}
它工作正常,但如果我将textBox1.Width = FORM2.Size.Width - 16;
代替textBox1.Width = this.Size.Width - 16;
,我会收到下一个错误:An object reference is required for the non-static 'Form.Size' field, method or property
。
其他事情提到:我怎么知道哪些是Form2边框? (我发现有16个和39个正在测试,但我确定必须有一些属性)(类似于textBox1.Width = Form2.Size.Width - Form2.Size.WIDTHBORDER
而不是textBox1.Width = Form2.Size.Width - 16
)
此外,当我在父(form1)上时,我想调用textBox1(在form2中声明的组件),如下所示:this.ActiveMdiChild.TEXTBOX1.prop
而不是this.ActiveMdiChild.ActiveControl.prop
(调用他的ID,而不是他的ActiveControl)但是我收到下一个错误:'Form' does not contain a definition for 'textBox1' nor is there any extension method 'textBox1' that accepts a first argument of the 'Form' type (are some using directive or an assembly reference missing?)
谢谢大家。
答案 0 :(得分:0)
正如HansPassant回答我的问题,为了解决我问题的第一部分:
ERROR: "An object reference is required for the non-static 'Form.Size'
field, method or property"
SOLUTION: var obj = (Form2)sender;
ERROR: "Delete form borders"
SOLUTION: 'Use property ClientSize instead of Size'
正如dlatikay回答我的问题,为了解决我问题的第二部分:
ERROR: "'Form' does not contain a definition for 'textBox1' nor is there any
extension method 'textBox1' that accepts a first argument of the 'Form' type
(are some using directive or an assembly reference missing?)"
SOLUTION: ((Form1)this.ActiveMdiChild).textBox1
非常感谢你们。