Groups | Blog | Home
all groups > vb.net controls > may 2004 >

vb.net controls : Dropdown forecolor


Tony
5/17/2004 11:21:25 PM
XP pro/web forms/vs 2002

I'm trying to change the text color in a drop down list depending on a value
in database as follows.



MakeDrpString = tbl.Rows(GetRecords).Item("BaseURL")

If IsDBNull(tbl.Rows(GetRecords).Item("RecordComplete")) Then
DropList.ForeColor = System.Drawing.Color.Red
Else
DropList.ForeColor = System.Drawing.Color.Green
labErr.Text = "yes i was in here at least once"
End If
DropList.Items.Add(MakeDrpString)



Everything is RED and labErr shows "yes i was in here at least once"

If I comment out DropList.ForeColor = System.Drawing.Color.Red

everything is Green.

What am I doing wrong?

Thanks
Tony

Ken Tucker [MVP]
5/18/2004 10:53:09 AM
Hi,

You are going to have to use an ownerdrawn combobox to do that.
Here is a simple example.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim arData() As String = {"Full", "Three Quarter", "Half",
"Quarter", "None"}
ComboBox1.DataSource = arData
ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
End Sub

Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
Dim g As Graphics = e.Graphics
Dim s As String
Dim br As Brush = SystemBrushes.WindowText
Dim brItem As SolidBrush
Dim brBack As Brush
Dim rDraw As Rectangle
Dim bSelected As Boolean = CBool(e.State And DrawItemState.Selected)
Dim bValue As Boolean = CBool(e.State And
DrawItemState.ComboBoxEdit)

rDraw = e.Bounds
rDraw.Inflate(-1, -1)

If bSelected And Not bValue Then
brBack = Brushes.LightBlue
g.FillRectangle(Brushes.LightBlue, rDraw)
g.DrawRectangle(Pens.Blue, rDraw)
Else
brBack = Brushes.White
g.FillRectangle(brBack, e.Bounds)
End If

br = Nothing
brBack = Nothing
rDraw = Nothing

Try
s = ComboBox1.Items.Item(e.Index).ToString
Catch
s = ""
End Try

Dim x, y As Integer

x = e.Bounds.Left + 2
y = e.Bounds.Top + 1
Dim c As Color
Dim b As SolidBrush
c = Color.FromName(s)
b = New SolidBrush(c)

If s = "Half" Then
brItem = New SolidBrush(Color.Red)
Else
brItem = New SolidBrush(Color.Green)
End If

g.FillRectangle(b, x - 20, y + 2, 10, 10)
g.DrawString(s, ComboBox1.Font, brItem, x, y)
End Sub

Ken
----------------------------
[quoted text, click to view]

AddThis Social Bookmark Button