ComboBox Control | dropdown
Not long ago, a friend posted, looking for color selection of the ComboBox method of production, after testing, sorted out, for reference only.
(Note: If there is a reference, please indicate the source and author)
--Min (2005/08/06 Dongguan)
private Sub filllistboxwithcolors ()
Me.ComboBox1.DrawMode = Drawmode.ownerdrawfixed
Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
Me.ComboBox1.ItemHeight =
' Avoid blinking beginupdate
Me.ComboBox1.BeginUpdate ()
ComboBox1.Items.Clear ()
Dim Pi as Reflection.propertyinfo
for each pi in GetType (Color). GetProperties (Reflection.BindingFlags.Public Or Reflection.BindingFlags.Static)
Me.ComboBox1.Items.Add (pi. Name)
Next
Combobox1.endupdate ()
End Sub
Private Sub Combobox1_drawitem (ByVal sender as Object, ByVal e as System.Windows.Forms.DrawItemEventArgs) Handles Combobo X1. DrawItem
If E.index < 0 Then Exit Sub
Dim rect as Rectangle = E.bounds ' border of each item
' Draw the item to display a highlighted background if it is selected, otherwise white
If E.state and drawitemstate.selected Then
E.graphics.fillrectangle (Systembrushes.highlight, rect)
Else
E.graphics.fillrectangle (Systembrushes.window, rect)
End If
Dim colorname as String = Combobox1.items (e.index)
Dim B as New SolidBrush (Color.fromname (colorname))
Narrows selected area ()
rect. Inflate ( -16,-2)
' Fill color (color corresponding to text)
E.graphics.fillrectangle (b, rect)
Draw Border ()
E.graphics.drawrectangle (pens.black, rect)
Dim B2 as Brush
' determines the color of the text displayed ()
If CInt (B.COLOR.R) + CInt (B.COLOR.G) + CInt (B.COLOR.B) > 128 * 3 Then
&NBSP;&NBSP;&N bsp; b2 = Brushes.black
Else
b2 = brushes.white
End If
E.graphics.drawstring (ColorName, Me.ComboBox1.Font, B2, rect. X, Rect. Y
End Sub
Private Sub Form1_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
Filllistboxwithcolors ()
End Sub