0

I am a begginer. I am creating a winform app, in that I have a Mainform(F1) and several differnt forms which are going to display in a panel(p1) of F1, from these one (F2) is open in load event of F1, there is also ToolStripMenuItem_Click to open form (F3) in same panel.

Now I placed a Close button in F3 to close this form and open a new form F1 again. to make my Mainfrom original appearence.

now as I click on close button F3 gets close and error shows 'panel1 was null'

pls help and don't mind my language and writing skills.

p1=panel_left_main (which is in Mainform) F2=Template

<//form3//>

private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
            th = new Thread(opennewform);
            th.SetApartmentState(ApartmentState.STA);
            th.Start();
        }

        private void opennewform()
        {
         
            Template tpt = new Template();
            tpt.TopLevel = false;
            tpt.Dock = DockStyle.Fill;
            tpt.TopMost= true;
            panel_left_main.Controls.Add(tpt);//< here error is comming //
            tpt.Show();


        }
wasim khan
  • 13
  • 2
  • 1
    is panel_left_main null? THe debugger will show you – pm100 Jul 27 '23 at 17:13
  • 1
    Why are you starting a Thread there? Don't do that. Never create Controls in a Thread other than the UI Thread. If the initialization of `Template` requires time, because you have to, e.g., fetch data, the code that retrieves the data can be run in a ThreadPool Thread (but, if possible, use async / await) and return the results. – Jimi Jul 27 '23 at 17:21

0 Answers0