windowform 中关掉Form2时如何向form1返回一个参数? 在Form1 点击btn1按钮 打开Form2,同时传递一个参数,然后对Form2操作完后,关掉form2,同时返回一个参数,Form1接收后继续处理。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在Form2中定义一个Delegate,在Form2_Close时FireEvents在Form1中绑定Form2定义的Delegate就可以实现详细可以参见MSDN中关于Delegate的例子:<SDK>v1.1\Samples\Technologies\DelegatesAndEvents Form1的button事件中。Form2 f2=new Form2();f2.canshu=this.XXX;//想传啥过去在Form2中写个属性之类的就可以了DialogResult result =f2.ShowDialog();if (result == DialogResult.Cancel){你要做的操作...f2.dispose();}在Form2 中Close按钮里面写上 this.DialogResult = DialogResult.Cancel; 用消息模式显示窗体DialogResult result =f2.ShowDialog();,就可以实现消息的返回啊! 新建两个窗口Form1.cs------------------------------------------------------------------using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace Delegate{ /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; #region Definiens /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; #endregion #region Constructor public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } #endregion #region Dispose /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #endregion #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(72, 32); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(112, 32); this.button1.TabIndex = 0; this.button1.Text = "Show Form2"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { Form2 frm = new Form2(); frm.OnSelected+=new Delegate.Form2.OnSelectedHandle(frm_OnSelected); frm.Show(); } private void frm_OnSelected(string sInput) { MessageBox.Show("Return Value:"+sInput); } }}Form2.cs-----------------------------------------------------------------------------using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;namespace Delegate{ /// <summary> /// Summary description for Form2. /// </summary> public class Form2 : System.Windows.Forms.Form { #region Delegate #region OnSelected /// <summary> /// 自定义动作的句柄,参数可以自定义 /// 我现在用的string,也可以用别的参数类型 /// </summary> public delegate void OnSelectedHandle(string sInput); public event OnSelectedHandle OnSelected; /// <summary> /// 触发OnSelected动作 /// </summary> protected void FireOnSelected(string s) { if(OnSelected != null) OnSelected(s); } #endregion private System.Windows.Forms.TextBox textBox1; #endregion #region Definiens /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; #endregion #region Constructor public Form2() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } #endregion #region Dispose /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #endregion #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(8, 8); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 0; this.textBox1.Text = "test"; // // Form2 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.textBox1); this.Name = "Form2"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Form2"; this.Closed += new System.EventHandler(this.Form2_Closed); this.ResumeLayout(false); } #endregion private void Form2_Closed(object sender, System.EventArgs e) { //触发动作 FireOnSelected(this.textBox1.Text); } }} 看这里,http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx 急救C#组件定义 gridview搜索后分页的问题 帮菜手解决问题,combobox cmbXsBj 总出现问题 仅有的17分,谁能帮我解决这个小问题,C# Gridview添加合计行问题! 用C#如何操作一些类似.Rsc.Exe.Dll这样格式的文件? 请问为什么在子文件夹里建立子web.config会出现错误呢?我用的是Forms的验证 winform打印gridview数据的问题 刚学C#的菜鸟--lable 如何设置用户控件属性 对于Form怎样使它的任务栏中的右键选项全部变灰,包括关闭选项。 采用UDP是不是只能从指定IP地址的一台主机接收数据?
在Form1中绑定Form2定义的Delegate就可以实现详细可以参见MSDN中关于Delegate的例子:
<SDK>v1.1\Samples\Technologies\DelegatesAndEvents
Form2 f2=new Form2();
f2.canshu=this.XXX;//想传啥过去在Form2中写个属性之类的就可以了
DialogResult result =f2.ShowDialog();
if (result == DialogResult.Cancel)
{
你要做的操作...
f2.dispose();
}
在Form2 中Close按钮里面写上
this.DialogResult = DialogResult.Cancel;
Form1.cs
------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace Delegate
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
#region Definiens
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
#endregion #region Constructor
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
}
#endregion #region Dispose
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(72, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 32);
this.button1.TabIndex = 0;
this.button1.Text = "Show Form2";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
Form2 frm = new Form2();
frm.OnSelected+=new Delegate.Form2.OnSelectedHandle(frm_OnSelected);
frm.Show();
} private void frm_OnSelected(string sInput)
{
MessageBox.Show("Return Value:"+sInput);
}
}
}Form2.cs
-----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace Delegate
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
#region Delegate
#region OnSelected
/// <summary>
/// 自定义动作的句柄,参数可以自定义
/// 我现在用的string,也可以用别的参数类型
/// </summary>
public delegate void OnSelectedHandle(string sInput);
public event OnSelectedHandle OnSelected;
/// <summary>
/// 触发OnSelected动作
/// </summary>
protected void FireOnSelected(string s)
{
if(OnSelected != null)
OnSelected(s);
}
#endregion private System.Windows.Forms.TextBox textBox1;
#endregion #region Definiens
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
#endregion #region Constructor
public Form2()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
}
#endregion #region Dispose
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "test";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.textBox1);
this.Name = "Form2";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form2";
this.Closed += new System.EventHandler(this.Form2_Closed);
this.ResumeLayout(false); }
#endregion private void Form2_Closed(object sender, System.EventArgs e)
{
//触发动作
FireOnSelected(this.textBox1.Text);
}
}
}
http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx