User Control colors are wrong?

I have a User Control that is dynamically inserted onto a dynamically added TabPage. The problem is that the ForeColor of all Label controls on the UC are blue and I have NO idea how that happened. I'm not setting them to that color anywhere in code. Ideas?

Below is the code that creates the new tabpage and uc.

    Private Sub BuildChannelControls()
        'Debug.Print("BuildChannelControls")

        Dim x As Integer = 0

        Try
            EH.ErrorMessage = String.Empty

            If tabTests.TabPages.Contains(tabChannels) Then
                For Each itm In arrChannels
                    Dim uC3 As New userChannelSettings

                    uC3.Name = "uC3_" & x + 1
                    uC3.mlChannelName.Text = itm

                    Dim tab As New TabPage

                    tab.Tag = itm
                    tab.Text = "Channel: " & itm.ToString
                    tab.Name = itm
                    tab.Controls.Add(uC3)
                    tabChannelList.Controls.Add(tab)
                Next
            End If

        Catch ex As Exception
            EH.ErrorMessage = "frmCalibration_3/BuildChannelControls() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try
    End Sub

Open in new window


Thanks!
BlakeMcKennaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
This may be causing your issue. From Microsoft documentation.

TabPage.BackColor Property

The default value of the BackColor property is the value of the Control.DefaultBackColor property unless the UseVisualStyleBackColor and Application.RenderWithVisualStyles property values are both true and the Appearance property of the parent TabControl has a value of Normal, in which case the default value of the BackColor property is Transparent. Child controls that you place on the TabPage inherit the BackColor value by default, so this behavior causes the background of the child controls to render with the current visual style.

Changing the value of the BackColor property automatically sets the UseVisualStyleBackColor property to false. If you want the TabPage background to render using visual styles but you want the child controls to inherit a BackColor value that you specify, set the UseVisualStyleBackColor property after you set the BackColor property.
0
BlakeMcKennaAuthor Commented:
That didn't work Fernando, however, I did notice that the colors only affect UserControls. I have a several different UC's on each of these tabs.
0
Fernando SotoRetiredCommented:
Hi BlakeMcKenna;

See if this corrects the issue.

uC3.BackColor = uC3.Parent.BackColor;
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

BlakeMcKennaAuthor Commented:
Unfortunately it did not...
0
Fernando SotoRetiredCommented:
Did anything at all different?
0
BlakeMcKennaAuthor Commented:
Nothing changed....all the UC's looked the same.
0
Fernando SotoRetiredCommented:
Is it possible to upload your UC project only so I can try and see if I can duplicate it here.
0
BlakeMcKennaAuthor Commented:
Unfortunately, we are not allowed to upload whole UC's/Forms/Projects etc. However, the control in question is comprised of 3 other UC's. I have provided the code-behind for these.

The one thing that these UC's have in common is that they make a call to a Module Level function called "GetFocusBackColor()". All this function does is set the BackColor for TextBoxes and ComboBoxes. I have attached that code as well.

UC #1
Public Class userChannelList

    Public Event ProcessSelection()
    Public Event EvaluateClick()

    Private Sub cmbCellAngle_Enter(sender As Object, e As EventArgs) Handles cmbCellAngle.Enter
        GetFocusBackColor(cmbCellAngle)
    End Sub

    Private Sub cmbCellAngle_Leave(sender As Object, e As EventArgs) Handles cmbCellAngle.Leave
        GetFocusBackColor(cmbCellAngle, True)
    End Sub

    Private Sub cmbCellAngle_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbCellAngle.SelectedIndexChanged
        RaiseEvent ProcessSelection()
    End Sub
    '
    '
    '
    '
    '
    '
    Private Sub cmbDirectionType_Enter(sender As Object, e As EventArgs) Handles cmbDirectionType.Enter
        GetFocusBackColor(cmbDirectionType)
    End Sub

    Private Sub cmbDirectionType_Leave(sender As Object, e As EventArgs) Handles cmbDirectionType.Leave
        GetFocusBackColor(cmbDirectionType, True)
    End Sub

    Private Sub cmbDirectionType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDirectionType.SelectedIndexChanged
        RaiseEvent ProcessSelection()
    End Sub
    '
    '
    '
    '
    '
    '
    Private Sub cmbDirection_Enter(sender As Object, e As EventArgs) Handles cmbDirection.Enter
        GetFocusBackColor(cmbDirection)
    End Sub

    Private Sub cmbDirection_Leave(sender As Object, e As EventArgs) Handles cmbDirection.Leave
        GetFocusBackColor(cmbDirection, True)
    End Sub

    Private Sub cmbDirection_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDirection.SelectedIndexChanged
        RaiseEvent ProcessSelection()
    End Sub
