Results 1 to 4 of 4

Thread: Combobox drawing autocomplete section

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    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:
    1. Private Sub cbdept_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cbdept.DrawItem
    2.         Dim c As System.Drawing.Color
    3.         Dim dcomb As ComboBox
    4.         Try
    5.             dcomb = CType(sender, ComboBox)
    6.  
    7.             'Sometimes calls this procedure with an index of -1
    8.             If e.Index < 0 Then
    9.                 e.DrawBackground()
    10.                 e.DrawFocusRectangle()
    11.             Else
    12.  
    13.                 'Determine colour to use
    14.  
    15.                 Select Case CType(dcomb.Items.Item(e.Index), DataRowView).Item(0).ToString
    16.                     Case "sales"
    17.                         c = Color.FromName("Blue")
    18.                     Case "IT"
    19.                         c = Color.FromName("Green")
    20.                     Case "Accounts"
    21.                         c = Color.FromName("Orange")
    22.                     Case "Red"
    23.                         c = Color.FromName("Red")
    24.                     Case Else
    25.                         c = Color.FromName("Red")
    26.                 End Select
    27.  
    28.                 'Carry out necessary procedures
    29.                 e.DrawBackground()
    30.                 e.DrawFocusRectangle()
    31.  
    32.                 'Create rectangle, fill it with desired colour, and add text
    33.                 e.Graphics.DrawRectangle(New Pen(c), New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
    34.                 e.Graphics.FillRectangle(New SolidBrush(c), New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
    35.                 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))
    36.  
    37.  
    38.             End If
    39.      
    40.    
    41.         Catch ex As Exception
    42.             MsgBox(ex.ToString)
    43.         End Try
    44.     End Sub

    Not sure how to do the same thing with the autocomplete section though.
    Any help would be apreciated.

    Cheers

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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 )
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    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.

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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
    Last edited by chris128; May 29th, 2009 at 10:34 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width