Combobox drawing autocomplete section
Hi,
Am hoping someone can help. I want to change the colour of the autocomplete section of a combo box depending on the value of the item. I found the following code to do this for the main drop down section like so.
; Code:
Private Sub cbdept_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cbdept.DrawItem
Dim c As System.Drawing.Color
Dim dcomb As ComboBox
Try
dcomb = CType(sender, ComboBox)
'Sometimes calls this procedure with an index of -1
If e.Index < 0 Then
e.DrawBackground()
e.DrawFocusRectangle()
Else
'Determine colour to use
Select Case CType(dcomb.Items.Item(e.Index), DataRowView).Item(0).ToString
Case "sales"
c = Color.FromName("Blue")
Case "IT"
c = Color.FromName("Green")
Case "Accounts"
c = Color.FromName("Orange")
Case "Red"
c = Color.FromName("Red")
Case Else
c = Color.FromName("Red")
End Select
'Carry out necessary procedures
e.DrawBackground()
e.DrawFocusRectangle()
'Create rectangle, fill it with desired colour, and add text
e.Graphics.DrawRectangle(New Pen(c), New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.Graphics.FillRectangle(New SolidBrush(c), New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.Graphics.DrawString(CType(dcomb.Items.Item(e.Index), DataRowView).Item(0).ToString, dcomb.Font, New SolidBrush(Color.Black), New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Not sure how to do the same thing with the autocomplete section though.
Any help would be apreciated.
Cheers
Re: Combobox drawing autocomplete section
I dont think you can "OwnerDraw" the autocomplete section so the only way I can think of doing it is by turning the autocomplete function off and then basicallly writing your own. So you would handle the TextChanged event or whatever its called and then just basically draw a rectangle containing lists of the autocomplete options... I think it would be more work than its worth personally though :)
On the other hand you could use WPF and it would take about 30 seconds :P
(I know I know, I plug WPF too much :) )
Re: Combobox drawing autocomplete section
Um, no sure I want to build it from scratch. Think those pesky users will just have to live without having pretty colours.
Will have a look into wpf when I get some time though, as we want to revamp our fromt end at some point and make it all super petty so am guessing this will be the way to go.
Anyway thanks for your help.
Re: Combobox drawing autocomplete section
Yep, super pretty front ends are WPFs strong point :) If you want to see an amazing example of what WPF can do with business applications, watch this video: http://www.dnrtv.com/default.aspx?showNum=115