zoukankan      html  css  js  c++  java
  • VS.NET开发小技巧——C/S程序中MDI子窗体控制其父窗体控件

          看到这个题目相信有很多人都会说:“这个还用说,早会了!”
          没错,无可否认有很多人肯定都知道了。然而最近突然有人问我,说他在网上查找了半天不是没有找到合适的文章就是找到的文章不是很清楚。于是我想就此问题的解决发表到我的博客中,让更多的人能够清楚实现方法。
          因为主要是窗体回调问题,所以就不过多阐述了,给出详细的代码应该就可以理解了。

          我们这里假设已经存在了两个窗体分别为:FormFather(父窗体)和FormChild(子窗体),而父窗体中有一个名为Btn_OpenChild的按钮,用来打开子窗体,子窗体中也有个名为Btn_IsTrue按钮和一个TextBox控件。当TextBox控件中输入“True”时,父窗体的Btn_OpenChild可用,并关闭子窗体,当输入其它任何字符或字符串父窗体的Btn_OpenChild都不可用而不关闭子窗体,当然刚启动程序时的父窗体的Btn_OpenChild按钮是可用的。下面是实现的代码:
     1//下面是主窗体的代码
     2using System;
     3using System.Collections.Generic;
     4using System.ComponentModel;
     5using System.Data;
     6using System.Drawing;
     7using System.Text;
     8using System.Windows.Forms;
     9
    10namespace MDIFormDemo
    11{
    12    public partial class FormFather : Form
    13    {
    14        public FormFather()
    15        {
    16            InitializeComponent();
    17        }

    18
    19        private void OpenChild()//在MDI父窗体中显示子窗体
    20        {
    21            foreach (Form f in this.MdiChildren)
    22            {
    23                if ((f) is FormChild)
    24                {
    25                    f.Activate();
    26                    return;
    27                }

    28            }

    29            FormChild frm = new FormChild(this);
    30            frm.MdiParent = this;
    31            frm.Show();
    32        }

    33
    34        private void FormFather_Load(object sender, EventArgs e)
    35        {
    36            OpenChild();//父窗体被打开时,子窗体也同时被打开
    37        }

    38        private void Btn_OpenChild_Click(object sender, EventArgs e)
    39        {
    40            OpenChild();//如果Btn_OpenChild可用,则点击此按钮也能打开子窗体
    41        }

    42}
    主窗体中没有什么特别的,只是注意第29行的代码中的“this”,接合子窗体的代码你就能明白为何要加上这个“this”了(平时只为了打开子窗体时,我们都不会需要在括号中输入“this”)。
    //下面是子窗体的代码 
    1
    using System;
     2using System.Collections.Generic;
     3using System.ComponentModel;
     4using System.Data;
     5using System.Drawing;
     6using System.Text;
     7using System.Windows.Forms;
     8
     9namespace MDIFormDemo
    10{
    11    public partial class FormChild : Form
    12    {
    13        private FormFather MyForm;
    14        public FormChild (FormFather f)
    15        {
    16            InitializeComponent();
    17            MyForm = f;
    18        }

    19
    20        private void FormChild_Load(object sender, EventArgs e)
    21        {
    22
    23        }

    24
    25        private void Btn_IsTrue_Click(object sender, EventArgs e)
    26        {
    27             if (this.textBox1.text == "True")
    28             {
    29                   MyForm.Btn_OpenChild.Enabled = true;
    30                  this.Close();
    31             }

    32             else
    33             {
    34                  MyForm.Btn_OpenChild.Enabled = false;
    35             }

    36        }

    37}
    主窗体的按钮能用暂且不说,你先在子窗体的TextBox控件中输入一个非“True”的字符或字符串,此时你看看主窗体的Btn_OpenChild是否变成灰色的不可用的状态了呢?

    以上代码运行环境为VS.NET2005,本人在此下面运行通过,你不妨建立一个MDIFormDemo工程试验一下。
  • 相关阅读:
    poj1703
    poj 2385
    poj 3169 差分约束
    poj3723 最大权森林
    POJ3255 次短路
    图论算法----最小生成树
    给linux操作系统安装中文环境
    Windows下使用python
    pku3668 Game of Lines
    pku3670 Eating Together
  • 原文地址:https://www.cnblogs.com/lijigang/p/619878.html
Copyright © 2011-2022 走看看