RalphF
RalphF

Reputation: 441

Can't seem to add button to another tabpage

I have a TabControl with three TabPages. On tabPage2 there is one button. I want to click on tabPage3 and see this button. I've searched around and the code below is susposed to work but when I click on tabPage3 from tabPage2, I don't see the button.

I must be missing something else?

Thanks for any help...

    private void tabPage3_Click(object sender, EventArgs e)
    {
        this.tabPage3.Controls.Add(this.button1);
    }

Upvotes: 0

Views: 101

Answers (2)

Idle_Mind
Idle_Mind

Reputation: 39132

You could use the SelectedIndexChanged() event:

    private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        tabControl1.SelectedTab.Controls.Add(this.button1);
    }

If you only wanted it to move between tabs 2 and 3 specifically:

    private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (tabControl1.SelectedIndex == 1 || tabControl1.SelectedIndex == 2)
        {
            tabControl1.SelectedTab.Controls.Add(this.button1);
        }
    }

As DonBoitnott pointed out, though, it can cause problems depending on how the form is laid out.

Upvotes: 0

DonBoitnott
DonBoitnott

Reputation: 11025

This sort of thing is going to cause you problems. Add a second button, or, if a single button must be visible, place it outside the TabControl altogether. Making controls hop around like that is a bad idea.

Upvotes: 1

Related Questions