1 构造函数传值
(string)
主窗体
private string arrlOut="htht";
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this.arrlOut);
f.ShowDialog();
}
子窗体
private string arrlOut;
public Form2(string arrlIn)
{
InitializeComponent();
arrlOut = arrlIn;
this.label1.Text = arrlOut;
}
(DataSet)
主窗体
private void button1_Click(object sender, EventArgs e)
{
DataSet arrlOut = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("first",typeof(string));
dt.Columns.Add("secend",typeof(string));
DataRow dr = dt.NewRow();
dr["first"] = "asdf";
dr["secend"] = "ghjk";
dt.Rows.Add(dr);
arrlOut.Tables.Add(dt);
Form2 f = new Form2(arrlOut);
f.ShowDialog();
}
子窗体
private DataSet arrlOut;
public Form2(DataSet arrlIn)
{
InitializeComponent();
arrlOut = arrlIn;
this.label1.Text = arrlOut.Tables[0].Rows[0]["first"].ToString();
this.label2.Text = arrlOut.Tables[0].Rows[0]["secend"].ToString();
}
2 属性传值
方法一
主窗体
public static F070702 Temp;
private string flag;//对账标志
private string str;
public string Flag
{
get
{
return flag;
}
set
{
this.flag = value;
str = this.comboBox2.Text.ToString();
str = value;
}
}
public F070702()//构造函数
{
InitializeComponent();
Temp = this;
}
private void button1_Click(object sender, EventArgs e)
{
this.flag = this.comboBox2.Text.ToString();
Form2 f=new Form2();
f.ShowDialog();
}
子窗体
_dr["flag"] = F070702.Temp.flag;
方法二
主窗体
private void button1_Click(object sender, EventArgs e)
{
string arrlOut="hjhkjhjk";
Form2 f = new Form2();
f.arrIn = arrlOut;
f.ShowDialog();
}
子窗体
private string arrlOut;
public string arrIn
{
set
{
this.arrlOut = value;//这里不能用arrlIn而要单独声明一个变量
this.label1.Text = this.arrlOut;
}
get
{
return this.arrlOut;
}
}
3 方法传值
主窗体
private void button1_Click(object sender, EventArgs e)
{
string arrlOut="hjhkjhjk";
Form2 f = new Form2();
f.setArr(arrlOut);
f.ShowDialog();
}
子窗体
private string arrlOut;
public void setArr(string arrlIn)
{
arrlOut = arrlIn;
this.label1.Text = arrlOut;
}
4. 事件委托页面传值
一.利用事件参数传值(页面一传到页面二)
新建一个public类
namespace eventTest
{
public class Class2
{
public delegate void EventUserFilterHandler(object sender,EventUserFilterArgs e);
}
public class EventUserFilterArgs : System.EventArgs
{
public EventUserFilterArgs(string str)
{
this.tempStr = str;
}
private string tempStr = string.Empty;
public string TempStr
{
get
{
return tempStr;
}
}
}
}
页面一(Form3)
public event eventTest.Class2.EventUserFilterHandler Test_Click;
private void button1_Click(object sender, EventArgs e)
{
EventUserFilterArgs args = new EventUserFilterArgs(this.textBox1.Text);
Form4 frm = new Form4();
this.Test_Click -= new eventTest.Class2.EventUserFilterHandler(frm.frm_Test_Click);
this.Test_Click += new eventTest.Class2.EventUserFilterHandler(frm.frm_Test_Click);
this.Test_Click(this, args);
frm.ShowDialog();
}
页面二 (Form4)
public void frm_Test_Click(object sender, EventUserFilterArgs e)
{
this.label1.Text = e.TempStr;
}
二 利用事件委托传值
1.定义公共类
public class Class1{
public delegate void NumberChangedEventHandler(int number);}
public class MyPublisher{
public Class1.NumberChangedEventHandler NumberChanged;}
2.页面一(Form1)
MyPublisher publisher = new MyPublisher();
Form2 subscriber = new Form2();
public Form1(){
InitializeComponent();
publisher.NumberChanged -= new Class1.NumberChangedEventHandler(subscriber.OnNumberChanged);
publisher.NumberChanged += new Class1.NumberChangedEventHandler(subscriber.OnNumberChanged);
}
private void button1_Click(object sender, EventArgs e)
{
publisher.NumberChanged(3);
subscriber.ShowDialog();
}
3.页面二(Form2)
public void OnNumberChanged(int number)
{
this.label1.Text = number.ToString();
}
三 利用委托回调(不带参)
1.公共类
public class Class1
{
public delegate void SendBackMessage();
}
2.页面一
private void button1_Click(object sender, EventArgs e)
{
Class1.SendBackMessage send = new Class1.SendBackMessage(WriteLable);
Form2 frm2 = new Form2();
frm2.send = send;
frm2.ShowDialog();
}
private void WriteLable()
{
this.label1.Text = "你好";
}
3.页面二(Form2)
public Class1.SendBackMessage send;
private void button1_Click(object sender, EventArgs e)
{
if (send != null)
{
send();
}
this.Close();
}
四 利用委托回调传值(带参)
1.公共类
public class Class1
{
public delegate DataTable SendDT(string id);
}
2.页面一(Form1)
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
dt.Columns.Add("id", typeof(Int32));
DataRow dr = dt.NewRow();
dr["id"] = 33;
dt.Rows.Add(dr);
this.dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
Class1.SendDT sendDt = new Class1.SendDT(this.WriteDT);
Form2 frm2 = new Form2();
frm2.send = sendDt;
frm2.ShowDialog();
}
private DataTable WriteDT(string id)
{
DataRow dr = dt.NewRow();
dr["id"] = id.ToString();
dt.Rows.Add(dr);
this.label1.Text = id.ToString();
this.dataGridView1.DataSource = dt;
return dt;
}
3.页面二(Form2)
public Class1.SendDT send;
private void button1_Click(object sender, EventArgs e)
{
if (send != null)
{
send("4");
}
this.Close();
}