Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / Visual Basic
Article

Flat ComboBox with MS Office XP/2003 style support

Rate me:
Please Sign up or sign in to vote.
4.79/5 (43 votes)
1 Dec 2005CPOL1 min read 177K   1.2K   77   41
An article on creating a flat combobox using VB.NET.

Image 1

Introduction

First of all, on the Internet, a lot of examples of flat combo boxes can be found. I tried to make a very simple one with a lot of functionality. On the screenshot above, you can see the functions that I have integrated: support for big fonts (the arrow will be centered), RTL (right to left) support, support for two dropdown styles (simple is not supported yet), and of course the two styles.

The different states

 
Office XP
Office 2003
normal

Image 2

Image 3

focused

Image 4

Image 5

disabled

Image 6

Image 7

dropeddown

Image 8

Image 9

VB
'Enum with all the possible states
Enum states
    normal
    focused
    dropeddown
    disabled
End Enum
'Variable to save the current state
Dim state As states = states.normal
    
Function UpdateState()
    'save the current state
    Dim temp As states = state
    '
    If Me.Enabled Then
        If Me.DroppedDown Then
            Me.state = states.dropeddown
        Else
            If ClientRectangle.Contains(_
                    PointToClient(Me.MousePosition)) Then
                Me.state = states.focused
            ElseIf Me.Focused Then
                Me.state = states.focused
            Else
                Me.state = states.normal
            End If
        End If
    Else
        Me.state = states.disabled
    End If
    'only redraw if the state has changed
    If state <> temp Then
        Me.Invalidate()
    End If
End Function

Using the code

FlatComboBox is a UserControl based on Windows.Forms.ComboBox:

VB
Imports System.Drawing.Drawing2D

Public Class FlatComboBox
    Inherits System.Windows.Forms.ComboBox

    '...

End Class

Reacting on events (mousemove etc.) is done by overriding WndProc:

VB
Protected Overrides Sub WndProc(ByRef m As _
                       System.Windows.Forms.Message)
        MyBase.WndProc(m)
        Select Case m.Msg

            Case &HF
                'WM_PAINT

                '"simple" is not currently supported
                If Me.DropDownStyle = _
                           ComboBoxStyle.Simple Then Exit Sub

                '==========START DRAWING===========
                g = Me.CreateGraphics
                'clear everything
                If Me.Enabled Then
                    g.Clear(Color.White)
                Else
                    g.Clear(Color.FromName("control"))
                End If
                'call the drawing functions
                DrawButton(g)
                DrawArrow(g)
                DrawBorder(g)
                DrawText(g)
                '===========STOP DRAWING============

            Case 7, 8, &H7, &H8, &H200, &H2A3
                'CMB_DROPDOWN, CMB_CLOSEUP, WM_SETFOCUS, 
                'WM_KILLFOCUS, WM_MOUSEMOVE,  
                'WM_MOUSELEAVE (if you move the mouse fast over
                'the combobox, mouseleave doesn't always react)

                UpdateState()
        End Select
    End Sub

To change the style (XP/2003), I've added a Public property:

VB
'Property to let the user change the style
    Public Property FlatComboStyle() As styles
        Get
            Return style
        End Get
        Set(ByVal Value As styles)
            style = Value
        End Set
    End Property

Points of interest

In the combo box, there are two parts (the control itself and a kind of textbox). If the DropDownStyle is set to DropDownList, I need to draw the text manually because then there is no textbox. Because the event MouseLeave doesn't always react, a timer refreshes the control every 20 ms. Just have a look at the code and you will see how easy it is!

Thanks to

Update

  • Bug fix: Timer will be disabled while disposing! (See reactions below for more information.)
  • First revision: Some big and small bugs are now fixed! (See reactions below for more information.)
  • Second revision: 'Function' replaced by 'Private Sub' and others, and added a VB 2005 (.NET 2.0) version of the demo.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionStop flickering Pin
Member 1110782325-Nov-18 23:00
Member 1110782325-Nov-18 23:00 
NewsAdvanced list box and combo box Pin
brett5630-Nov-05 5:41
brett5630-Nov-05 5:41 
GeneralRe: Advanced list box and combo box Pin
Wouter Devinck30-Nov-05 6:03
Wouter Devinck30-Nov-05 6:03 
NewsRe: Advanced list box and combo box Pin
brett5630-Nov-05 6:26
brett5630-Nov-05 6:26 
QuestionMemory Leak? Pin
JerryTanski23-Nov-05 7:07
JerryTanski23-Nov-05 7:07 
AnswerRe: Memory Leak? Pin
JerryTanski23-Nov-05 7:16
JerryTanski23-Nov-05 7:16 
GeneralRe: Memory Leak? Pin
Wouter Devinck23-Nov-05 8:45
Wouter Devinck23-Nov-05 8:45 
GeneralRe: Memory Leak? Pin
legabertrand13-Dec-05 5:33
legabertrand13-Dec-05 5:33 
GeneralRe: Memory Leak? Pin
Wouter Devinck13-Dec-05 7:06
Wouter Devinck13-Dec-05 7:06 
QuestionFunctions? Pin
DodgerSharp14-Nov-05 5:10
DodgerSharp14-Nov-05 5:10 
AnswerRe: Functions? Pin
Wouter Devinck14-Nov-05 8:01
Wouter Devinck14-Nov-05 8:01 
GeneralRe: Functions? Pin
DodgerSharp14-Nov-05 9:13
DodgerSharp14-Nov-05 9:13 
GeneralPermission Pin
DodgerSharp14-Nov-05 4:53
DodgerSharp14-Nov-05 4:53 
GeneralRe: Permission Pin
Wouter Devinck14-Nov-05 8:14
Wouter Devinck14-Nov-05 8:14 
GeneralRe: Permission Pin
DodgerSharp14-Nov-05 9:11
DodgerSharp14-Nov-05 9:11 
Sorry but the published is a dummy mailbox. I cannot read.
Just to avoid spam.
Regards,
GeneralRe: Permission Pin
Wouter Devinck15-Nov-05 6:07
Wouter Devinck15-Nov-05 6:07 
GeneralRe: Permission Pin
DodgerSharp15-Nov-05 7:06
DodgerSharp15-Nov-05 7:06 
GeneralRe: Permission Pin
Wouter Devinck15-Nov-05 7:44
Wouter Devinck15-Nov-05 7:44 
AnswerRe: Permission Pin
DodgerSharp18-Nov-05 19:56
DodgerSharp18-Nov-05 19:56 
GeneralNot Support DataSource Pin
Member 222617913-Nov-05 16:29
Member 222617913-Nov-05 16:29 
GeneralRe: Not Support DataSource Pin
Wouter Devinck14-Nov-05 7:54
Wouter Devinck14-Nov-05 7:54 
GeneralGreat work, A problem with large strings Pin
SotirisF7-Nov-05 4:53
SotirisF7-Nov-05 4:53 
NewsRe: Great work, A problem with large strings Pin
Wouter Devinck8-Nov-05 9:03
Wouter Devinck8-Nov-05 9:03 
GeneralRe: Great work, A problem with large strings Pin
Wouter Devinck9-Nov-05 4:58
Wouter Devinck9-Nov-05 4:58 
GeneralRe: Great work, A problem with large strings Pin
SotirisF9-Nov-05 6:07
SotirisF9-Nov-05 6:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.