日期:2014-05-19  浏览次数:20902 次

窗体级访问
我在学习C#,做Form程序.   程序先加载Form1,然后加载Form2.   Form1中有textbox1控件,我想在Form2中通过点击按钮,随时能打印出Form1中textbox1的当前值.我想知道C#都是通过什么方法来访问的,有无VB中Form1.textbox.text这样的直接访问方式?请老师教我.

------解决方案--------------------
public class Form1 : Form
{
public Form1()
{
//some initialize code
}

public string TextBoxValue
{
get{return textbox1.Text;}
}
}

public class Form2 : Form
{
private Form1 frm1;

public Form2(Form1 parent)
{
frm1=parent;
}

public void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(frm1.TextBoxValue);
}
}
------解决方案--------------------
form2:
form2:form
{
string TT= " ";
public form2(string tt)
{
this.TT=tt;
}

//下面就可以用TT的值 了,它就是TextBox的内容

}

Form1:form
{
Button_click()
{
form2 frm=new form2(this.textbox1.text);
frm.show();
}