Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. object reference not set to an instance of an object

object reference not set to an instance of an object

Scheduled Pinned Locked Moved C#
graphicstutorial
14 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Latheesan
    wrote on last edited by
    #1

    Hello, Im trying to workout how to dynamically add tab pages with RichTextBox inside them. So far: 1. I have a form with a input field to enter how many tab pages to create 2. A button that calls the method to create tab page when clicked 3. a tabcontrol with no pages on it Here is a demo - http://img525.imageshack.us/img525/4519/untitledxv6.jpg[^] This is the code i have on form1.cs static int maxTabs = 20; TabPage[] tabPages = new TabPage[maxTabs]; SyntaxHighlightingTextBox[] shtb; private void AddTab(int tabNumber) { for (int i = 0; i < tabNumber; i++) { // Create a new tab tabPages[tabNumber] = new TabPage(); tabPages[tabNumber].Location = new System.Drawing.Point(4, 22); tabPages[tabNumber].Name = "tabPage" + tabNumber.ToString(); tabPages[tabNumber].Padding = new System.Windows.Forms.Padding(3); tabPages[tabNumber].Size = new System.Drawing.Size(403, 386); tabPages[tabNumber].TabIndex = tabNumber; tabPages[tabNumber].Text = "tabPage " + tabNumber.ToString(); tabPages[tabNumber].UseVisualStyleBackColor = true; // Create a new RichTextBox inside the tab shtb[tabNumber] = new SyntaxHighlightingTextBox(); shtb[tabNumber].Name = "shtb" + tabNumber.ToString(); shtb[tabNumber].Location = new Point(0, 0); shtb[tabNumber].Dock = DockStyle.Fill; // Add Tab and RichTextBox Control tabPages[tabNumber].Controls.Add(shtb[tabNumber]); tabControl.Controls.Add(tabPages[tabNumber]); // Reset of tabControl tabPages[tabNumber].SuspendLayout(); tabPages[tabNumber].ResumeLayout(false); int counter = tabControl.Controls.Count; tabControl.SelectedIndex = counter - 1; this.Text = "TabText1 - " + tabPages[tabNumber].Text; } } private void AddButton_Click(object sender, EventArgs e) { try { AddTab(int.Parse(tabNumberTextBox.Text)); } catch (Exception ex)

    P 1 Reply Last reply
    0
    • L Latheesan

      Hello, Im trying to workout how to dynamically add tab pages with RichTextBox inside them. So far: 1. I have a form with a input field to enter how many tab pages to create 2. A button that calls the method to create tab page when clicked 3. a tabcontrol with no pages on it Here is a demo - http://img525.imageshack.us/img525/4519/untitledxv6.jpg[^] This is the code i have on form1.cs static int maxTabs = 20; TabPage[] tabPages = new TabPage[maxTabs]; SyntaxHighlightingTextBox[] shtb; private void AddTab(int tabNumber) { for (int i = 0; i < tabNumber; i++) { // Create a new tab tabPages[tabNumber] = new TabPage(); tabPages[tabNumber].Location = new System.Drawing.Point(4, 22); tabPages[tabNumber].Name = "tabPage" + tabNumber.ToString(); tabPages[tabNumber].Padding = new System.Windows.Forms.Padding(3); tabPages[tabNumber].Size = new System.Drawing.Size(403, 386); tabPages[tabNumber].TabIndex = tabNumber; tabPages[tabNumber].Text = "tabPage " + tabNumber.ToString(); tabPages[tabNumber].UseVisualStyleBackColor = true; // Create a new RichTextBox inside the tab shtb[tabNumber] = new SyntaxHighlightingTextBox(); shtb[tabNumber].Name = "shtb" + tabNumber.ToString(); shtb[tabNumber].Location = new Point(0, 0); shtb[tabNumber].Dock = DockStyle.Fill; // Add Tab and RichTextBox Control tabPages[tabNumber].Controls.Add(shtb[tabNumber]); tabControl.Controls.Add(tabPages[tabNumber]); // Reset of tabControl tabPages[tabNumber].SuspendLayout(); tabPages[tabNumber].ResumeLayout(false); int counter = tabControl.Controls.Count; tabControl.SelectedIndex = counter - 1; this.Text = "TabText1 - " + tabPages[tabNumber].Text; } } private void AddButton_Click(object sender, EventArgs e) { try { AddTab(int.Parse(tabNumberTextBox.Text)); } catch (Exception ex)

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      I suspect this: SyntaxHighlightingTextBox[] shtb; it's not initialized. I also wonder why you have arrays of TabPages and SyntaxHighlightingTextBoxes.

      L 3 Replies Last reply
      0
      • P PIEBALDconsult

        I suspect this: SyntaxHighlightingTextBox[] shtb; it's not initialized. I also wonder why you have arrays of TabPages and SyntaxHighlightingTextBoxes.

        L Offline
        L Offline
        Latheesan
        wrote on last edited by
        #3

        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.

        P 2 Replies Last reply
        0
        • P PIEBALDconsult

          I suspect this: SyntaxHighlightingTextBox[] shtb; it's not initialized. I also wonder why you have arrays of TabPages and SyntaxHighlightingTextBoxes.

          L Offline
          L Offline
          Latheesan
          wrote on last edited by
          #4

          I tried entering 0 into the input box and try to add a tab, this time no exception error, but instead, nothing happens. Where im i going wrong? Why wont this work?

          1 Reply Last reply
          0
          • P PIEBALDconsult

            I suspect this: SyntaxHighlightingTextBox[] shtb; it's not initialized. I also wonder why you have arrays of TabPages and SyntaxHighlightingTextBoxes.

            L Offline
            L Offline
            Latheesan
            wrote on last edited by
            #5

            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...

            1 Reply Last reply
            0
            • L Latheesan

              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.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Note the difference between these two statements: TabPage[] tabPages **= new TabPage[maxTabs];** SyntaxHighlightingTextBox[] shtb;

              L 1 Reply Last reply
              0
              • L Latheesan

                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.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                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.

                L 1 Reply Last reply
                0
                • P PIEBALDconsult

                  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.

                  L Offline
                  L Offline
                  Latheesan
                  wrote on last edited by
                  #8

                  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.

                  P 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Note the difference between these two statements: TabPage[] tabPages **= new TabPage[maxTabs];** SyntaxHighlightingTextBox[] shtb;

                    L Offline
                    L Offline
                    Latheesan
                    wrote on last edited by
                    #9

                    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[]'

                    P C 2 Replies Last reply
                    0
                    • L Latheesan

                      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[]'

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      As you should, try SyntaxHighlightingTextBox[] shtb = new SyntaxHighlightingTextBox**[somevalue]**;

                      L 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        As you should, try SyntaxHighlightingTextBox[] shtb = new SyntaxHighlightingTextBox**[somevalue]**;

                        L Offline
                        L Offline
                        Latheesan
                        wrote on last edited by
                        #11

                        OMG... that worked O_O; Thank you very much :)

                        P 1 Reply Last reply
                        0
                        • L Latheesan

                          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.

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #12

                          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() ;
                              }
                          
                          1 Reply Last reply
                          0
                          • L Latheesan

                            OMG... that worked O_O; Thank you very much :)

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #13

                            Of course it does. Now donate a pint of blood to the Red Cross.

                            1 Reply Last reply
                            0
                            • L Latheesan

                              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[]'

                              C Offline
                              C Offline
                              Christian Graus
                              wrote on last edited by
                              #14

                              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 )

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups