|
-
Apr 26th, 2013, 01:06 AM
#6
Re: ComboBox with different color Items and DropDownList Style
 Originally Posted by dunfiddlin
It's all in those e.State values!
vb.net Code:
Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem e.DrawBackground() 'GET ITEM TO DRAW If Not e.Index = -1 Then Dim myItem As ColoredComboboxItem = DirectCast(ComboBox1.Items(e.Index), ColoredComboboxItem) 'DRAW TEXT USING SPECIFIED FONT AND COLOR ListBox1.Items.Add(CInt(e.State).ToString) If e.State = 769 Or e.State = 785 Or e.State = 4881 Then e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(Color.White), e.Bounds) Else e.Graphics.DrawString(myItem.Text, e.Font, New SolidBrush(myItem.Color), e.Bounds) End If End If e.DrawFocusRectangle() End Sub
You should absolutely never be comparing a variable or property whose type is an Enum with numbers like that. Absolutely NEVER. As Edgemeal has shown, if you're interested in one specific value then you can pick that value out of an arbitrary combination regardless of what the other values are with a bitwise logic. If you're interested in specific combinations then you should specify those combinations as their Enum values, not as numbers. To combine two Enum values you use the bitwise Or operator. Or is to combine, And is to test, Xor is to toggle, And Not is to remove.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|