C#窗体传值小总
1 通过委托事件 传值
说明: 窗体2的值传值给窗体1
窗体1 代码
private void button1_Click(object sender, EventArgs e) //委托 事件传值
{
searchFrm sf = new searchFrm();
sf.SearchEvent += new test1.searchFrm.SearchDelegate(sf_SearchEvent);
sf.ShowDialog();
}
private void sf_SearchEvent(string r1,string r2)
{
string s = r1;
string q = r2;
//this.dgv.DataSource = getDT(sql);
textBox1.Text = r1;
textBox2.Text = r2;
}
窗体2 代码
public string m_dRetrieveData = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
public string m_dRetrieveDataEnd = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
public delegate void SearchDelegate(string str,string str1);
public event SearchDelegate SearchEvent;
private void button1_Click(object sender, EventArgs e)
{
DateTime t1 = DateTime.Parse("00:00:00");
DateTime t2 = DateTime.Parse("23:59:59");
m_dRetrieveData = dateTimePicker_end1.Value.ToString("yyyy-MM-dd") + t1.ToString(" HH:mm:ss");
m_dRetrieveDataEnd = dateTimePicker_end1.Value.ToString("yyy-MM-dd") + t2.ToString(" HH:mm:ss");
SearchEvent(m_dRetrieveData, m_dRetrieveDataEnd);
this.Close();
}
2 通过属性传值
说明 :窗体1 的值传给窗体2
窗体1 代码
private void button1_Click(object sender, EventArgs e) // 通过属性传值
{
Form2 f = new Form2();
f.SeekvideoFormValue = textBox1.Text;
f.ShowDialog();
}
窗体2 代码
public string SeekvideoFormValue
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text = value;
}
}
3 要求效果显示关闭窗体1然后传值窗体2
窗体1 代码
private void button1_Click(object sender, EventArgs e) // 项目中要求显示效果关闭窗体1 然后传值到窗体2
{
Form2 F2 = new Form2();
F2.S1 = textBox1.Text;
#region 方法1
this.Close();
new System.Threading.Thread((System.Threading.ThreadStart)delegate
{ Application.Run(F2);
}).Start();
#endregion
#region 方法2
//this.Hide();
//F2.ShowDialog();
#endregion
}
窗体2 代码
public string S1
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}