WinForm中TabControl的一些事件写法(C#)

在TabControl控件中并没提供单个选项卡的Click事件,今天下午翻了翻MSDN 结果还是没找到相关的文档:( 看来只有自个儿折腾了...
还好有个SelectedIndexChanged事件为我所用:)

以下是代码片段:
private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   switch(this.tabControl1.SelectedIndex)
   {
    case 0:
       MessageBox.Show("tabPage1 is Selected");
     break;
    case 1:
       MessageBox.Show("tabPage2 is Selected");
     break;
   }
}

 

 

 

在 WinForm开 发的过程中,MDI是一种常见的形式。在MDI主窗体打开的子窗体处于活动状态的只有一个,很多时候我们需要在打开的MDI子窗 体中切换。然而.Net的框架并没有提供一种易见的切换方式,当然可以通过“Ctrl+Tab”来切换,但是这种方式是不易见的,并且只能按照打开窗体的 顺序来切换。

      下面来介绍一种结合了TabCotrol控件的软件设计形式。

       

       这种设计形式要解决三个问题:

      (1)TabControl的TabPage要和窗体对应

      (2)TabControl的TabPage切换的时候,对应的窗体要激活

      (3)窗体的新建或关闭,对应的TabPage要创建或撤销

      下面就来一步步的解决这些问题

      首先拖一个TabControl控件到MDI主窗口中,设置其DOCK为Bottom或者TOP,根据需要也可以设置到左或右。设置其Height为0。主窗体添加如下代码:

       //切换TabPage,将对应的窗体激活
        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length > 0)
            {
                for (int i = 0; i < this.MdiChildren.Length; i++)
                {
                    if (this.tabControl.SelectedIndex == i)
                    {
                        this.MdiChildren[i].Activate();
                        return;
                    }
                }
            }
        }

 

or

  //切换TabPage,将对应的窗体激活
        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ChangeTabPage();
        }

        //子窗体切换激活
        private void ChangeTabPage()
        {
            if (this.MdiChildren.Length > 0 && tabControl.SelectedIndex > -1)
            {
                for (int i = 0; i < this.MdiChildren.Length; i++)
                {
                    if (this.tabControl.SelectedIndex == i)
                    {
                        this.MdiChildren[i].WindowState = FormWindowState.Maximized;
                        this.MdiChildren[i].Visible = true;
                        this.MdiChildren[i].Activate();

                    }
                    else if (this.MdiChildren[i].Visible == true)
                    {
                        this.MdiChildren[i].Visible = false;
                    }
                }
            }

        }
        //增加TabPage,将对应的窗体激活
        private void tabControl_ControlAdded(object sender, ControlEventArgs e)
        {
            if (this.tabControl.SelectedIndex == 0)
            this.ChangeTabPage();
        }

        //关闭TabPage,将对应的窗体关闭
        private void tabControl_ControlRemoved(object sender, ControlEventArgs e)
        {
            //if (this.tabControl.SelectedIndex == 0)
                //this.ChangeTabPage();
        }

       //窗体关闭的时候,撤销对应的TabPage
        public void RemoveTabPage(TabPage tb)
        {
            this.MultiPageControl.TabPages.Remove(tb);
            if (this.tabControl.TabPages.Count == 0)
            {
                this.TabControlSize = new Size(this.TabControlSize.Width, 0);
            }
        }

        //窗体创建的时候,创建对应的TabPage
        public void AddTabPage(TabPage tb)
        {
            if (this.tabControl.TabPages.Count == 0)
            {
                this.TabControlSize = new Size(this.TabControlSize.Width, 20);
            }
            this.MultiPageControl.TabPages.Add(tb);
            this.MultiPageControl.SelectedTab = tb;
        }

         //获取TabControl控件,以便子窗体调用(只读)
        public TabControl MultiPageControl
        {
            get
            {
                return this.tabControl;
            }
        }

        //获取TabControl控件的Size属性,以便子窗体调用

        public Size TabControlSize
        {
            get
            {
                return this.tabControl.Size;
            }
            set
            {
                this.tabControl.Size = value;
            }
        }

       对应子窗体,每个窗体打开的时候,主窗体都需要创建一个TabPage,关闭的时候,都要撤销一个TabPage,也就是说所有的子窗体都有共同的属性和功能,所以我设置了一个父窗体来实现这些功能,子窗体通过继承而拥有这些功能。

        private frmMain frMain = null;
        private TabPage tabPage = new TabPage();

        private void frmTabMain_ParentChanged(object sender, EventArgs e)
        {
            if (this.MdiParent != null)
            {

               this.tabPage.Text = this.Text + "  ";
                frMain = this.MdiParent as frmMain;//获取主窗体
                frMain.AddTabPage(tabPage);//调用主窗体方法,创建一个TabPage
            }
        }

        //窗体被激活,对应的TabPage也呈选中状态

        private void frmTabMain_Enter(object sender, EventArgs e)
        {
            if (this.frMain != null)
            {
                for (int i = 0; i < this.frMain.MdiChildren.Length; i++)
                {
                    if (this.frMain.MdiChildren[i] == this)
                    {
                        this.frMain.MultiPageControl.SelectedIndex = i;
                    }
                }
            }
        }

        //窗体关闭,调用主窗体方法,撤销对应TabPage
        private void frmTabTitleMain_FormClosed(object sender, FormClosedEventArgs e)
        {


            this.Dispose();

            this.frMain.RemoveTabPage(tabPage);
        }

       //窗体加载的时候,将窗体的标题赋给对应的TabPage

        private void frmTabMain_Load(object sender, EventArgs e)
        {
           // this.tabPage.Text = this.Text;
        }

 

 

