Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tabcontrol (tab_control) with 3 tab pages (tb1,tb2,tb3)in the one Windows Form and i have another one form in (check box control)with 3 check box(chk1,chk2,chk3) .if i checked chk1,chk2 i want output tab pages show(tb1,tb2).how to i give the conditions. When I load the form I want to disable to access the tb3 pages.

please Help Me!
Posted
Updated 27-Jun-14 3:59am
v2

1 solution


A TabControl may only display one of its TabPages at a time. A TabControl can show/hide a TabPage's Text but it cannot show more than one TabPage at a time.



A TabControl contains a collection of TabPages. For that reason, we use Add and Remove to show and hide individual TabPages.



I'm not sure why you are using CheckBoxes to control the display of the individual TabPages. The TabControl recognizes a click on a TabPage tag and brings that TabPage to the front. I think that your use of CheckBoxes makes for an unnecessarily complex UI.



In the designer for the following code:



  • A Form (Form1) was declared
  • A TabControl (tab_control) was added to the Form
  • Into the TabControl's TabPage collection were added the TabPages
    (tb1, tb2, tb3)
  • On each TabPage, an appropriate Label was added (tb1_LAB, tb2_LAB,
    tb3_LAB)
  • Three CheckBoxes (chk1, chk2, chk3) were added to the Form

C#
using System;
using System.Windows.Forms;

namespace TabPageExample
    {

    // *************************************************** class Form1

    public partial class Form1 : Form
        {

        // ******************************************** initialize_tb1

        void initialize_tb1 ( )
            {
                                        // initialize tb1 contents
            tb1_LAB.Text = @"This is TabPage tb1";
            }

        // ******************************************** initialize_tb2

        void initialize_tb2 ( )
            {
                                        // initialize tb2 contents
            tb2_LAB.Text = @"This is TabPage tb2";
            }

        // ******************************************** initialize_tb3

        void initialize_tb3 ( )
            {
                                        // initialize tb3 contents
            tb3_LAB.Text = @"This is TabPage tb3";
            }

        // ******************************************** initialize_GUI

        void initialize_GUI ( )
            {
                                        // hide all of the TabPages
            tab_control.TabPages.Remove ( tb1 );
            tab_control.TabPages.Remove ( tb2 );
            tab_control.TabPages.Remove ( tb3 );
                                        // set the Checked state of 
                                        // check boxes
            chk1.Checked = false;
            chk2.Checked = false;
            chk3.Checked = false;
                                        // initialize TabPage contents
            initialize_tb1 ( );
            initialize_tb2 ( );
            initialize_tb3 ( );
                                        // show all but tb3
            chk1.Checked = true;
            chk2.Checked = true;
            chk3.Checked = false;
            }

        // ***************************************************** Form1

        public Form1 ( )
            {
            InitializeComponent ( );

            initialize_GUI ( );
            }

        // **************************************** chk_CheckedChanged

        void chk_CheckedChanged ( object    sender, 
                                  EventArgs e )
            {
            CheckBox  check_box = ( CheckBox ) sender;
            bool      is_checked = check_box.Checked;
            string    tag = check_box.Tag.ToString ( ).
                                          ToLower ( ).
                                          Trim ( );

            switch ( tag )
                {
                case "chk1":
                    if ( is_checked )
                        {
                        tab_control.TabPages.Add ( tb1 );
                        }
                    else
                        {
                        tab_control.TabPages.Remove ( tb1 );
                        }
                    break;

                case "chk2":
                    if ( is_checked )
                        {
                        tab_control.TabPages.Add ( tb2 );
                        }
                    else
                        {
                        tab_control.TabPages.Remove ( tb2 );
                        }
                    break;

                case "chk3":
                    if ( is_checked )
                        {
                        tab_control.TabPages.Add ( tb3 );
                        }
                    else
                        {
                        tab_control.TabPages.Remove ( tb3 );
                        }
                    break;

                default:
                    throw new ApplicationException (
                        String.Format (
                            "Unrecognized CheckBox tag ({0})",
                            tag ) ); 
                }
            }

        } // class Form1

    } // namespace TabPageExample


The project files for this example may be found at TabPageControl.zip (11.4 KB)[^].


Hope that helps

 
Share this answer
 
Comments
jeevakumar.T 28-Jun-14 8:14am    
Yes It is Correct sir Thanks.But i have another Question.i have 2 forms(form 1,form 2)OK.
form 1 i have 4 check box(ch1,ch2,ch3,ch4) and form 2 i have One Tab control(4 tab pages) .If i checked multiple check box(eg:ch1,ch2) in Form 1.After it will go to Form 2 show Tab control Tab pages(eg:tp1,tp2).how to i get that sir.

Thanks By
Jeeva

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900