2

I come to ask because i'm new using windows forms and i need some help.

I have a Windows Forms project with 2 forms. The MainForm and the InnerForm.

I'm trying to access to a TableLayoutPanel in MainForm from InnerForm to add new rows in this tablelayoutpanel with some actions that happen in the InnerForm.

I have the next code:

MainForm:

 public partial class MainForm : Form
 {
    public MainForm()
    {
        InitializeComponent();

        TableLayoutPanel panel = tableLayoutPanel1;

        panel.ColumnCount = 4;
        panel.RowCount = 1;
        panel.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
        panel.Controls.Add(new Label() { Text = "Tag/ID" }, 0, 0);
        panel.Controls.Add(new Label() { Text = "Tipo" }, 1, 0);
        panel.Controls.Add(new Label() { Text = "Acción" }, 2, 0);
        panel.Controls.Add(new Label() { Text = "Ejecutar" }, 3, 0);
    }

    private void AddInnerForm(string url)
    {
        var inner = new InnerForm(url)
        // more code
    }

    public void agregarRow(ConsoleMessageEvents args){
        // some action with tableLayoutPanel1
    }
 }

InnerForm:

 public partial class InnerForm : UserControl
 {
    MainForm theMain;

    public InnerForm(MainForm main)
    {
        theMain = main;
    }

    public InnerForm(string url)
    {
        InitializeComponent();
        // more code
    }

    private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
    {
       theMain.agregaRow(args);
    }
 }

but when I'm debugging the program I get this error:

An unhandled exception of type 'System.NullReferenceException' occurred in Project.exe

Additional information: Object reference not set to an instance of an object.

in this line in InnerForm:

private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
{
   **theMain.agregaRow(args)**;
}

What is the problem here?

2

2 Answers 2

2

That should fix it. The field theMain was not initialized, also you need to call InitializeComponenets in every constructos to create the child controls.

   public partial class InnerForm : UserControl
 {
    MainForm theMain;

    public InnerForm(MainForm main)
    {
        InitializeComponent();
        theMain = main;
    }

    public InnerForm(string url, MainForm mainForm)
    {
        this.theMain = mainForm;
        InitializeComponent();
        // more code
    }

    private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
    {
       theMain.agregaRow(args);
    }
 }

And in MainForm:

private void AddInnerForm(string url)
    {
        var inner = new InnerForm(url, this)
        // more code
    }
Sign up to request clarification or add additional context in comments.

Comments

1

From your code it easy to see what you're initializing theMain variable in this constructor:

public InnerForm(MainForm main)
{
    theMain = main;
}

But in your code var inner = new InnerForm(url) you're using another constructor which is not initializing this variable - so theMain is left null and you're getting that exception when trying to access its method.

public InnerForm(string url)
{
    InitializeComponent();
    // more code
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.