在子窗体中声明一个公开的字段 public myData Data{get{return _Data;}} _Data是你要返回的数据。 设定一下子窗体的DialogResult;父窗体的调用使用 if (childFrom.ShowDialog()==DialogResult.Ok) { //处理childForm.Data }
利用事件,代码如下(在Form2中改变Form1里的TextBox的值): using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms;namespace Event { /// <summary> /// Form2 的摘要说明。 /// </summary> public class Form2 : System.Windows.Forms.Form { //定义一个委托 public delegate void myDelegate(string str); public event myDelegate myEvent;//定义一个事件private System.Windows.Forms.Button btnF2; private System.Windows.Forms.TextBox textBoxF2; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null;public Form2() { /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.btnF2 = new System.Windows.Forms.Button(); this.textBoxF2 = new System.Windows.Forms.TextBox();private void btnF2_Click(object sender, System.EventArgs e) { myEvent(textBoxF2.Text);//调用事件,将参数传给父窗体 } } }using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;namespace Event { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button btnF1; private System.Windows.Forms.TextBox textBoxF1; /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); }private void btnF1_Click(object sender, System.EventArgs e) { Form2 F2 = new Form2(); F2.Show(); F2.myEvent += new Event.Form2.myDelegate(F2_myEvent); }private void F2_myEvent(string str) { textBoxF1.Text = str;//得到子窗体中的数据 } } }
重载子窗体的构造函数 public Form2(Form1 parent) { // // Required for Windows Form Designer support // InitializeComponent();// // TODO: Add any constructor code after InitializeComponent call // }在父窗体中 Form2 frm2 = new Form2(this); frm2.showdialog();然后就可以在子窗体用用parent.XXX来操作父窗体中的数据,也就相当于子窗体向父窗体传值了。另外,还可以用事件或delegate,看看msdn吧,里面有例子的
可以用一个方法来包装 public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { } public DialogResult Show(string input) { textBox1.Text = input; return ShowDialog(); } }public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 f = new Form2(); MessageBox.Show( f.Show("SSSSSSSSS").ToString()); } }这样两个窗体类就是协作关系;
public myData Data{get{return _Data;}}
_Data是你要返回的数据。
设定一下子窗体的DialogResult;父窗体的调用使用
if (childFrom.ShowDialog()==DialogResult.Ok)
{
//处理childForm.Data
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace Event
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
//定义一个委托
public delegate void myDelegate(string str);
public event myDelegate myEvent;//定义一个事件private System.Windows.Forms.Button btnF2;
private System.Windows.Forms.TextBox textBoxF2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;public Form2()
{
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnF2 = new System.Windows.Forms.Button();
this.textBoxF2 = new System.Windows.Forms.TextBox();private void btnF2_Click(object sender, System.EventArgs e)
{
myEvent(textBoxF2.Text);//调用事件,将参数传给父窗体
}
}
}using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace Event
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnF1;
private System.Windows.Forms.TextBox textBoxF1;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}private void btnF1_Click(object sender, System.EventArgs e)
{
Form2 F2 = new Form2();
F2.Show();
F2.myEvent += new Event.Form2.myDelegate(F2_myEvent);
}private void F2_myEvent(string str)
{
textBoxF1.Text = str;//得到子窗体中的数据
}
}
}
public Form2(Form1 parent)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();//
// TODO: Add any constructor code after InitializeComponent call
//
}在父窗体中
Form2 frm2 = new Form2(this);
frm2.showdialog();然后就可以在子窗体用用parent.XXX来操作父窗体中的数据,也就相当于子窗体向父窗体传值了。另外,还可以用事件或delegate,看看msdn吧,里面有例子的
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{ } public DialogResult Show(string input)
{
textBox1.Text = input;
return ShowDialog(); }
}public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
MessageBox.Show( f.Show("SSSSSSSSS").ToString());
}
}这样两个窗体类就是协作关系;
在主窗口MainForm下:
CForm frm = new CForm();
frm.FormBorderStyle = FormBorderStyle.FixedDialog;
frm.StartPosition = FormStartPosition.CenterParent;
frm.ShowDialog(this);在模式对话框CForm中:
使用MainForm.c1.Text="??";出现错误错误
"非静态的字段、方法或属性“P1.MainForm.cli_messagebox”要求对象引用"
并且CForm中使用不了任何MainForm的数据
frm.ShowDialog(this);这句将MainForm对象转形成为了IWin32Window接口对象
另外MainForm.c1.Text="??";的MainForm是不是主窗体名称?来自于哪里?
Mainform调用cForm,cForm又去访问Mainform,这样很容易形成环状引用
例如我点击按钮 弹出一个模式对话框 我可以在这个对话框里调整一些数据
调整完之后我想把刚才输入的数据写到主窗口的一个textbox控件上
也就是 主窗口->弹出窗口->输入数据->弹出窗口.按钮->主窗口.textbox
所有有关空间属性都是public
可能有点乱 呵呵
我这样理解对吧