您的位置:首页 > 编程语言 > C#

VS.NET 2005(C#)的C/S程序中MDI子窗体控制其父窗体

2011-07-04 17:13 330 查看
      我们这里假设已经存在了两个窗体分别为:FormFather(父窗体)和FormChild(子窗体),而父窗体中有一个名为Btn_OpenChild的按钮,用来打开子窗体,子窗体中也有个名为Btn_IsTrue按钮和一个TextBox控件。当TextBox控件中输入“True”时,父窗体的Btn_OpenChild可用,并关闭子窗体,当输入其它任何字符或字符串父窗体的Btn_OpenChild都不可用而不关闭子窗体,当然刚启动程序时的父窗体的Btn_OpenChild按钮是可用的。下面是实现的代码: //下面是主窗体的代码

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace MDIFormDemo

{

public partial class FormFather : Form

{

public FormFather()

{

InitializeComponent();

}

private void OpenChild()//在MDI父窗体中显示子窗体

{

foreach (Form f in this.MdiChildren)

{

if ((f) is FormChild)

{

f.Activate();

return;

}

}

FormChild frm = new FormChild(this);

frm.MdiParent = this;

frm.Show();

}

private void FormFather_Load(object sender, EventArgs e)

{

OpenChild();//父窗体被打开时,子窗体也同时被打开

}

private void Btn_OpenChild_Click(object sender, EventArgs e)

{

OpenChild();//如果Btn_OpenChild可用,则点击此按钮也能打开子窗体

}

}


 
主窗体中没有什么特别的,只是注意第29行的代码中的“this”,接合子窗体的代码你就能明白为何要加上这个“this”了(平时只为了打开子窗体时,我们都不会需要在括号中输入“this”)。
 //下面是子窗体的代码

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace MDIFormDemo

{

public partial class FormChild : Form

{

private FormFather MyForm;

public FormChild (FormFather f)

{

InitializeComponent();

MyForm = f;

}

private void FormChild_Load(object sender, EventArgs e)

{

}

private void Btn_IsTrue_Click(object sender, EventArgs e)

{

if (this.textBox1.text == "True")

{

MyForm.Btn_OpenChild.Enabled = true;

this.Close();

}

else

{

MyForm.Btn_OpenChild.Enabled = false;

}

}

}


 
主窗体的按钮能用暂且不说,你先在子窗体的TextBox控件中输入一个非“True”的字符或字符串,此时你看看主窗体的Btn_OpenChild是否变成灰色的不可用的状态了呢?
以上代码运行环境为VS.NET2005。
 
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# textbox object class system