--------------------------------------------------------------------------------------------------------------

本页面实现功能为:点击左侧边栏的菜单 页面部分TabControl自动添加所对应的页面
        /// </summary>
        /// <param name="MainTabControlKey">选项卡的键值</param>
        /// <param name="objTabControl">要添加到的TabControl对象</param>
        /// <returns></returns>
        private Boolean ErgodicModiForm(string MainTabControlKey, TabControl objTabControl)
        {
            //遍历选项卡判断是否存在该子窗体
            foreach (Control con in objTabControl.Controls)
            {
                tbPage = (TabPage)con;
                if (tbPage.Name == MainTabControlKey)
                {
                    return false;//存在
                }
            }
            return true;//不存在
        }

        private TabPage tbPage = null;
        /// <summary>
        /// 添加选项卡页面显示窗体信息
        /// </summary>
        /// <param name="id">选项卡id</param>
        /// <param name="name">选项卡名称</param>
        /// <param name="newForm">所要加载的窗体</param>
        private void AddTab(string id, string name, Form newForm)
        {
            if (ErgodicModiForm(id, tbcMain))
            {
                tbPage = new TabPage();
                tbPage.Name = id;
                tbPage.Text = name;
                tbcMain.TabPages.Add(tbPage);
                tbcMain.BackColor = Color.FromName("#3775B2");
                //newForm.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right |

AnchorStyles.Bottom;
                newForm.Dock = DockStyle.Fill;
                newForm.BackColor = ColorTranslator.FromHtml("#e3f1fe"); 
                newForm.FormBorderStyle = FormBorderStyle.None;
                newForm.TopLevel = false;

                //添加右键菜单
                contextMenu = new ContextMenuStrip(this.components);
                //contextMenu.TopLevel = false;
                contextMenu.Items.Add("关闭窗口");
                //this.Controls.Add(contextMenu);
                contextMenu.Items[0].Click += new System.EventHandler(this.contextMenuItem_Click);

                newForm.Show();
                tbPage.Controls.Add(newForm);
                newForm.ContextMenuStrip = contextMenu;  //指定窗体的右键菜单                
            }
            tbcMain.SelectedTabPage = tbPage;
        }

        /// <summary>
        /// 右键菜单单击关闭窗体,关闭当前选项卡页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void contextMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Control i in this.tbcMain.SelectedTab.Controls)
            {
                if (i.GetType() == typeof(Form))
                {
                    Form form = (Form)i;
                    form.Close();
                }
            }
            this.tbcMain.TabPages.RemoveAt(this.tbcMain.SelectedIndex);
        }

        /// <summary>
        /// 点击操作员管理菜单,加载操作员管理页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiOperManage_Click(object sender, EventArgs e)
        {
            User_Mange userMange=new User_Mange();
            AddTab("OperManage","操作员管理",userMange);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值