End Class

Open in new window


UC #2
Public Class userLoadSettings

    Public Event CheckComboValues(gTestType)
    Public Event GetUnitType()
    '
    '
    '
    '
    '
    '
    Private Sub txtLoadCapacity_Click(sender As Object, e As EventArgs) Handles txtLoadCapacity.Click
        txtLoadCapacity.SelectAll()
    End Sub

    Private Sub txtLoadCapacity_Enter(sender As Object, e As EventArgs) Handles txtLoadCapacity.Enter
        GetFocusBackColor(txtLoadCapacity)
    End Sub

    Private Sub txtLoadCapacity_Leave(sender As Object, e As EventArgs) Handles txtLoadCapacity.Leave
        GetFocusBackColor(txtLoadCapacity, True)
    End Sub
    '
    '
    '
    '
    '
    '
    Private Sub cmbLoadDecimal_Enter(sender As Object, e As EventArgs) Handles cmbLoadDecimal.Enter
        GetFocusBackColor(cmbLoadDecimal)
    End Sub

    Private Sub cmbLoadDecimal_Leave(sender As Object, e As EventArgs) Handles cmbLoadDecimal.Leave
        GetFocusBackColor(cmbLoadDecimal, True)
    End Sub

    Friend Sub cmbLoadDecimal_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbLoadDecimal.SelectedIndexChanged
        If frmCalibration_3.blnIsLoaded Then
            If cmbLoadDecimal.SelectedIndex <> -1 Then
                RaiseEvent CheckComboValues(gTestType)
            End If
        End If
    End Sub
    '
    '
    '
    '
    '
    '
    Private Sub cmbLoadUnitType_Enter(sender As Object, e As EventArgs) Handles cmbLoadUnitType.Enter
        GetFocusBackColor(cmbLoadUnitType)
    End Sub

    Private Sub cmbLoadUnitType_Leave(sender As Object, e As EventArgs) Handles cmbLoadUnitType.Leave
        GetFocusBackColor(cmbLoadUnitType, True)
    End Sub

    Friend Sub cmbLoadUnitType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbLoadUnitType.SelectedIndexChanged
        If frmCalibration_3.blnIsLoaded Then
            If cmbLoadUnitType.SelectedIndex <> -1 Then
                gblnLoadUnitTypeSelectedLoad = True
                gblnLoadUnitTypeSelectedCreep = True
                gblnLoadUnitTypeSelectedRepeat = True

                RaiseEvent GetUnitType()
                RaiseEvent CheckComboValues(gTestType)
            End If
        End If
    End Sub
    '
    '
    '
    '
    '
    '
    Private Sub cmbLoadUnit_Enter(sender As Object, e As EventArgs) Handles cmbLoadUnit.Enter
        GetFocusBackColor(cmbLoadUnit)
    End Sub

    Private Sub cmbLoadUnit_Leave(sender As Object, e As EventArgs) Handles cmbLoadUnit.Leave
        GetFocusBackColor(cmbLoadUnit, True)
    End Sub

    Friend Sub cmbLoadUnit_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbLoadUnit.SelectedIndexChanged
        If frmCalibration_3.blnIsLoaded Then
            If cmbLoadUnit.SelectedIndex <> -1 Then
                RaiseEvent CheckComboValues(gTestType)
            End If
        End If
    End Sub
    '
    '
    '
    '
    '
    '
    Private Sub cmbLoadPoints_Enter(sender As Object, e As EventArgs) Handles cmbLoadPoints.Enter
        GetFocusBackColor(cmbLoadPoints)
    End Sub

    Private Sub cmbLoadPoints_Leave(sender As Object, e As EventArgs) Handles cmbLoadPoints.Leave
        GetFocusBackColor(cmbLoadPoints, True)
    End Sub

    Friend Sub cmbLoadPoints_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbLoadPoints.SelectedIndexChanged
        If frmCalibration_3.blnIsLoaded Then
            If cmbLoadPoints.SelectedIndex <> -1 Then
                RaiseEvent CheckComboValues(gTestType)
            End If
        End If
    End Sub
