天天看点

C# tabconctrol切换事件

tabconctrol没有click事件,切换page时,调用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; 

   } 

}

以下摘抄扩展:

http://www.cnblogs.com/wang2650/archive/2011/11/07/2240421.html

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

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

C# tabconctrol切换事件

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

      (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;

        }

C# tabconctrol切换事件