天天看點

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切換事件