把Form1中的 private System.Windows.Forms.TextBox textBox1;
修改成:
public System.Windows.Forms.TextBox textBox1; 然后在Class1中:
Form1 f1=new Form1();
string st=f1.textBox1.Text; 或这样: public static System.Windows.Forms.TextBox textBox1; //声明为静态的 然后在Class1中:
string st=Form1.textBox1.Text; :)
在你的Class里面写一个属性:public Form1 _form;然后new你的类的时候,把Form1的实例传给它:YourClass obj = new YourClass();
obj.Form1=this;然后在Class里面,用:
_form.textBox1.Text来访问
在 class showstring 前加上:using nameplace_form1;
然后在class showstring 中
Form1 form1=new Form1();
string string=form1.textbox1.text;
第二种方法,也可以,但是如果关闭掉程序重新打开时,程序会自动把public static System.Windows.Forms.TextBox textBox1;改为private System.Windows.Forms.TextBox textBox1; 我也不知道为什么,但还是多谢这位老兄!
1)假如在Form1中实例化ShowString,
timmy3310(Tim)的方法很好。
或者,如果ShowString中用到Form1中很多东西,也可以这样:
private Form1 form1;
public ShowString(Form1 form1)
{
this.form1=form1;
}
在Form1中:
ShowString showString=new ShowString(this);2)如果在ShowString的实例中生成Form1,则参考chNET(有神论者)的方法。
private Form1 frm;
在需要的地方
frm=new Form1();只能出现一次,下次需要的地方直接使用frm1即可。
这样就不会生成多个Form1的实例。