End Class

Open in new window


UC #3
Public Class userOutputSettings

    Private strTestType As String = String.Empty

    Public Event GetUnitType()
    Public Event CheckComboValues(strTestType)
    '
    '
    '
    '
    '
    '
    Private Sub cmbOutputUnitType_Enter(sender As Object, e As EventArgs) Handles cmbOutputUnitType.Enter
        GetFocusBackColor(cmbOutputUnitType)
    End Sub

    Private Sub cmbOutputUnitType_Leave(sender As Object, e As EventArgs) Handles cmbOutputUnitType.Leave
        GetFocusBackColor(cmbOutputUnitType, True)
    End Sub

    Friend Sub cmbOutputUnitType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbOutputUnitType.SelectedIndexChanged
        If cmbOutputUnitType.SelectedIndex <> -1 Then
            gblnOutputUnitTypeSelectedLoad = True
            gblnOutputUnitTypeSelectedCreep = True
            gblnOutputUnitTypeSelectedRepeat = True

            RaiseEvent GetUnitType()

            If EH.ErrorMessage = String.Empty Then
                RaiseEvent CheckComboValues(strTestType)
            End If
        End If
    End Sub
    '
    '
    '
    '
    '
    '
    Private Sub cmbOutputUnit_Enter(sender As Object, e As EventArgs) Handles cmbOutputUnit.Enter
        GetFocusBackColor(cmbOutputUnit)
    End Sub

    Private Sub cmbOutputUnit_Leave(sender As Object, e As EventArgs) Handles cmbOutputUnit.Leave
        GetFocusBackColor(cmbOutputUnit, True)
    End Sub

    Friend Sub cmbOutputUnit_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbOutputUnit.SelectedIndexChanged
        If cmbOutputUnit.SelectedIndex <> -1 Then
            RaiseEvent CheckComboValues(strTestType)
        End If
    End Sub
    '
    '
    '
    '
    '
    '
    Private Sub cmbOutputDecimal_Enter(sender As Object, e As EventArgs) Handles cmbOutputDecimal.Enter
        GetFocusBackColor(cmbOutputDecimal)
    End Sub

    Private Sub cmbOutputDecimal_Leave(sender As Object, e As EventArgs) Handles cmbOutputDecimal.Leave
        GetFocusBackColor(cmbOutputDecimal, True)
    End Sub

    Friend Sub cmbOutputDecimal_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbOutputDecimal.SelectedIndexChanged
        If cmbOutputDecimal.SelectedIndex > -1 Then
            RaiseEvent CheckComboValues(strTestType)
        End If
    End Sub
End Class

Open in new window


