This is a sample code I wrote to add some tab pages with the controls in them at run time. but when I run it , I get a Null Ref exception error.
what part I am doing wrong?
TabPage[] tabPages = new TabPage[2];
CheckBox ck = new CheckBox();
tabPages[0].BackColor = Color.Firebrick;
tabPages[0].Controls.Add(ck);
tabPages[1].BackColor = Color.Firebrick;
tabPages[1].Controls.Add(ck);
tabGuy.SuspendLayout();
tabGuy.TabPages.Add(tabPages[0]);
tabGuy.TabPages.Add(tabPages[1]);
tabGuy.ResumeLayout();
You’re missing
tabPages[0] = new TabPage()andtabPages[1] = new TabPage()before any assignment. Creating an array assigns every of its elements to its default value, that isnullfor any reference type.