C#实现窗体间传递数据 C#实现窗体间传递数据实例
人气:0想了解C#实现窗体间传递数据实例的相关内容吗,在本文为您仔细讲解C#实现窗体间传递数据的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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(); } }
加载全部内容