在组合框中显示颜色及其名称的列表



我正在尝试在组合框中显示颜色列表。框中的每个项都包含由填充相应颜色的矩形后缀的颜色名称,如此处的项目所示。

该项目是用 c# 编写的。我不懂 c#,所以我将代码转换为 vb .net,如下所示。

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim ColorList As ArrayList = New ArrayList()
Dim colorType As Type = GetType(System.Drawing.Color)
Dim propInfoList As PropertyInfo() = colorType.GetProperties(BindingFlags.[Static] Or BindingFlags.DeclaredOnly Or BindingFlags.[Public])
For Each c As PropertyInfo In propInfoList
ComboBox1.Items.Add(c.Name)
Next
End Sub
Private Sub Combobox1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles ComboBox1.DrawItem
Dim g As Graphics = e.Graphics
Dim rect As Rectangle = e.Bounds
If e.Index >= 0 Then
Dim n As String = (CType(sender, ComboBox)).Items(e.Index).ToString()
Dim f As Font = New Font("Arial", 9, FontStyle.Regular)
Dim c As Color = Color.FromName(n)
Dim b As Brush = New SolidBrush(c)
g.DrawString(n, f, Brushes.Black, rect.X, rect.Top)
g.FillRectangle(Brushes.Blue, rect.X + 110, rect.Y + 5, rect.Width - 10, rect.Height - 10)
End If
End Sub

当我执行上面的代码时,组合框Combobox1显示仅带有颜色名称的项目。彩色矩形不会显示在我上面链接的项目中。
是什么原因导致矩形不显示?
我尝试将Combobox1DrawMode设置为所有可用的绘图模式。什么都没用。

请注意,我已经使用 Telerik 的代码转换器将 c# 代码转换为 vb .net 代码。

Dim N As String = (CType(sender, ComboBox)).Items(e.Index).ToString()
Dim F As Font = New Font("Arial", 10, FontStyle.Regular)
Dim B As Brush = New SolidBrush(e.ForeColor)
Dim C As Color = Color.FromName(N)
Dim P As Brush = New SolidBrush(C)
Dim L As Pen = New Pen(e.ForeColor)
Dim R1 As Rectangle = New Rectangle(e.Bounds.Left + 2, e.Bounds.Top + 2, 50, e.Bounds.Height - 4)
Dim R2 As Rectangle = New Rectangle(e.Bounds.Left + 3, e.Bounds.Top + 3, 48, e.Bounds.Height - 6)
Dim R3 As Rectangle = New Rectangle(e.Bounds.Left + 2, e.Bounds.Top + 2, 50, e.Bounds.Height - 4)
Dim R4 As Rectangle = New Rectangle(e.Bounds.Left + 2, e.Bounds.Top + 2, 50, e.Bounds.Height - 4)
e.Graphics.DrawRectangle(L, R1)
e.Graphics.FillRectangle(P, R2)
e.Graphics.DrawString(N, F, B, e.Bounds.Left + 65, e.Bounds.Top + 2)
e.DrawFocusRectangle()

相关内容

  • 没有找到相关文章