【问题标题】:Change controls backcolor/forecolor properties from an Integer value?从整数值更改控件背景颜色/前景色属性?
【发布时间】:2013-08-29 18:28:43
【问题描述】:

我有一个允许用户控制应用程序外观的应用程序。他们能够更改 BackColor、ForeColor、Italicize 或 Bold 多种控件。所有这些属性值都存储在 SQL Server DB 中,包括以整数形式存储的 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

【问题讨论】:

  • intBC 使用什么样的值?
  • 传入 intBC 的值是 16711936,是的,粗体字体确实有效!

标签: vb.net


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2012-12-07
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 2017-12-16
    相关资源
    最近更新 更多