Home Articles Projects Curriculum Vitae Blog
 
  Articles » CodeProject.com » ComboBox zoals in Office XP/2003

This is a Dutch translation. The original (English) version is available at the website codeproject.com (click here).


Klik hier voor een vergroting

Inleiding

Op internet zijn veel voorbeelden van vlakke comboboxen te vinden. Ik heb geprobeerd om een erg simpele versie te maken, met toch voldoende functies. Op de screenshot hierboven kun je deze functies zien, namelijk: ondersteuning voor grote lettertypes (het pijltje zal worden gecentreerd), ondersteuning voor RTL (right to left), ondersteuning voor twee van de drie "dropdown styles" (simple wordt niet ondersteund) en natuurlijk ook de twee verschillende stijlen.

De verschillende uitzichten

  Office XP Office 2003
normal
focused
disabled
droppeddown

Hieronder een fragment uit de code, klik in de kolom rechts op 'Download broncode' voor de volledige broncode.
'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

Interessante gegevens

De combobox bestaat uit twee delen, de eigenlijke control en een soort textbox. Als DropDownStyle op DropDownList staat, moet je de tekst manueel tekenen omdat er dan geen sprake is van een textbox. Omdat het MouseLeave event niet altijd reageert, heb ik een timer voorzien die de control elke 20 milliseconden ververst.
Downloads
Sourcecode
Demo (.net 1.1)
Demo (.net 2.0)

Links
www.codeproject.com
Original version
My profile

Awards
Competition winner
augustus 2005

History
Posted: 17 Aug 2005
Updated: 1 Dec 2005