Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

asked on

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!
SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BlakeMcKenna

ASKER

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.
Hi BlakeMcKenna;

See if this corrects the issue.

uC3.BackColor = uC3.Parent.BackColor;
Unfortunately it did not...
Did anything at all different?
Nothing changed....all the UC's looked the same.
Is it possible to upload your UC project only so I can try and see if I can duplicate it here.
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
My mistake it is ForeColor. Thanks Jacques I do not know how I transposed that.
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.
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

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.)
For me it's called tunnel vision! LOL
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
Thanks guys for your help!