CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Nov 2008
    Posts
    2

    Exclamation Problem with TabControl

    i create a button in TabControl1 with a name Button1

    when i use

    Button1.BackColor = System.Drawing.Color.MintCream

    it is working fine but when i use (any of the following)

    dim i as integer=1

    Controls(String.Concat("Button", i)).BackColor = System.Drawing.Color.MintCream

    (or)

    Controls(String.Concat("Button"& i)).BackColor = System.Drawing.Color.MintCream

    it gives "An unhandled exception of type 'System.NullReferenceException' occurred in xxxx.exe" error with Additional information: Object reference not set to an instance of an object

    what is the problem i didnt understand :S
    (i am using vb 2005)
    Last edited by matatu; November 25th, 2008 at 06:00 AM. Reason: version of my visual basic compiler

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Problem with TabControl

    Welcome to the Forums!

    You need to specify the button's container control as well.

    By doing it the way you have it, it will assume that button1's container is the form. Do it like this :
    Code:
            Dim i As Integer = 1 'Create Variable & Set it to 1
    
            Me.TabPage1.Controls(String.Concat("Button", i)).BackColor = System.Drawing.Color.Blue 'Set Backcolor Of Button1 To Blue
            i = i + 1 'Increment Counter
            Me.TabPage1.Controls(String.Concat("Button" & i)).BackColor = System.Drawing.Color.Green 'Set Background of Button2 To Green

  3. #3
    Join Date
    Nov 2008
    Posts
    2

    Re: Problem with TabControl

    Nice solution. It worked!
    But the problem is that i have 82 buttons with 7 tabpages in one tabcontol and buttons somehow randomly distributed into tabpages :S and some buttons are directly on the forum (isnt related with any tabcontrol)
    Is there any other solution for this?
    There is probably 2 solutions for this problem.
    One is to somehow get the button id's for every tabpages and and use above alogirthm
    Second is direct acess to buttons such as buttoni.back color where i=1 to N
    Maybe there is different command for the second solution.
    Last edited by matatu; November 25th, 2008 at 06:56 AM. Reason: improvement

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Problem with TabControl

    The solution I can think of would be to use the Controls collection instead. Trust me, you'll save yourself a whole lot of trouble

    You could loop through the form, and each tabpage and determine whether or not the "child" control is a button, then, set it's color.

    Here's a bit more info on Controls Collection :

    http://www.sellsbrothers.com/askthew...rolcollect.htm

    http://visualbasic.about.com/od/usin...ctrlarraya.htm

    http://www.eggheadcafe.com/community...the--cont.aspx

  5. #5
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: Problem with TabControl

    How about using something along the lines of:
    Code:
            For Each TabC In Me.Controls.OfType(Of TabControl)()
                For Each But In TabC.Controls.OfType(Of Button)()
                    'Process the buttons here
                Next
            Next
    This requires the extensions given by one of the .NET namespaces...I'll look up which one.

    *EDIT*: It's needs System.Linq to be imported...
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

Tags for this Thread

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