C # Tabconctrol Toggle Events

Source: Internet
Author: User

Tabconctrol No Click event, the SelectedIndexChanged event is called when the page is toggled;

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
}
}

The following excerpt expands:

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

In the process of WinForm development, MDI is a common form. The subform that is open in the MDI main form is active only, and many times we need to switch in an open MDI child form. However. NET Framework does not provide a easy way to switch, of course, can be "ctrl+tab" to switch, but this method is not easy to see, and can only be opened in the order of the form to switch.

The following describes a software design form that combines the Tabcotrol control.

This form of design solves three problems:

(1) The TabPage of TabControl is to correspond with the form

(2) TabControl of the TabPage switch, the corresponding form to activate

(3) New or Closed form, corresponding TabPage to be created or revoked

Here are some steps to solve these problems

Drag a TabControl control to the MDI main window first, set its dock to bottom or top, or set it to either left or right, as needed. Set its height to 0. The main form adds the following code:

Toggle TabPage to activate the corresponding form
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

Toggle TabPage to activate the corresponding form
private void Tabcontrol_selectedindexchanged (object sender, EventArgs e)
{
This. Changetabpage ();
}

Child form Toggle Activation
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;
}
}
}

}
Add TabPage to activate the corresponding form
private void Tabcontrol_controladded (object sender, ControlEventArgs e)
{
if (This.tabControl.SelectedIndex = = 0)
This. Changetabpage ();
}

Close TabPage, close the corresponding form
private void Tabcontrol_controlremoved (object sender, ControlEventArgs e)
{
if (This.tabControl.SelectedIndex = = 0)
This. Changetabpage ();
}

When the form is closed, undo the corresponding 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);
}
}

       //form creation, create a corresponding 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;
       }

Gets the TabControl control so that the subform is called (read-only)
Public TabControl Multipagecontrol
{
Get
{
return This.tabcontrol;
}
}

Gets the size property of the TabControl control so that the child form calls

        public Size tabcontrolsize
         {
            get
             {
                 return this.tabControl.Size;
           }
            Set
             {
                 this.tabControl.Size = value;
           }
       }

The subform, each time the form opens, the main form needs to create a tabpage, when it is closed, to undo a tabpage, that is, all the subforms have common properties and functionality, so I set up a parent form to implement these functions, and the subform has these features through inheritance.

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;//get main form
Frmain.addtabpage (tabPage);//Call the main form method to create a tabPage
}
}

The form is activated and the corresponding TabPage is selected

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

Form close, call main form method, undo corresponding TabPage
private void Frmtabtitlemain_formclosed (object sender, Formclosedeventargs e)
{


This. Dispose ();

This.frMain.RemoveTabPage (TabPage);
}

When the form is loaded, assign the title of the form to the corresponding TabPage

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

C # Tabconctrol Toggle Events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.