C# 简单问题,调用另一个form(100分)

  • 主题发起人 kevin8093
  • 开始时间
假设一个mdi程序,主窗体类有一个公共变量frmCnt纪录打开的子窗体数量,如何在子窗体
关闭时使主窗体的frmCnt减一??
一个窗体关闭时怎样让另一个窗体知道?既是两个独立的窗体实例怎么相互通信?
 
private void button1_Click(object sender, System.EventArgs e)
{
Form2 fm2 = new Form2();
fm2.ShowDialog();
//或 fm2.Show();
}
然后在form2.cs中添加
public form1 fm2;
试一下吧[:D]
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// 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.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 96);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
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>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Form2.ShowForm(this);
}
}
}
//以上为调用窗体
//一下为被调用窗体
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsApplication1
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private Form1 frm;
/// <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.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 64);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
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.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
public static void ShowForm(Form1 frm1)
{
Form2 f=new Form2();
f.frm=frm1;
f.ShowDialog();
}
private void button1_Click(object sender, System.EventArgs e)
{
frm.Text="dcx";
}
}
}
 
dcx0026的方法我试过了,是有效的。
但好象和Delphi的编程思想已经很不一样了。
在Form1中并没有using Form2,但却可以执行: Form2.ShowForm(this)不报错;
Form2是被调用的,却要有个声明: private Form1 frm;
还有,Form的ShowDialog方法是Private的吗?
为啥还要自己专门写一个public的ShowForm方法?
刚开始学C#,觉得和Delphi并不很类似,甚至可以说很不一样。
 
-----------------------------------------------------------------------
在Form1中并没有using Form2,但却可以执行: Form2.ShowForm(this)不报错;
-----------------------------------------------------------------------
因为Form1,Form2是在同一个命名空间中,C#中using的是命名空间,而不是类
 
顶部