Color drop down combobox control (vb.net)

Source: Internet
Author: User
Tags reflection
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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.