CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2010
    Posts
    7

    Seperating Tabs from tabcontrol

    Hi,

    I am using Visual Studio 2008 to make my first c# application. I have a tab control with one tabpage on it.
    What i would like to do is to have a seperate class/form/usercontrol that has content that needs to be displayed in a new tabpage. This tabpage i will create programmatically (this i can do).
    What i tried was to have a user control and then add this usercontrol to the tab, but it didn't work for me.

    My aim is to have the class for each tab seperatly for easy managability, as it is in Java. Can anybody help me out please?

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Seperating Tabs from tabcontrol

    Quote Originally Posted by snipered View Post
    What i tried was to have a user control and then add this usercontrol to the tab, but it didn't work for me.
    this is (was) the right approach. what didn't work?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  3. #3
    Join Date
    Feb 2010
    Posts
    7

    Re: Seperating Tabs from tabcontrol

    Hi,

    Thanks for the reply.

    I have tried the following:

    Code:
     UserControl1 controlName = new UserControl1();
                tabControl1.TabPages.Add("test");
                tabControl1.TabPages["test"].Controls.Add( controlName);
                tabControl1.Show();
    There is no error. The tab is created, but the userControl does not show up on the tab, in fact it doesn't show at all.

    Any ideas, what i'm doing wrong?

  4. #4
    Join Date
    Feb 2010
    Posts
    7

    Re: Seperating Tabs from tabcontrol

    Oh, by the way, this is just a test and i'm running this on the form load event.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Seperating Tabs from tabcontrol

    Quote Originally Posted by snipered View Post
    Oh, by the way, this is just a test and i'm running this on the form load event.
    Put the code in the constructor after the InitializeComponent( ) call.

  6. #6
    Join Date
    Feb 2010
    Posts
    7

    Re: Seperating Tabs from tabcontrol

    Quote Originally Posted by Arjay View Post
    Put the code in the constructor after the InitializeComponent( ) call.
    Hi,

    I tried placing it in the constructor but now it keeps giving me and error:

    System.NullReferenceException was unhandled
    Message="Object reference not set to an instance of an object."

    I've highlighted where it crashes. If i've created and instance at UserControl1 controleeName, why is it moaning 2 lines down?

    Code:
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            
                UserControl1 controleeName = new UserControl1();
                tabControl1.TabPages.Add("test");
                tabControl1.TabPages["test"].Controls.Add( controleeName ); //crashes here
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
              
            }
        }

  7. #7
    Join Date
    Feb 2010
    Posts
    7

    Re: Seperating Tabs from tabcontrol

    I added a try catch statement. That solved the nullreference error. But still the usercontrol wont show within the tabpage.

  8. #8
    Join Date
    Feb 2010
    Posts
    7

    Re: Seperating Tabs from tabcontrol

    I managed to get it to work.

    All i had to do was change this:

    Code:
     tabControl1.TabPages.Add("test");
    To this:

    Code:
    tabControl1.TabPages.Add("test","d");

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Seperating Tabs from tabcontrol

    Quote Originally Posted by snipered View Post
    Hi,

    I tried placing it in the constructor but now it keeps giving me and error:

    System.NullReferenceException was unhandled
    Message="Object reference not set to an instance of an object."

    I've highlighted where it crashes. If i've created and instance at UserControl1 controleeName, why is it moaning 2 lines down?

    Code:
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            
                UserControl1 controleeName = new UserControl1();
                tabControl1.TabPages.Add("test");
                tabControl1.TabPages["test"].Controls.Add( controleeName ); //crashes here
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
     
            }
        }
    You are declaring a local UserControl1 variable which goes out of scope at the end of the constructor.

    You need to declare a class field and then initialize it in the constructor.

    Code:
     
    public partial class Form1 : Form
    {
      public Form1()
      {
        InitializeComponent();            
        _controleeName = new UserControl1();
        tabControl1.TabPages.Add("test");
        tabControl1.TabPages["test"].Controls.Add( controleeName ); //crashes here
      }
     
      private UserControl1 _controleeName = null;
    }

  10. #10
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Seperating Tabs from tabcontrol

    Quote Originally Posted by snipered View Post
    I managed to get it to work.
    All i had to do was change this:
    Code:
     tabControl1.TabPages.Add("test");
    To this:
    Code:
    tabControl1.TabPages.Add("test","d");
    true, the first overloaded Add adds only a tab with the text "test" but the second creates a tab with the key "text" and a text "d"
    so, in your first try the key didn't exist that was the reason for null reference
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured