主窗体: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace StudyWinform { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 f = new Form2(textBox1.Text); f.Owner = this; f.Show(); } private void Form1_Load(object sender, EventArgs e) { } public void SetData(string a) { textBox1.Text = a; } } } 子窗体: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace StudyWinform { public partial class Form2 : Form { public Form2(string data) { InitializeComponent(); this.Data = data; } public string Data { get; set; } private void button1_Click(object sender, EventArgs e) { Form1 f = this.Owner as Form1; f.SetData(textBox1.Text); this.Close(); } private void Form2_Load(object sender, EventArgs e) { textBox1.Text = Data; } private void Form2_FormClosing(object sender, FormClosingEventArgs e) { Form1 f = this.Owner as Form1; f.SetData(textBox1.Text); } } } 两个窗体,里面一个textBox,一个button。
2、构造传递,Form2关闭委托传回
3、公开属性
2.自己写个showfrom()方法,里面调用ShowDialog方法,在写个return 语句
http://blog.sina.com.cn/s/blog_60d69ce00100eldt.html
主窗体:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace StudyWinform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(textBox1.Text);
f.Owner = this;
f.Show();
} private void Form1_Load(object sender, EventArgs e)
{ } public void SetData(string a)
{
textBox1.Text = a;
}
}
}
子窗体:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace StudyWinform
{
public partial class Form2 : Form
{
public Form2(string data)
{
InitializeComponent();
this.Data = data;
} public string Data { get; set; } private void button1_Click(object sender, EventArgs e)
{
Form1 f = this.Owner as Form1;
f.SetData(textBox1.Text);
this.Close();
} private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = Data;
} private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Form1 f = this.Owner as Form1;
f.SetData(textBox1.Text);
}
}
}
两个窗体,里面一个textBox,一个button。