GetFocusBackColor
    Public Sub GetFocusBackColor(ByVal sender As Control, Optional ByVal blnLostFocus As Boolean = False)
        'Debug.Print("GetFocusBackColor")

        Try
            Dim ctlID As Integer = 0
            Dim iBackGroundColor As Integer = 0
            Dim iFocusBackGroundColor As Integer = 0
            Dim ctl As Control = DirectCast(sender, Control)

            EH.ErrorMessage = String.Empty

            If TypeOf ctl Is Button Then
                ctlID = 1
            ElseIf TypeOf ctl Is TextBox Then
                ctlID = 4
            ElseIf TypeOf ctl Is ComboBox Then
                ctlID = 7
            End If

            Dim result = lstBGColors.Find(Function(c) c.control_ID = ctlID)

            If gblnFocusBackColor Then
                iFocusBackGroundColor = gFocusBackColor
            Else
                iFocusBackGroundColor = ctl.BackColor.ToArgb
            End If

            If gblnVisualSettings Then  'The Visual Settings in turned on for User
                If blnLostFocus Then
                    If Not IsNothing(result) Then
                        If result.checked Then
                            iBackGroundColor = result.BC
                        Else
                            If ctlID <> 1 Then
                                iBackGroundColor = Color.White.ToArgb
                            Else
                                iBackGroundColor = Control.DefaultBackColor.ToArgb
                            End If
                        End If
                    Else
                        If ctlID <> 1 Then
                            iBackGroundColor = Color.White.ToArgb
                        Else
                            iBackGroundColor = Control.DefaultBackColor.ToArgb
                        End If
                    End If

                    ctl.BackColor = Color.FromArgb(iBackGroundColor)
                Else
                    ctl.BackColor = Color.FromArgb(iFocusBackGroundColor)
                End If
            End If

        Catch ex As Exception
            EH.ErrorMessage = "modMain/GetFocusBackColor() - " & ex.Message & "~E"
        End Try

        EH.ProcessMessages(frmMain, frmMain.sbr, EH.ErrorMessage)
    End Sub

Open in new window


I have also attached the UC the way it should look and the way it looks with the blue colors.
Correct-Coloring.JPG
Incorrect-Coloring.JPG
0
Jacques Bourgeois (James Burger)PresidentCommented:
The question was about the ForeColor, not the BackColor.

By default, the ForeColor with take the same color as the container control. So, by default:

A TabControl put on a Form will take the ForeColor of that Form
A TabPage put on a TabControl will take the ForeColor of that TabControl
A UserControl put on a TabPage will take the ForeColor of that TabPage
A Label put on a UserControl will take the Forecolor of that UserControl

So, either the Form, the TabControl, theTabPage or the UserControl has its ForeColor set to blue, and it cascade up to the labels.

If you eventually does not see where in the sequence of container controls the blue comes from, simply set the ForeColor of your Labels to Black, so that it does not inherits its ForeColor from somewhere else.
0
Fernando SotoRetiredCommented:
The only difference between the two images is Correct-Coloring.JPG and Incorrect-Coloring.JPG is that the ForeColor of the Text is in blue in Incorrect-Coloring.JPG, is that correct?
0
Fernando SotoRetiredCommented:
My mistake it is ForeColor. Thanks Jacques I do not know how I transposed that.
0
BlakeMcKennaAuthor Commented:
Fernando...you are correct. The forecolor of the text is blue...they should be black!

This is really strange because no where in my code do I set the ForeColor to blue.

