Results 1 to 10 of 10

Thread: [RESOLVED] [2008] Adding TabPages at runtime, BackColor is wrong!

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Resolved [RESOLVED] [2008] Adding TabPages at runtime, BackColor is wrong!

    Hi,

    I am trying to add a TabPage containing a UserControl to a TabControl at runtime, because the number of TabPages to add are determined at runtime.

    The UserControl (objDataInfo) is just two Textboxes, two labels and two Radiobuttons, nothing too exciting...

    The TextBoxes have their Anchor properties set to Left, Up, Right so they scale with the width of the usercontrol.

    The TabPages are added in the constructor of the form, which takes the number of tabpages to create (n) as an argument:
    vb.net Code:
    1. Dim cList As List(Of objDataInfo)
    2.  
    3.     Public Sub New(ByVal n As Integer)
    4.         InitializeComponent()
    5.  
    6.         Dim t As TabPage
    7.         Dim c As objDataInfo
    8.         cList = New List(Of objDataInfo)
    9.  
    10.         'Create tabpages on the fly
    11.         For i As Integer = 1 To n
    12.             t = New TabPage("Objective " & i.ToString)
    13.             c = New objDataInfo(i)
    14.  
    15.             tab.TabPages.Add(t)
    16.  
    17.             c.Dock = DockStyle.Fill
    18.             c.BackColor = t.BackColor
    19.  
    20.             t.Controls.Add(c)
    21.  
    22.             cList.Add(c)
    23.         Next
    24.     End Sub

    The cList is just a list so I can reference the controls later.


    The problem is that the BackColor of the usercontrols are just the standard gray, while the BackColor of a normal TabPage is not (at least not on all themes). As far as I know it is pure white on Windows Vista, but on XP (Media Center Edition) it is some kind of blueish hue...
    (I am setting the usercontrol BackColor to transparent so it should match the BackColor of the TabPage, right?)

    I have also tried setting it to the BackColor of the first TabPage (which is not created at run-time so does have the correct BackColor, see image below) but that didn't work either.

    Here is a screenshot of the first Tabpage (created during Design Time) with the correct BackColor (should match the standard BackColor on any theme):


    And here is the second TabPage (generated at run-time) with obviously the wrong BackColor...




    So... How can I get the standard BackColor of a standard TabPage?


    (I tried leaving out the usercontrol all-together, but the TabPage still has a gray background...)

  2. #2
    Addicted Member
    Join Date
    Dec 2008
    Posts
    185

    Re: [2008] Adding TabPages at runtime, BackColor is wrong!

    You could create a custom tabpage control (which inherits tabpage) and set the colour as it is initialised... or you could have one created at designtime and made invisible... then create new instance at runtime.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2008] Adding TabPages at runtime, BackColor is wrong!

    Quote Originally Posted by HairyMike
    You could create a custom tabpage control (which inherits tabpage) and set the colour as it is initialised
    I don't see how that is any different from just setting the BackColor after I create the TabPage. I still don't know what color to set it to, since it can be different for every type of Windows, every Theme etc...
    I also saw this behaviour in all of my custom tabcontrols (I made quite a few); they all had a gray background. I just set it to White manually though because I was using Vista back then, I didn't even think about users on XP who might find a white background strange (oops!).

    Quote Originally Posted by HairyMike
    or you could have one created at designtime and made invisible... then create new instance at runtime.
    I just tried this but I didn't get it to work correctly, can you explain this further?

    I added just a blank TabPage (TabPage1) and changed the code to this:
    vb.net Code:
    1. Dim cList As List(Of objDataInfo)
    2.  
    3.     Public Sub New(ByVal n As Integer)
    4.         InitializeComponent()
    5.  
    6.         cList = New List(Of objDataInfo)
    7.  
    8.         'Create tabpages on the fly
    9.         For i As Integer = 1 To n
    10.             Dim t As TabPage = TabPage1
    11.             t.Text = "Objective " & i
    12.             Dim c As objDataInfo = New objDataInfo(i)
    13.             t.Controls.Add(c)
    14.             c.Dock = DockStyle.Fill
    15.             c.Visible = True
    16.  
    17.             tab.TabPages.Add(t)
    18.         Next
    19.         tab.TabPages.Remove(TabPage1)
    20.     End Sub

    The BackColor is now correct, but I just see a blank "TabPage 5" (n = 5) 5 times. There is no sign of my control, and only TabPage5 appears (5 times...)

    Not very surprising actually since I'm just copying the same tabpage into the TabControl 5 times..?

    I probably didn't understand correctly.

  4. #4
    Addicted Member
    Join Date
    Dec 2008
    Posts
    185

    Re: [2008] Adding TabPages at runtime, BackColor is wrong!

    Replace this
    Code:
    Dim t As TabPage = TabPage1
    with this
    Code:
    Dim t As New TabPage1
    Sorry, in a rush

  5. #5

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2008] Adding TabPages at runtime, BackColor is wrong!

    it should be a New TabPage ..... don't try to set it to tabPage1 .... that will jsut get you a reference to the first tab page that's in the collection.... not a new one, which is what you really want. If you want to use the backcolor of TabPage1, that's fine, but just use it as TabPage1.BackColor ... don't set T to it at all.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2008] Adding TabPages at runtime, BackColor is wrong!

    Quote Originally Posted by techgnome
    it should be a New TabPage ..... don't try to set it to tabPage1 .... that will jsut get you a reference to the first tab page that's in the collection.... not a new one, which is what you really want. If you want to use the backcolor of TabPage1, that's fine, but just use it as TabPage1.BackColor ... don't set T to it at all.

    -tg
    That's all fine, I understand that, but that is exactly what this whole thread is about. Of course I can add a New Tabpage, but it will have a gray backcolor, not the backcolor of the standard TabPages when you add them at Design Time!
    Setting the backcolor of my new TabPage to 'TabPage1.BackColor' doesn't work; TabPage1.BackColor is Transparent, and if I set my new TabPage's backcolor to Transparent, it still shows as gray.

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] Adding TabPages at runtime, BackColor is wrong!

    It's very funny... I can't seem to tell where a tabpage gets backcolor from in design mode since it is automatically set to transparent. The tabcontrol.backcolor = control, thus the tabpage should be seen as control color since its backcolor is transparent, and yet it is actually drawn in whitesmoke color.
    Now tabpages created during runtime have the default backcolor = control, and that is consistent with what Nick described. Manuaaly set its backcolor to transparent has no effect since it's then picks up the color of the tabcontrol, which is control color. Maybe setting it to whitesmoke, but that also will be different depending on the windows theme used.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  9. #9
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2008] Adding TabPages at runtime, BackColor is wrong!

    Hi,

    I think you can change the background color of the TabPage's page area (where all of the controls sit) itself with the BackColor property... however you cannot change the color of the tabs themselves or the overall TabControl control without painting it yourself.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2008] Adding TabPages at runtime, BackColor is wrong!

    Quote Originally Posted by stanav
    It's very funny... I can't seem to tell where a tabpage gets backcolor from in design mode since it is automatically set to transparent. The tabcontrol.backcolor = control, thus the tabpage should be seen as control color since its backcolor is transparent, and yet it is actually drawn in whitesmoke color.
    Now tabpages created during runtime have the default backcolor = control, and that is consistent with what Nick described. Manuaaly set its backcolor to transparent has no effect since it's then picks up the color of the tabcontrol, which is control color. Maybe setting it to whitesmoke, but that also will be different depending on the windows theme used.
    Exactly.

    Quote Originally Posted by sparrow1
    Hi,

    I think you can change the background color of the TabPage's page area (where all of the controls sit) itself with the BackColor property... however you cannot change the color of the tabs themselves or the overall TabControl control without painting it yourself.

    Wkr,

    sparrow1
    I did not mention changing the BackColor of the TabControl at all, only of the TabPage. The problem is that the Designer somehow paints a different BackColor on a new TabPage then it says in the Properties.


    I have had a quick look with .NET Reflector at the TabPage, maybe this is interesting:
    vb.net Code:
    1. Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
    2.     Dim parentInternal As TabControl = TryCast(Me.ParentInternal,TabControl)
    3.     If ((Application.RenderWithVisualStyles AndAlso Me.UseVisualStyleBackColor) AndAlso ((Not parentInternal Is Nothing) AndAlso (parentInternal.Appearance = TabAppearance.Normal))) Then
    4.         Dim backColor As Color = IIf(Me.UseVisualStyleBackColor, Color.Transparent, Me.BackColor)
    5.         Dim bounds As Rectangle = LayoutUtils.InflateRect(Me.DisplayRectangle, MyBase.Padding)
    6.         Dim rectangle2 As New Rectangle((bounds.X - 4), (bounds.Y - 2), (bounds.Width + 8), (bounds.Height + 6))
    7.         TabRenderer.DrawTabPage(e.Graphics, rectangle2)
    8.         If (Not Me.BackgroundImage Is Nothing) Then
    9.             ControlPaint.DrawBackgroundImage(e.Graphics, Me.BackgroundImage, backColor, Me.BackgroundImageLayout, bounds, bounds, Me.DisplayRectangle.Location)
    10.         End If
    11.     Else
    12.         MyBase.OnPaintBackground(e)
    13.     End If
    14. End Sub
    I suppose this takes care of the TabPage background rendering, but I still can't explain why it doesn't run this when I create a TabPage during run-time...???
    I have been looking for the TabPageDesigner (the designer that takes care of the actions taken when a component/control is created at design-time) but I couldn't find it... Reflector says it should be:
    vb.net Code:
    1. <Designer("System.Windows.Forms.Design.TabPageDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), ...
    But I can't find any TabPageDesigner entry in System.Windows.Forms.Design...

    I don't really know enough about Reflector to be able to draw any conclusions from this lol...



    EDIT
    Got it, it seems you need to manually set "UseVisualStyleBackColor" to true to make it work. Apparently the TabPageDesigner handles this internally somewhere only for Design-time.
    vb.net Code:
    1. t.UseVisualStyleBackColor = True

    Thanks for the help!

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