object reference not set to an instance of an object
-
I suspect this:
SyntaxHighlightingTextBox[] shtb;
it's not initialized. I also wonder why you have arrays ofTabPage
s andSyntaxHighlightingTextBox
es.Okay, i made a silly mistake in the for loop, but here's the fixed version of the for loop code:
for (int i = 0; i < tabNumber; i++) { // Create a new tab tabPages[i] = new TabPage(); tabPages[i].Location = new System.Drawing.Point(4, 22); tabPages[i].Name = "tabPage" + i.ToString(); tabPages[i].Padding = new System.Windows.Forms.Padding(3); tabPages[i].Size = new System.Drawing.Size(403, 386); tabPages[i].TabIndex = i; tabPages[i].Text = "tabPage " + i.ToString(); tabPages[i].UseVisualStyleBackColor = true; // Create a new RichTextBox inside the tab shtb[i] = new SyntaxHighlightingTextBox(); shtb[i].Name = "shtb" + i.ToString(); shtb[i].Location = new Point(0, 0); shtb[i].Dock = DockStyle.Fill; // Add Tab and RichTextBox Control tabPages[i].Controls.Add(shtb[i]); tabControl.Controls.Add(tabPages[i]); // Reset of tabControl tabPages[i].SuspendLayout(); tabPages[i].ResumeLayout(false); int counter = tabControl.Controls.Count; tabControl.SelectedIndex = counter - 1; this.Text = "TabText1 - " + tabPages[i].Text; }
This compiles correctly, yet, when i run it, i get the exception error again... -
If its not getting initialized, should i move the code inside: public Form1() { InitializeComponent(); } I have an array of tabpages and shtbs because, i want to be able to many many instances of tabs with richtextboxes inside them, dynamically on demand.
Note the difference between these two statements:
TabPage[] tabPages **= new TabPage[maxTabs];** SyntaxHighlightingTextBox[] shtb;
-
If its not getting initialized, should i move the code inside: public Form1() { InitializeComponent(); } I have an array of tabpages and shtbs because, i want to be able to many many instances of tabs with richtextboxes inside them, dynamically on demand.
Latheesan wrote:
I have an array of tabpages and shtbs because, i want to be able to many many instances of tabs with richtextboxes inside them, dynamically on demand.
You don't need arrays for that, the TabControl and TabPage have Collections for them.
-
Latheesan wrote:
I have an array of tabpages and shtbs because, i want to be able to many many instances of tabs with richtextboxes inside them, dynamically on demand.
You don't need arrays for that, the TabControl and TabPage have Collections for them.
-
Note the difference between these two statements:
TabPage[] tabPages **= new TabPage[maxTabs];** SyntaxHighlightingTextBox[] shtb;
I saw the difference in the code, however, when i changed the code like this (before when i was doing some trial and error):
TabPage[] tabPages = new TabPage[maxTabs]; SyntaxHighlightingTextBox[] shtb = new SyntaxHighlightingTextBox();
I started to get this error message: Cannot implicitly convert type 'SynHighLib.SyntaxHighlightingTextBox' to 'SynHighLib.SyntaxHighlightingTextBox[]' -
I saw the difference in the code, however, when i changed the code like this (before when i was doing some trial and error):
TabPage[] tabPages = new TabPage[maxTabs]; SyntaxHighlightingTextBox[] shtb = new SyntaxHighlightingTextBox();
I started to get this error message: Cannot implicitly convert type 'SynHighLib.SyntaxHighlightingTextBox' to 'SynHighLib.SyntaxHighlightingTextBox[]'As you should, try
SyntaxHighlightingTextBox[] shtb = new SyntaxHighlightingTextBox**[somevalue]**;
-
As you should, try
SyntaxHighlightingTextBox[] shtb = new SyntaxHighlightingTextBox**[somevalue]**;
-
Oh? i didnt know that, Is there any example on how to use this? I haven't seen any good tutorials on tab pages anywhere yet. I did do a bit research tho.
Here's my version: (I used a RichTextBox because I don't know what that other thing is.)
private void AddTabs ( int tabNumber ) { System.Windows.Forms.TabPage tabPage = null ; System.Windows.Forms.RichTextBox shtb; tabControl.SuspendLayout() ; tabControl.Controls.Clear() ; for (int i = 0; i < tabNumber; i++) { // Create a new tab tabPage = new System.Windows.Forms.TabPage(); tabPage.Location = new System.Drawing.Point(4, 22); tabPage.Name = "tabPage" + i.ToString(); tabPage.Padding = new System.Windows.Forms.Padding(3); tabPage.Size = new System.Drawing.Size(403, 386); tabPage.TabIndex = i; tabPage.Text = "tabPage " + i.ToString(); tabPage.UseVisualStyleBackColor = true; // Create a new RichTextBox inside the tab shtb = new System.Windows.Forms.RichTextBox(); shtb.Name = "shtb" + i.ToString(); shtb.Location = new System.Drawing.Point(0, 0); shtb.Dock = System.Windows.Forms.DockStyle.Fill; // Add Tab and RichTextBox Control tabPage.Controls.Add(shtb); tabControl.Controls.Add(tabPage); // Reset of tabControl tabPage.SuspendLayout(); tabPage.ResumeLayout(false); } this.Text = "TabText1 - " + tabPage.Text; tabControl.SelectedIndex = tabControl.Controls.Count - 1; tabControl.ResumeLayout() ; }
-
Of course it does. Now donate a pint of blood to the Red Cross.
-
I saw the difference in the code, however, when i changed the code like this (before when i was doing some trial and error):
TabPage[] tabPages = new TabPage[maxTabs]; SyntaxHighlightingTextBox[] shtb = new SyntaxHighlightingTextBox();
I started to get this error message: Cannot implicitly convert type 'SynHighLib.SyntaxHighlightingTextBox' to 'SynHighLib.SyntaxHighlightingTextBox[]'I recommend if you're coding blind and trying things to see if they work, you buy a C# book and work through it.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )