Click here to Skip to main content
16,004,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two buttons, each of them will display a tab page with an user control...

all are working ok.. but when i click a button the tab page is displaying and if i press that button again another tab page is displaying... like wise so many,.. how many times we click the button... that many times the tab pages are displaying....
i want to control the occurrence of many tab pages..... i want to display the same tab that already displayed when ever i click the button ...
the code i written is


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ttr
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            tabControl1.Visible = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            var contentControl = new tta();
            contentControl.Dock = DockStyle.Fill;

            var page = new TabPage("Languages");
            page.Controls.Add(contentControl);
            //tabControl1.TabPages.Clear();
            tabControl1.TabPages.Add(page);
            tabControl1.SelectedTab = page;
            button1.Visible = true;
            button2.Visible = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            var contentControl = new hhh();
            contentControl.Dock = DockStyle.Fill;

            var page = new TabPage("Education");
            page.Controls.Add(contentControl);
            tabControl1.SelectedTab = page;
            //tabControl1.TabPages.Clear();
            tabControl1.TabPages.Add(page);

            button1.Visible = true;
            button2.Visible = true;




        }
    }
}
Posted

if you want to add one tabpage unless you press many times the buttons.

1. before adding to TabPage to control "tabCobtrol1" check if it's already exist by iterating "tabControl1.TabPages".
 
Share this answer
 
C#
//Declare at class level.
TabPage languagesPage = null;
TabPage educationPage = null;

//Put this check before initializing the object. Do the same for other tab too.
if(educationPage == null)
{
    educationPage  = new TabPage("Education");
    page.Controls.Add(contentControl);
    tabControl1.TabPages.Add(page);
}
tabControl1.SelectedTab = educationPage  ;
 
Share this answer
 
Comments
chaitanya556 11-Jul-14 5:36am    
it was some what got ok, but when ever i click another button, and again this button the tap page is disappearing
CHill60 11-Jul-14 9:53am    
What's the other button doing?

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