[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...
http://i42.tinypic.com/64phrn.jpg
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:
Dim cList As List(Of objDataInfo)
Public Sub New(ByVal n As Integer)
InitializeComponent()
Dim t As TabPage
Dim c As objDataInfo
cList = New List(Of objDataInfo)
'Create tabpages on the fly
For i As Integer = 1 To n
t = New TabPage("Objective " & i.ToString)
c = New objDataInfo(i)
tab.TabPages.Add(t)
c.Dock = DockStyle.Fill
c.BackColor = t.BackColor
t.Controls.Add(c)
cList.Add(c)
Next
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):
http://i43.tinypic.com/23wr6kg.jpg
And here is the second TabPage (generated at run-time) with obviously the wrong BackColor...
http://i39.tinypic.com/1zcg5fq.jpg
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...)
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.
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:
Dim cList As List(Of objDataInfo)
Public Sub New(ByVal n As Integer)
InitializeComponent()
cList = New List(Of objDataInfo)
'Create tabpages on the fly
For i As Integer = 1 To n
Dim t As TabPage = TabPage1
t.Text = "Objective " & i
Dim c As objDataInfo = New objDataInfo(i)
t.Controls.Add(c)
c.Dock = DockStyle.Fill
c.Visible = True
tab.TabPages.Add(t)
Next
tab.TabPages.Remove(TabPage1)
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.
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 :)
Re: [2008] Adding TabPages at runtime, BackColor is wrong!
Doesn't let me do that because TabPage1 isn't a type, it's an instance of a type... It says "Type 'TabPage1' is not defined."
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
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.
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.
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
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:
Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
Dim parentInternal As TabControl = TryCast(Me.ParentInternal,TabControl)
If ((Application.RenderWithVisualStyles AndAlso Me.UseVisualStyleBackColor) AndAlso ((Not parentInternal Is Nothing) AndAlso (parentInternal.Appearance = TabAppearance.Normal))) Then
Dim backColor As Color = IIf(Me.UseVisualStyleBackColor, Color.Transparent, Me.BackColor)
Dim bounds As Rectangle = LayoutUtils.InflateRect(Me.DisplayRectangle, MyBase.Padding)
Dim rectangle2 As New Rectangle((bounds.X - 4), (bounds.Y - 2), (bounds.Width + 8), (bounds.Height + 6))
TabRenderer.DrawTabPage(e.Graphics, rectangle2)
If (Not Me.BackgroundImage Is Nothing) Then
ControlPaint.DrawBackgroundImage(e.Graphics, Me.BackgroundImage, backColor, Me.BackgroundImageLayout, bounds, bounds, Me.DisplayRectangle.Location)
End If
Else
MyBase.OnPaintBackground(e)
End If
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:
<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:
t.UseVisualStyleBackColor = True
Thanks for the help!