【原创】利用事件-委托,实现主-子窗体动态显示,不同菜单栏,不同工具栏。(类似SQL Server 2000 企业管理器)


1,创建工程“EnterpriseManagerWindow”。

2,添加主界面“frmMain.cs”(Windows窗体)。

   在“frmMain.cs”中添加控件“menuStrip1”(MenuStrip)。

   设置“frmMain.cs”的属性:IsMdiContainer=True。

   设置“frmMain.cs”的属性:MainMenuStrip=menuStrip1。

   在“menuStrip1”中添加控件“fileToolStripMenuItem”(ToolStripMenuItem)
   在“menuStrip1”中添加控件“actionToolStripMenuItem”(ToolStripMenuItem)
   在“menuStrip1”中添加控件“viewToolStripMenuItem”(ToolStripMenuItem)
   在“menuStrip1”中添加控件“toolsToolStripMenuItem”(ToolStripMenuItem)
   在“menuStrip1”中添加控件“windowToolStripMenuItem”(ToolStripMenuItem)
   在“menuStrip1”中添加控件“helpToolStripMenuItem”(ToolStripMenuItem)

   在“windowToolStripMenuItem”中添加控件“consoleToolStripMenuItem”(ToolStripMenuItem)
   在“windowToolStripMenuItem”中添加控件“designToolStripMenuItem”(ToolStripMenuItem)

3,添加子界面“frmConsole.cs”(Windows窗体)。

   在“frmConsole.cs”中添加控件“toolStrip1”(ToolStrip)。

   在“toolStrip1”中添加控件“forwardToolStripButton”(ToolStripButton)
   在“toolStrip1”中添加控件“backToolStripButton”(ToolStripButton)

   在“frmConsole.cs”中添加委托,事件,触发方法,以及一个FormClosed事件代码。
        public delegate void FormExitEventHandler();
        public event FormExitEventHandler ExitEvent;
        private void OnPost()
        {
            if (this.ExitEvent != null)
            {
               this.ExitEvent();
            }
        }
        private void frmConsole_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.OnPost();
        }

4,添加子界面“frmDesign.cs”(Windows窗体)。

   在“frmDesign.cs”中添加控件“toolStrip1”(ToolStrip)。

   在“toolStrip1”中添加控件“saveToolStripButton”(ToolStripButton)
   在“toolStrip1”中添加控件“attributeToolStripButton”(ToolStripButton)
   在“toolStrip1”中添加控件“setPrimaryKeyToolStripButton”(ToolStripButton)

   在“frmConsole.cs”中添加委托,事件,触发方法,以及一个FormClosed事件代码。
        public delegate void FormExitEventHandler();
        public event FormExitEventHandler ExitEvent;
        private void OnPost()
        {
            if (this.ExitEvent != null)
            {
               this.ExitEvent();
            }
        }
        private void frmDesign_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.OnPost();
        }

5,在主界面“frmMain.cs”中添加2个方法,2个事件。
        private void consoleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmConsole obj = new frmConsole();
            obj.ExitEvent += new frmConsole.FormExitEventHandler(ConsoleExit);           
            obj.MaximizeBox = true;
            obj.MinimizeBox = true;
            obj.ShowInTaskbar = false;
            obj.StartPosition = FormStartPosition.Manual;
            obj.WindowState = FormWindowState.Maximized;
            obj.MdiParent = this;
            obj.Show();
        }

        private void ConsoleExit()
        {
            this.fileToolStripMenuItem.Visible = true;
            this.actionToolStripMenuItem.Visible = true;
            this.viewToolStripMenuItem.Visible = true;
            this.toolsToolStripMenuItem.Visible = true;
            this.windowToolStripMenuItem.Visible = true;
            this.helpToolStripMenuItem.Visible = true;
        }

        private void designToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.fileToolStripMenuItem.Visible = true;
            this.actionToolStripMenuItem.Visible = false;
            this.viewToolStripMenuItem.Visible = false;
            this.toolsToolStripMenuItem.Visible = false;
            this.windowToolStripMenuItem.Visible = true;
            this.helpToolStripMenuItem.Visible = true;

            frmDesign obj = new frmDesign();
            obj.ExitEvent += new frmDesign.FormExitEventHandler(DesignExit);
            obj.MaximizeBox = true;
            obj.MinimizeBox = true;
            obj.ShowInTaskbar = false;
            obj.StartPosition = FormStartPosition.Manual;
            obj.WindowState = FormWindowState.Maximized;
            obj.MdiParent = this;
            obj.Show();
        }

        private void DesignExit()
        {
            this.fileToolStripMenuItem.Visible = true;
            this.actionToolStripMenuItem.Visible = true;
            this.viewToolStripMenuItem.Visible = true;
            this.toolsToolStripMenuItem.Visible = true;
            this.windowToolStripMenuItem.Visible = true;
            this.helpToolStripMenuItem.Visible = true;
        }

6, 附源代码:https://files.cnblogs.com/TaeheeLive/EnterpriseManagerWindow_060920.rar

posted on 2006-09-23 09:45  冷火  阅读(575)  评论(0编辑  收藏  举报