Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys,

i have three buttons and three tabpages.
each button displays each tabpage.

like but1, but2, but3.
but1 displays tab1, but diaplays tab2 and but3 displays tab3.

if but1 diaplays tab1 and but2 displays tab2 and but3 displays tab3 by clicking in order from but1,but2 and but3. they are displaying in order. and again if i press but2 it displays(comes to from) tab2 that already displayed. like other 2 buttons does the same...

but

when but2 is clicked first, tab2 is displaying and but1 clicked tab1 displaying and but3 clicks tab3 displayes.. here the order is tab2>tab1>tab3.

as per the rule, if but1 is cliked now, tab1 should be displayed, but2 display tab2 and but3 displays tab3.... but here what is happening is...... if the order is tab2>tab3>tab1. and but1 is pressed tab2 is coming front, but2 is pressed, tab3 is coming front, and but3 is pressed tab1 is coming front... the code i written to do so is..
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
    {
        TabPage languages = null;
        TabPage education = null;
        TabPage job = null;
        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");
            if (languages == null)
            {
                languages = new TabPage("Languages");
                page.Controls.Add(contentControl);

                //tabControl1.TabPages.Clear();
                tabControl1.TabPages.Add(page);
                tabControl1.SelectedTab = page;
            }

            else
            {
                tabControl1.SelectedTab = languages;
                tabControl1.SelectedIndex = 0;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            tabControl1.Visible = true;
            var contentControl = new hhh();    
            contentControl.Dock = DockStyle.Fill;
            var page = new TabPage("Education");
            if (education == null)
            {
                education = new TabPage("Education");
                page.Controls.Add(contentControl);
                tabControl1.SelectedTab = page;
                //tabControl1.TabPages.Clear();
                tabControl1.TabPages.Add(page);
            }
            else 
            {
                tabControl1.SelectedTab = education;
                tabControl1.SelectedIndex = 1;
                
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
           
            tabControl1.Visible = true;
            var contentcontrol = new job();
            contentcontrol.Dock = DockStyle.Fill;
            var page = new TabPage("Job Titles");
            if (job == null)
            {
                job = new TabPage("Job Titles");
                page.Controls.Add(contentcontrol);
                tabControl1.SelectedTab = page;
                tabControl1.TabPages.Add(page);
            }
            else
            {
                tabControl1.SelectedTab = job;
                tabControl1.SelectedIndex = 2;
                
            }
        }
    }
}

please help to display tab pages according to button, but not through the index....
Posted

1 solution

I would rewrite the methods like this:

C#
private void button1_Click(object sender, EventArgs e)
{
    if (languages == null)
    {
        tabControl1.Visible = true;
        var contentControl = new tta();
        contentControl.Dock = DockStyle.Fill;
        languages = new TabPage("Languages");
        languages.Controls.Add(contentControl);

        tabControl1.TabPages.Add(languages);
        tabControl1.SelectedTab = languages;
    }
    else
    {
        tabControl1.SelectedTab = languages;
    }
}
 
Share this answer
 
Comments
chaitanya556 14-Jul-14 2:01am    
it was working fine but, no content is displaying in tabpages....
here if i keep tabcontrol1.TabPages.Add(pages); it was diaplaying content but not coming acording to my desire... if i keep languages instead of page.. it was displaying nothing
chaitanya556 14-Jul-14 2:07am    
ok sorry.. i didnt observed the above code.... thanks a lot..... thank you very much....

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