I tried the code you suggested earlier Fernando except this time I used ctl.Parent.ForeColor and that still didn't work.
0
BlakeMcKennaAuthor Commented:
Here is the code that dynamically builds the controls. It's pretty much the same as original post but with some changes based on Fernando's suggestions.

    Private Sub BuildChannelControlTabs()
        'Debug.Print("BuildChannelControlTabs")

        Dim x As Integer = 0

        Try
            EH.ErrorMessage = String.Empty

            If tabTests.TabPages.Contains(tabChannels) Then
                For Each itm In arrChannels
                    Dim tab As New TabPage

                    tab.Tag = itm
                    tab.Text = "Channel: " & itm.ToString
                    tab.Name = itm
                    tabChannelList.Controls.Add(tab)

                    Dim uC1 As New userChannelSettings

                    uC1.Name = "uC1_" & x + 1
                    uC1.mlChannelName.Text = itm
                    tab.Controls.Add(uC1)
                    uC1.ForeColor = uC1.Parent.ForeColor
                Next
            Else
                EH.ErrorMessage = "Invalid Control Creation!" & "~E"
                GoTo ProcessMessage
            End If

            If tabTests.TabPages.Contains(tabEquipmentByChannel) Then
                For Each itm In arrChannels
                    Dim tab As New TabPage

                    tab.Tag = itm
                    tab.Text = "Channel: " & itm.ToString
                    tab.Name = itm
                    tabChannelEquipment.Controls.Add(tab)

                    Dim uC2 As New userChannelEquipment

                    uC2.Name = "uC2_" & x + 1
                    tab.Controls.Add(uC2)
                Next
            Else
                EH.ErrorMessage = "Invalid Control Creation!" & "~E"
            End If

ProcessMessage:

        Catch ex As Exception
            EH.ErrorMessage = "frmCalibration_3/BuildChannelControlTabs() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try
    End Sub

Open in new window

0
Jacques Bourgeois (James Burger)PresidentCommented:
Fernando, it's probably because you had your back to the screen when you answered. Blake probably also works with his back to the screen, because he did not seem to notice your mistake. A lot of programmers, me included and more often than almost everybody else, sometimes work with the screen in their back. It's easy to mix up Fore and Back in such a situation.

:-)

(The text in italic is a joke that one of my students made once, when I made the same mistake. In the training room, the projector shows the screen in my back.)
0
BlakeMcKennaAuthor Commented:
For me it's called tunnel vision! LOL
0
BlakeMcKennaAuthor Commented:
Ok,

New development. I tried adding a tab in the designer and dropping an instance of the UC on the tab and ran the app. It worked as it should. So....I'm thinking it has something to do with the dynamic creation of the control. I'm obviously not setting something.
0
BlakeMcKennaAuthor Commented:
I found the problem. For some reason, it had to do with the order in which I was adding the UC to the tab.

OLD CODE:
                For Each itm In arrChannels
                    Dim tab As New TabPage

                    tab.Tag = itm
                    tab.Text = "Channel: " & itm.ToString
                    tab.Name = itm
                    tabChannelList.Controls.Add(tab)

                    Dim uC1 As New userChannelSettings

                    uC1.Name = "uC1_" & x + 1
                    uC1.mlChannelName.Text = itm
                    tab.Controls.Add(uC1)
                    uC1.ForeColor = tab.ForeColor
                Next

Open in new window


NEW CODE
                For Each itm In arrChannels
                    Dim tab As New TabPage
                    Dim uC1 As New userChannelSettings

                    uC1.Name = "uC1_" & x + 1
                    uC1.mlChannelName.Text = itm
                    tab.Controls.Add(uC1)
                    uC1.ForeColor = tab.ForeColor

                    tab.Tag = itm
                    tab.Text = "Channel: " & itm.ToString
                    tab.Name = itm
                    tabChannelList.Controls.Add(tab)
                Next

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Well I can't see why that would cause this issue but great that it is working for you. Make sure you document that in the code so that if someone works on that part of the code in the future they will be aware of this.
0
Jacques Bourgeois (James Burger)PresidentCommented:
uC1.ForeColor = tab.ForeColor is useless unless you changed the default ForeColor of your UserControl. By default, a UserControl automatically takes the ForeColor of the Tab that contains it.

You have something that is set to Blue somewhere, and it cascades down the chain. Changing the order that you use to set things simply changes the chain and hides that from you.

Do a search on your whole project or solution, I am quite sure that you will find the word Blue somewhere in there.
0
BlakeMcKennaAuthor Commented:
Thanks guys for your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.