如何将组合框的选择从数值设置为命名颜色

时间:2017-10-23 20:25:36

标签: .net vb.net combobox

我从本网站上找到的一些代码创建了一个颜色选择器组合框,虽然我现在找不到代码。

绘制颜色的代码:

Private Sub cboColorPicker_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles cboColor.DrawItem, cboBackground.DrawItem

    Dim g As Graphics
    Dim ItemBrush As Brush
    Dim ItemColor As Color
    Dim ItemFont As Font
    Dim ItemName As String
    Dim rect As Rectangle


    g = e.Graphics
    rect = e.Bounds

    If e.Index >= 0 Then
        'Get item color name
        ItemName = CType(sender, ComboBox).Items(e.Index).ToString

        'Get instance a font to draw item name with this style
        ItemFont = New Font("Arial", 9, FontStyle.Regular)

        'Get instance color from item name
        ItemColor = Color.FromName(ItemName)

        'Get instance brush with Solid style to draw background
        ItemBrush = New SolidBrush(ItemColor)

        'Draw the item name
        g.DrawString(ItemName, ItemFont, Brushes.Black, rect.X, rect.Top)

        'Draw the background with my brush style and rectangle of item
        g.FillRectangle(ItemBrush, rect.X, rect.Y, rect.Width, rect.Height)

    End If

End Sub

我填充这样的组合框:

Private Sub PopulateColorCombo(ByVal cbo As ComboBox)

    cbo.Items.Add("Black")
    cbo.Items.Add("Blue")
    cbo.Items.Add("Lime")
    cbo.Items.Add("Cyan")
    cbo.Items.Add("Red")
    cbo.Items.Add("Fuchsia")
    cbo.Items.Add("Yellow")
    cbo.Items.Add("White")
    cbo.Items.Add("Navy")
    cbo.Items.Add("Green")
    cbo.Items.Add("Teal")
    cbo.Items.Add("Maroon")
    cbo.Items.Add("Purple")
    cbo.Items.Add("Olive")
    cbo.Items.Add("Gray")

End Sub

我将值作为int存储在数据库中,例如black将为-16777216。

这是允许用户自定义报告的表单的一部分。我想在加载数据时更新组合,以根据数据库中存储的内容显示颜色。我似乎无法做到这一点。

所以说我加载的数据为HDI.Color = -16777216

Color.FromArgb(HDI.Color).ToString = "Color [A=255, R=0, G=0, B=0]"
Color.FromArgb(HDI.Color).Name = "ff000000"

但我无法获得实际的颜色名称。以下都不是。

cboColor.SelectedIndex = cboColor.FindStringExact(Color.FromArgb(HDI.Color).ToKnownColor.ToString)
cboColor.SelectedIndex = cboColor.FindStringExact(CType(Color.FromArgb(HDI.Color), Color).ToString)
cboColor.SelectedIndex = cboColor.FindStringExact(System.Drawing.ColorTranslator.FromHtml(Color.FromArgb(HDI.Color).Name).ToString)

我使用错误的组合框来完成这项任务吗?有没有办法让它发挥作用?

我不想提供全彩色选择器,因为它们不会使用很多颜色,所以我试图让用户保持简单。我甚至可以将颜色减半。

编辑:修复泄漏问题。

Private Sub cboColorPicker_DrawItem(ByVal sender As Object,ByVal e As DrawItemEventArgs)处理cboColor.DrawItem,cboBackground.DrawItem

    Dim g As Graphics
    Dim ItemColor As Color
    Dim ItemName As String
    Dim rect As Rectangle


    g = e.Graphics
    rect = e.Bounds

    If e.Index >= 0 Then
        'Get item color name
        ItemName = CType(sender, ComboBox).Items(e.Index).ToString

        'Get instance color from item name
        ItemColor = Color.FromName(ItemName)

        'Get instance brush with Solid style to draw background
        Using ItemBrush = New SolidBrush(ItemColor)

            'Draw the item name
            g.DrawString(ItemName, DirectCast(sender, ComboBox).Font, Brushes.Black, rect.X, rect.Top)

            'Draw the background with my brush style and rectangle of item
            g.FillRectangle(ItemBrush, rect.X, rect.Y, rect.Width, rect.Height)

        End Using

    End If

End Sub

表格结构: enter image description here

1 个答案:

答案 0 :(得分:0)

终于明白了,这是解决方案:

    Dim ActualColor As Color
    Dim ColorToCheck As KnownColor


    For Each ColorToCheck In [Enum].GetValues(GetType(KnownColor))
        ActualColor = Color.FromKnownColor(ColorToCheck)
        If Not ActualColor.IsSystemColor And HDI.Color = ActualColor.ToArgb Then
            cboColor.Text = ActualColor.Name
            Exit For
        End If
    Next