在Form1 点击btn1按钮 打开Form2,同时传递一个参数,然后对Form2操作完后,关掉form2,同时返回一个参数,Form1接收后继续处理。
解决方案 »
- WPF如何让ListView的Item获得焦点!!!!!!!
- .NET FileUpload 控件如何上传文件到远端服务器?
- double 数据类型可以表示多少个数值?..答对了给分啊..
- controls中foreach的问题-在线等
- 关于C#里的怎么又继承一个类又实现一个接口
- C#如何通过读取数据库权限来动态生成菜单和按钮
- 想找一个winform 的源程序看看.(最好是OA) 请问谁可以提供?
- 讨论一下,C#可以用来做什么?
- 如何以变量作为form名字来实例化?
- .net类库里有树和图吗?
- 对于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