0

I want to add new tabpage from other form in parent form.

My parent form is MainWindow and this form has TabControl. I have child form ChildForm when i click on child form button i want to add new tabpage in TabControl from MainWindow.

I try to create constructor dependency in ChildForm

private MainWindow mainWindow;   

public List(MainWindow form)
{
   this.mainWindow = form;
}

private void createButton_Click(object sender, EventArgs e)
{
    TabPage tabPage = new TabPage("ASD");

    mainWindow.MainTabControl.TabPages.Add(tabPage);
}

This will throw System.NullReferenceException!

I also try to create in MainWindow accessors witch will return mainTabControl access in MainWindow but also not work.

public static TabControl MainTabControl
{
   get {
        MainWindow self = new MainWindow();

        return self.mainTabControl;
    }
}

This not work becouse i create new reference and that is problem.

I try 2 examples and both not work and i know whay not work!!!

Anyone know any other opetion how to slove this problem ?

Ivan
  • 5,139
  • 11
  • 53
  • 86
  • Your first example, while it is not a best practice, should work. Can you use the debugger to find out what is null in that expression? _mainWindow.MainTabControl.TabPages.Add(tabPage);_ – Steve Dec 15 '16 at 16:06
  • `null` is becouse i never create instance of `MainWindow` class. – Ivan Dec 15 '16 at 16:11
  • The default `Modifiers` value for Controls is **Private**, which means you can't access the TabControl from the other Form. The exception is almost certainly because of one of your attempts to make it Public via a Property. You could simply change the Modifiers value of the TabControl to Public or Internal, then use it's actual name, like: `mainWindow.tabControl1.TabPages.Add(tabPage);` – Idle_Mind Dec 15 '16 at 16:13
  • `Idle_Mind` problem is null reference. i can create it pulblic and that again will not work becouse is reference null... There is no object – Ivan Dec 15 '16 at 16:19
  • [Interaction between forms — How to change a control of a form from another form?](http://stackoverflow.com/a/38769212/3110834) – Reza Aghaei Dec 15 '16 at 17:34
  • If you're still getting a null reference after changing the Modifiers property of the TabControl to Public, and changing to the Name (to "tabControl1" for instance)...then likely you are not passing the **Reference to the Main Form** correctly into the Child Form. Make sure to get rid of all the "static references" you have made as well. – Idle_Mind Dec 15 '16 at 18:09

1 Answers1

0

A better approach is leaving the task to create new tabpages to the MainWindow and do not let the child forms know anything of the internal details of the MainWindow. The child forms exposes an event and they will raise it when they want to notify their parent that it is time to create a new tabpage (or whatever the MainWindow wants to do). The MainWindow subscribes to this event and start the creation of the new tab page when requested to do so....

public class ListForm: Form
{
    public delegate void OnNewTabPage(string key);
    public event OnNewTabPage NewTabPage;

    public ListForm()
    {
       .....
    }

    private void createButton_Click(object sender, EventArgs e)
    {
        // Here we pass to the subscriber of the event just a string as 
        // the delegate requires, but, of course, you could change this 
        // to whatever data you wish to pass to the mainwindow 
        // Event a reference to an instance of a class with many fields...
        NewTabPage?.Invoke("ASD");
    }
}

Code in main form

ListForm f = new ListForm();
f.NewTab += CreateTabPage;
f.Show();

private void CreateTabPage(string key)
{
    TabPage page = new TabPage(key);
    this.TabControl.TabPages.Add(page);
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • You dont understand me! `MainWIndow` just need to hold`TabControl` instance. From other forms i want to add tabPages inside MainWindow in existing tabcontro. Whit this example for every new tab i need to write new code inside `MainWindow` and inform `MainWindow` for that new form – Ivan Dec 15 '16 at 16:23
  • Let me understand then. When you talk of parent and child in my knowledge it means that your MainWindow creates the ChildForm thus the MainWindow exists (and thus I have commented about your first example) Is this wrong? If it is wrong then who creates the ChildForm? And why you need to change the MainWindow when you create a new TabPage? The same code that you write inside any child form should be placed in the mainwindow and if you create a new child form with different requirements for the tabpage it is just a different place where you write the same code. You are right, I don't understand. – Steve Dec 15 '16 at 16:30
  • Hi, Might you explain why are you using `.Invoke()`? – McNets Dec 15 '16 at 16:31
  • Mr.Steve my idea is to have one mainwindow(parent) and all other forms is children. All children has toolbar buttons. When in child i click on button i want to add new tabpage in parent. I want all forms open in new tab nevermind if that is in child toolbar action. With this example i all time need to write new form object what i want to open. Sorry my english is not good – Ivan Dec 15 '16 at 16:35
  • 1
    @mcNets to raise the event only if there is a subscriber. (_if NewTabPage != null) NewTabPage("ASD");_ – Steve Dec 15 '16 at 16:43