MDI表单VisualStudio错误:引用的对象,不包含组件的定义

问题描述 投票:0回答:1

我是Visual Studio和C#的新手。我正在使用MDI表单。 (Form1 - > parent; Form2 - > Son)

我正在尝试在表单大小更改时调整textBox组件的大小。所以我有下一个活动:

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?)

谢谢你们。

c# visual-studio
1个回答
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

非常感谢你们。

© www.soinside.com 2019 - 2024. All rights reserved.