我在form1 里点击button1 ,然后是form2.show,我想在关掉form2的同时也将form1也关掉,应该怎么做,谢谢,急
解决方案 »
- AutoResetEvent表现异常时如何检测?
- 急急急!找不到指定的模块。 (异常来自 HRESULT:0x8007007E)怎么解决,
- C# 编解码
- Visual C#上传文件,总是提示404错误
- 请教,TCPCLIENT与服务器通信,如做聊天程序,是每次发送消息都连接服务器还是一次连接多次发送
- 帮我看看啊,拜托。。。
- 在window form下如何更新DataGrid控件内容
- 本人是新手,想请问各位大哥一个简单的问题!
- 小第做了个方法,是访问DATAREADER的,但是总是出错,同样方法访问DATASET则可以,郁闷呀同,急急。。。
- Visual Studio.net 各个版本号码参考
- 最难的.NET程序员面试问题
- tall[i-1] 的 [i-1]是什么意思?
应该是this.Hide;
给你个例子
主窗体代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace 委托传消息
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.TextBox textBox1;
private Form2 myForm = new Form2 ( ) ; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
this.myForm.Send += new Form2.SendMess( Send );
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(80, 72);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 64);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(128, 40);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Send ( string str )
{
textBox1.Text = str ;
MessageBox.Show("textBox1.Text = " + textBox1.Text);
//把接收来的字符串通过TextBox组件显示出来
//this.button2.PerformClick();
} private void button1_Click(object sender, System.EventArgs e)
{
myForm.ShowDialog ( ) ;
//显示从窗体
}
}
}从窗体代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace 委托传消息
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(30, 62);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(163, 23);
this.label1.TabIndex = 0;
this.label1.Text = "传递到主窗体的字符串:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(37, 94);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(187, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(99, 155);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(85, 41);
this.button1.TabIndex = 2;
this.button1.Text = "传递";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false); }
#endregion public delegate void SendMess( string str );
//定义委托类型
public event SendMess Send;
//定义一个事件类型 private void button1_Click(object sender, System.EventArgs e)
{
//MessageBox.Show( textBox1.Text );
Send( textBox1.Text ); }
}
}
class Form1 : ...
{
private void btn1_click(...)
{
Form2 form = new Form2(this);
form.ShowDialog();
}
}class Form2 : ...
{
private Form1 theForm = null;
public Form2(Form1 theForm)
{
this.theForm = theForm;
}
private void btn2_click(...)
{
theForm.Close();
Close();
}
}