C#完成窗體間傳遞數據實例。本站提示廣大學習愛好者:(C#完成窗體間傳遞數據實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成窗體間傳遞數據實例正文
本文以實例胪陳了C#兩個窗體之間傳遞數據的完成辦法,詳細的操作步調以下:
1.樹立兩個窗體,並采取公用變量值傳遞:
public partial class Form1 : Form //父窗體 { public string name=""; public Form1() { InitializeComponent(); } private void newBtn_Click(object sender, EventArgs e) { Form2 form2 =new Form2(); form2.ShowDialog(); if (form2.DialogResult == DialogResult.OK) { textBox1.Text = form2.name; form2.Close(); } } }
public partial class Form2 : Form // 子窗體 { public string name { set { textBox1.Text = value; } get { return textBox1.Text; } } public Form2() { InitializeComponent(); } private void OK_Click(object sender, EventArgs e) { if (textBox1.Text == "") { MessageBox.Show("input!"); return; } DialogResult = DialogResult.OK; Close(); } }
2.應用地址方法傳遞
public partial class Form1 : Form //parent form { public string name=""; public Form1() { InitializeComponent(); } private void newBtn_Click(object sender, EventArgs e) { Form2 form2 =new Form2(); form2.Owner = this;//form2的指針指向form1 form2.ShowDialog(); textBox1.Text = form2.name; form2.Close(); } }
public partial class Form2 : Form //son form { public string name { set { textBox1.Text = value; } get { return textBox1.Text; } } public Form2() { InitializeComponent(); } private void OK_Click(object sender, EventArgs e) { if (textBox1.Text == "") { MessageBox.Show("input!"); return; } Form1 form1 = (Form1)this.Owner;//form2的父窗體指針賦給form1 Close(); } }