模态窗体(ShowDialog)打开后,后面的主窗体就不能做任何操作了。

本博客要实现非模态窗体(show)实现模态窗体(ShowDialog)的一些效果(主窗体关闭,子窗体也要关闭。子窗体只能打开一个。)

同时,保留非模态窗体的一些特性(主窗体和子窗体都能操作,比如文本的书写等)。

现实的一些用处:

1、比如要在主窗体中写已有的内容,用子窗体显示这些已有的内容(一般信息比较多,无法在主窗体中用某个空间显示完全)以供参考。

2、直接点击子窗体中的信息,在主窗体中显示出来。

等等

代码如下:

FormA----主窗体

FormB----子窗体

view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Text;  
using System.Windows.Forms;  
 
namespace BasicWindowsApplication  
{  
    public partial class FormA : Form  
    {  
        FormB formB = null;  
 
        public FormA()  
        {  
            InitializeComponent();  
        }  
 
        private void button1_Click(object sender, EventArgs e)  
        {  
            if (formB == null)  
            {  
                formB = new FormB(this);//这一步很重要  
                formB.CloseFrm += new EventHandler(frmA_CloseFrm);  
                formB.Show();  
            }  
            else 
            {  
                formB.TopMost = true;  
            }  
              
        }  
 
        /// <summary>  
        /// 窗体关闭事件  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        public void frmA_CloseFrm(object sender, EventArgs e)  
        {  
            formB = null;  
        }   
 
        /// <summary>  
        /// 窗体关闭事件  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        private void FrmA_FormClosed(object sender, FormClosedEventArgs e)  
        {  
            if (formB != null)  
            {  
                formB.Dispose();  
            }  
        }  
    }  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BasicWindowsApplication
{
    public partial class FormA : Form
    {
        FormB formB = null;

        public FormA()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (formB == null)
            {
                formB = new FormB(this);//这一步很重要
                formB.CloseFrm += new EventHandler(frmA_CloseFrm);
                formB.Show();
            }
            else
            {
                formB.TopMost = true;
            }
           
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void frmA_CloseFrm(object sender, EventArgs e)
        {
            formB = null;
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmA_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (formB != null)
            {
                formB.Dispose();
            }
        }
    }
}

view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Text;  
using System.Windows.Forms;  
 
namespace BasicWindowsApplication  
{  
    public partial class FormB : Form  
    {  
        FormA formA;  
        public event EventHandler CloseFrm;  
 
        public FormB(FormA frm)  
        {  
            formA = frm;  
            InitializeComponent();  
            this.FormClosed += new FormClosedEventHandler(FrmB_FormClosed);  
        }  
 
        /// <summary>  
        /// 窗体关闭事件  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        void FrmB_FormClosed(object sender, FormClosedEventArgs e)  
        {  
            if (CloseFrm != null)  
            {  
                CloseFrm(sender, e);  
            }  
        }  
    }  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BasicWindowsApplication
{
    public partial class FormB : Form
    {
        FormA formA;
        public event EventHandler CloseFrm;

        public FormB(FormA frm)
        {
            formA = frm;
            InitializeComponent();
            this.FormClosed += new FormClosedEventHandler(FrmB_FormClosed);
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void FrmB_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (CloseFrm != null)
            {
                CloseFrm(sender, e);
            }
        }
    }
}

MainForm为了显示主窗体(FormA)关闭,子窗体(FormB)也关闭。

view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
public partial class MainForm: Form  
    {  
        public MainForm()  
        {  
            InitializeComponent();  
        }  
 
        private void button1_Click(object sender, EventArgs e)  
        {  
            FormA formA = new FormA();  
            formA.ShowDialog();  
        }  
    } 
public partial class MainForm: Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FormA formA = new FormA();
            formA.ShowDialog();
        }
    }

 

posted on 2010-04-22 17:57  xiao~~  阅读(889)  评论(0编辑  收藏  举报