一、父窗体向子窗体传值
父窗体
子窗体
1.重写构造函数传值
在ChildForm中写一个有形参的构造函数,所以一实例化就能够把值赋给实例化的子窗体的控件的text
先在子窗体里写构造函数:
public ChildForm(string str)
{
InitializeComponent();
childText.Text = str; ;
}
在父窗体里写:
private void button1_Click(object sender, EventArgs e)
{
//1.1方法
ChildForm childForm = new ChildForm(mainText.Text);
childForm.ShowDialog();
}
因此会实例化一个有childText.Text值的子窗体
优点:一开始设定好的父窗体的值就可以传给子窗体,可以作为初始化子窗体的值一直为某一特定值的功能,估计也可以用字符串数组写一系列值
缺点:
(1)父窗体不写值打开,子窗体就无值
(2)父窗体写值后打开,子窗体才有值,两个窗体并存无法改变子窗体的值,需关掉子窗体
2.利用公共变量来传递值(此时注意写在父窗体中的公共变量需是静态变量static)
子窗体中写:
private void ChildForm_Load(object sender, EventArgs e)
{
childText.Text = Form1.strValue;//说明静态变量无需实例化此类
}
父窗体中写:
public static string value=string.Empty;<br /> private void button1_Click(object sender, EventArgs e)<br /> {<br /> ChildForm childForm = new ChildForm();<br /> value = mainText.Text;<br /> childForm.ShowDialog();<br /> }
LilTip:
(1)方法体外不可以对控件进行赋值
(2)static静态变量表示程序执行前系统就为其静态分配存储空间的一类值
因此
public static和public的区别:
static:静态修饰符,被static修饰的变量和方法类似于全局变量和全局方法,可以在不创建对象时调用,当然也可以在创建对象之后调用。常见的可以用于工具类的工具方法中等,譬如:Math类中的绝大多数方法都是静态方法,他们扮演了工具方法的作用。
public:声明当前被修饰的对象、方法、变量为公有的。这里的公有指的是可以被公有访问,举个例子:一个类就像是一台电脑,公有的部分就是除去电脑本身之外用户可见的部分,譬如:你知道点击哪里可以登录QQ,摁哪里可以开关机,等等,你可以使用这个类所有的可见的东西都是被声明为public的,公有可见且公有可被访问的。
(3)实例化会初始类里的变量值
所以如果在父窗体中定义public string str来存储Textbox的值的话,在子窗体实例化之后静态变量会为定义的初始值,即无论在父窗体中写了什么,子窗体文本框的值都为abc;
错误示例:
父窗体
public string strValue=string.Empty;
private void button1_Click(object sender, EventArgs e)
{
ChildForm childForm = new ChildForm();
strValue = mainText.Text;
childForm.ShowDialog();
}
子窗体中
private void ChildForm_Load(object sender, EventArgs e)
{
Form1 form = new Form1();
childText.Text = form.strValue;
}
则错误为(主要是因为点击按钮会实例化childForm,因此会重新初始strValue的值为string.Empty;初始化strValue为“abc”同理)
3.通过子窗体public公共变量,即无需是静态的
子窗体
public string childStr;
private void ChildForm_Load(object sender, EventArgs e)
{
//1.3方法在子窗体定义public变量,无需静态变量
childText.Text = childStr;
}
父窗体
private void button1_Click(object sender, EventArgs e)
{
ChildForm childForm = new ChildForm();<br /> childForm.childStr = mainText.Text;<br /> childForm.ShowDialog();<br /> }
二、子窗体向父窗体传值
1.将父窗体作为子窗体的owner
子窗体中写
private void button1_Click(object sender, EventArgs e)
{
//2.1方法设置父窗体是子窗体的owner
Form1 form = (Form1)this.Owner;
form.Controls[“mainText”].Text = childText.Text;
}
父窗体主要实现打开子窗体
private void button1_Click(object sender, EventArgs e)
{
//2.1方法
ChildForm childForm = new ChildForm();
childForm.ShowDialog(this);//保证子窗体childform是父窗体的一个控件,并且showdialog保证只打开一个窗体
}
2.在父窗体中添加方法
子窗体中实例化父窗体,并且制定父窗体是this.owner
父窗体中写入方法,因此子窗体变可以调用。
子窗体写
private void button1_Click(object sender, EventArgs e)
{
//2.2方法
ChildForm childForm = new ChildForm();
childForm.ShowDialog(this);
}
父窗体中写
//2.2方法父窗体中写入函数,子窗体调用
public void setValue(string str)
{
mainText.Text = str;
}
private void button1_Click(object sender, EventArgs e)
{
//2.2方法调用父窗体中的函数
Form1 form = (Form1)this.Owner;
form.setValue(childText.Text);
}
3.父窗体中创建属性
此时仍然要使父窗体是子窗体的owner,子窗体是父窗体的子控件
注意属性使用的时候,属性处于等号左边则用的是set方法,属性位于等号右边利用get方法。
父窗体
private void button1_Click(object sender, EventArgs e)
{
//2.3
ChildForm childForm = new ChildForm();
childForm.ShowDialog(this);
}
//2.3方法父窗体中创建属性,用的时候setTextValue在等号左边利用set,等号右边利用get
public string setTextValue
{
get {return setTextValue; }
set {mainText.Text=value; }
}
子窗体
private void button1_Click(object sender, EventArgs e)
{
////2.1方法设置父窗体是子窗体的owner
//Form1 form = (Form1)this.Owner;
//form.Controls[“mainText”].Text = childText.Text;
////2.2方法调用父窗体中的函数
//Form1 form = (Form1)this.Owner;
//form.setValue(childText.Text);
//2.3
Form1 form = (Form1)this.Owner;
form.setTextValue = childText.Text;
}