user1079805
user1079805

Reputation: 11

c# add tab to tabControl from separate form

Ok, what I am trying to do is I have a form, MainForm, that has a TabControl and another form called newProject that I'm trying to create a new tab on the TabControl of the MainForm with.

This is the code on my newProject Form

void wizard1_FinishButtonClick(object sender, CancelEventArgs e)
{
    MainForm mf = new MainForm();

    createTab();
    this.Hide();
}
void createTab()
{
    MainForm mf = new MainForm();
    string name = textBoxX1.Text;
    SuperTabItem tab = mf.ProjectTabControl.CreateTab(name);
}

and this is the code on my MainForm

public SuperTabControl ProjectTabControl
{
    get
    {
        return projectTabControl;
    }
}
private void newProjectTab_Click(object sender, EventArgs e)
{
    newproject.Show();
}
public void AddTab()
{
    string s = "New File " + NewTab++;

    this.ProjectTabControl.Tabs.Add(new BPSTabItem(s));

    textEditor();
}

I have no clue why it is not working, so any help would be appreciated,

thank you in advanced, Kyle

Upvotes: 1

Views: 789

Answers (2)

Waqas
Waqas

Reputation: 6802

Probably because MainForm you have declared only exists within the scope of createTab() function. Instead of instantiating new object of MainForm pass the the object of MainForm you create in wizard1_FinishButtonClick to createTab() method. Like this:

void wizard1_FinishButtonClick(object sender, CancelEventArgs e)
{
    MainForm mf = new MainForm();

    createTab(mf);
    this.Hide();
}

void createTab(MainForm mf)
{
    string name = textBoxX1.Text;
    SuperTabItem tab = mf.ProjectTabControl.CreateTab(name);
}

Upvotes: 0

Marco
Marco

Reputation: 57573

It's not working because you start everytime a new MainForm

void createTab()
{
    MainForm mf = new MainForm();
}

You could do:

TabControl tc = null;
public newProject(TabControl tc)
{
    this.tc = tc;
}

void createTab()
{
    string name = textBoxX1.Text;
    // Add tab to tc
}

In MainForm you can then use

newProject frm = new newProject(my_tab_control);

I don't understand well your solution with names.
Anyway the idea is that when you create your second form (from main one) you pass it a reference to your tab control; so in your second form you can directly add a new tab to tab control.

Upvotes: 2

Related Questions