更改从Integer值控制backcolor / forecolor属性?

时间:2013-08-29 18:28:43

标签: vb.net

我有一个允许用户控制应用程序外观的应用程序。他们能够改变BackColor,ForeColor,Italicize或Bold等各种控件。所有这些属性值都存储在SQL Server数据库中,包括BackColor和ForeColor,它们存储为整数。

我遇到的问题是,当用户登录时,会从数据库中检索这些值,并且由于某种原因,BackColor / ForeColor属性不起作用。下面是我的代码,我试图将颜色(它是整数值)分配给特定的控件。

    Public Sub ApplyUserSettings(ByVal userID As Integer, ByVal frm As Form)
    Try
        Dim colValues As New Collection

        colValues = BL.GetUserSettings(userID)

        'Col0 = userID
        'Col1 = ctlID           Type of Control / 1=Buttons 2=Grids 3=GroupBoxes 4=TextBoxes 5=Forms 6=Labels 7=ComboBoxes 8=RadioButton 9=CheckBoxes 10=Tabs
        'Col2 = propertyCode    BC=BackColor FC=ForeColor BT=Bold Text I=Italics
        'Col3 = ctlState        True or False
        'Col4 = ctlForeColor    Integer Value
        'Col5 = ctlBackColor    Integer Value

        Dim arr() As String
        Dim tmpCtlID As Integer = 0
        Dim ctlID As Integer = 0
        Dim ctlState As Boolean
        Dim propertyCode As String = ""
        Dim intFC As Integer = 0
        Dim intBC As Integer = 0
        Dim iFC As Color
        Dim iBC As Color
        Dim blnBoldAndItalics As Boolean = False

        For Each itm In colValues
            arr = Split(itm, ",")

            ctlID = CInt(arr(1))
            propertyCode = Trim(arr(2))
            ctlState = CBool(arr(3))
            intFC = arr(4)
            intBC = arr(5)

            If Not ctlID = tmpCtlID Then
                tmpCtlID = ctlID
                blnBoldAndItalics = False
            End If

            Select Case ctlID
                Case 1  'Buttons
                    Select Case propertyCode
                        Case "I"    'Italics
                            If ctlState = True Then
                                If blnBoldAndItalics Then
                                    For Each ctl As Control In frm.Controls
                                        If TypeOf (ctl) Is Button Then
                                            ctl.Font = New Font(ctl.Font.Name, ctl.Font.Size, Drawing.FontStyle.Bold Or Drawing.FontStyle.Italic)

                                        End If
                                    Next
                                Else
                                    For Each ctl As Control In frm.Controls
                                        If TypeOf (ctl) Is Button Then
                                            ctl.Font = New Font(ctl.Font, FontStyle.Italic)
                                        End If
                                    Next
                                End If
                            End If
                        Case "BT"   'Bold Text
                            If ctlState = True Then
                                For Each ctl As Control In frm.Controls
                                    If TypeOf (ctl) Is Button Then
                                        ctl.Font = New Font(ctl.Font, FontStyle.Bold)
                                    End If
                                Next
                                blnBoldAndItalics = True
                            End If
                        Case "BC"   'BackColor
                            If ctlState = True Then
                                For Each ctl As Control In frm.Controls
                                    If TypeOf (ctl) Is Button Then
                                        iBC = Color.FromArgb(intBC)
                                        ctl.BackColor = iBC
                                    End If
                                Next
                            End If
                        Case "FC"   'ForeColor
                            If ctlState = True Then
                                For Each ctl As Control In frm.Controls
                                    If TypeOf (ctl) Is Button Then
                                        iFC = Color.FromArgb(intFC)
                                        ctl.ForeColor = iFC
                                    End If
                                Next
                            End If
                    End Select

                Case 2  'DataGridViews
                Case 3  'GroupBoxes
                Case 4  'TextBoxes
                Case 5  'Forms
                Case 6  'Labels
                Case 7  'ComboBoxes
                Case 8  'RadioButton
                Case 9  'CheckBoxes
                Case 10 'TabControl
            End Select
        Next

    Catch ex As Exception
        strErr = "modMain/ApplyUserSettings() - " & ex.Message
        MessageBox.Show(strErr)
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

按钮将UseVisualStyleBackColor默认为true,这将阻止调用BackColor属性。

必须在设计时更改该属性。