如果你想直接在 的范围内引用它myClass
,那么将它声明为静态。
或者您需要一个 的实例Form1
,但这不允许您在类定义范围内声明对它的引用,您需要Form1
在构造函数(或类型初始化程序)中实例化实例,您可以将其分配给myClass
.
namespace Calculator {
public partial class Form1: Form {
public Form1() {
InitializeComponent();
}
class myclass {
TextBox textBox3=Form1.textBox1;
TextBox textBox4;
Form1 form1;
public myclass() {
form1=new Form1();
textBox4=form1.textBox2;
}
}
static public System.Windows.Forms.TextBox textBox1;
public System.Windows.Forms.TextBox textBox2;
}
}
更新:
对于 的当前实例Form1
,以下是实例化时传递当前实例的各种方法之一myclass
namespace Calculator {
public partial class Form1: Form {
public Form1() {
InitializeComponent();
textBox1=textBox2; // demonstration for "static make sense"
var x=new Form1.myclass {
form1=this
};
// now x.textBox3 is reference to textBox2
}
public class myclass {
TextBox textBox3=Form1.textBox1;
TextBox textBox4;
internal Form1 form1;
public myclass() {
// form1=new Form1();
textBox4=form1.textBox2;
}
}
static public System.Windows.Forms.TextBox textBox1;
public System.Windows.Forms.TextBox textBox2;
}
}