Friday, November 21, 2008
Dynamic Flat DropDownListbox, the easy way!

The mouse is off of it...

The mouse is over it...

You have control. Make it simple and tasteful ...

Or really ugly...



Download your demo here. Just 14k for the demo harness and the control dll.

Hope it helps.

UserControl Code

Public Class svFlatDropDownCombo
    Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'svFlatDropDownCombo overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lbDropDown As System.Windows.Forms.ListBox
    Friend WithEvents lblButton As System.Windows.Forms.Label
    Friend WithEvents lblTextArea As System.Windows.Forms.Label
    Private Sub InitializeComponent()
        Me.lbDropDown = New System.Windows.Forms.ListBox
        Me.lblButton = New System.Windows.Forms.Label
        Me.lblTextArea = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'lbDropDown
        '
        Me.lbDropDown.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.lbDropDown.Location = New System.Drawing.Point(8, 48)
        Me.lbDropDown.Name = "lbDropDown"
        Me.lbDropDown.Size = New System.Drawing.Size(120, 91)
        Me.lbDropDown.TabIndex = 0
        Me.lbDropDown.Visible = False
        '
        'lblButton
        '
        Me.lblButton.BackColor = System.Drawing.SystemColors.Control
        Me.lblButton.Font = New System.Drawing.Font("Marlett", 8.25!, _
	   System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, _
           CType(2, Byte))
        Me.lblButton.Location = New System.Drawing.Point(136, 0)
        Me.lblButton.Name = "lblButton"
        Me.lblButton.Size = New System.Drawing.Size(16, 23)
        Me.lblButton.TabIndex = 1
        Me.lblButton.Text = "u"
        Me.lblButton.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'lblTextArea
        '
        Me.lblTextArea.BackColor = System.Drawing.Color.White
        Me.lblTextArea.Name = "lblTextArea"
        Me.lblTextArea.TabIndex = 2
        Me.lblTextArea.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'svFlatDropDownCombo
        '
        Me.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), _
	   CType(192, Byte), CType(192, Byte))
        Me.Controls.AddRange(New System.Windows.Forms.Control() _
           {Me.lblTextArea, Me.lblButton, Me.lbDropDown})
        Me.Name = "svFlatDropDownCombo"
        Me.Size = New System.Drawing.Size(152, 128)
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region "Declarations"
    Private Declare Function SetParent Lib "user32" Alias "SetParent" _
	(ByVal hWndChild As Integer, _
        ByVal hWndNewParent As Integer) As Integer

    Private m_DropDownListWidth As Integer = 0

    Private m_ButtonWidth As Integer = 10
    Private m_LBBorderStyle As BorderStyle = BorderStyle.FixedSingle

    Private m_ColorButtonBackFocused As Color = Color.LightSteelBlue
    Private m_ColorButtonBackUnFocused As Color = Color.FromName("Control")

    Private m_ColorOpenButtonFore As Color = Color.White
    Private m_ColorOpenButtonBack As Color = Color.DarkBlue
    Private m_ColorClosedButtonFore As Color = Color.Black

    Private m_ColorBorderFocused As Color = Color.Black
    Private m_ColorBorderUnFocused As Color = Color.White

    Private m_ColorTextAreaBackFocused As Color = Color.LightYellow
    Private m_ColorTextAreaBackUnFocused As Color = Color.White

    Private m_ColorTextAreaForeFocused As Color = Color.Black
    Private m_ColorTextAreaForeUnFocused As Color = Color.Black

    Private m_Font As Font = New Font("Tahoma", 8)

#End Region

#Region "Public Properties"

    Public Property ColorTextAreaForeUnFocused() As Color
        Get
            Return m_ColorTextAreaForeUnFocused
        End Get

        Set(ByVal Value As Color)
            m_ColorTextAreaForeUnFocused = Value
            InitGui()
        End Set

    End Property

    Public Property ColorTextAreaForeFocused() As Color
        Get
            Return m_ColorTextAreaForeFocused
        End Get

        Set(ByVal Value As Color)
            m_ColorTextAreaForeFocused = Value
            InitGui()
        End Set

    End Property

    Public Property ColorTextAreaBackUnFocused() As Color
        Get
            Return m_ColorTextAreaBackUnFocused
        End Get

        Set(ByVal Value As Color)
            m_ColorTextAreaBackUnFocused = Value
            InitGui()
        End Set

    End Property

    Public Property ColorTextAreaBackFocused() As Color
        Get
            Return m_ColorTextAreaBackFocused
        End Get

        Set(ByVal Value As Color)
            m_ColorTextAreaBackFocused = Value
            InitGui()
        End Set

    End Property

    Public Property ColorBorderUnFocused() As Color
        Get
            Return m_ColorBorderUnFocused
        End Get

        Set(ByVal Value As Color)
            m_ColorBorderUnFocused = Value
            InitGui()
        End Set

    End Property

    Public Property ColorBorderFocused() As Color
        Get
            Return m_ColorBorderFocused
        End Get

        Set(ByVal Value As Color)
            m_ColorBorderFocused = Value
        End Set

    End Property

    Public Property ColorButtonForeClosed() As Color
        Get
            Return m_ColorClosedButtonFore
        End Get

        Set(ByVal Value As Color)
            m_ColorClosedButtonFore = Value
            InitGui()
        End Set

    End Property

    Public Property ColorButtonForeOpen() As Color
        Get
            Return m_ColorOpenButtonFore
        End Get

        Set(ByVal Value As Color)
            m_ColorOpenButtonFore = Value
        End Set

    End Property

    Public Property ColorButtonBackOpen() As Color
        Get
            Return m_ColorOpenButtonBack
        End Get

        Set(ByVal Value As Color)
            m_ColorOpenButtonBack = Value
        End Set

    End Property

    Public Property ColorButtonBackFocused() As Color
        Get
            Return m_ColorButtonBackFocused
        End Get

        Set(ByVal Value As Color)
            m_ColorButtonBackFocused = Value
        End Set

    End Property

    Public Property ColorButtonBackUnFocused() As Color
        Get
            Return m_ColorButtonBackUnFocused
        End Get

        Set(ByVal Value As Color)
            m_ColorButtonBackUnFocused = Value
            InitGui()
        End Set

    End Property

    Public Property ButtonWidth() As Integer
        Get
            Return m_ButtonWidth
        End Get

        Set(ByVal Value As Integer)
            m_ButtonWidth = Value
            InitGui()
        End Set

    End Property

    Public Property DropDownWidth() As Integer
        Get
            Return m_DropDownListWidth
        End Get

        Set(ByVal Value As Integer)

            m_DropDownListWidth = Value
            InitGui()
        End Set

    End Property

    Public Property DropDownBorderStyle() As BorderStyle
        Get
            Return m_LBBorderStyle
        End Get

        Set(ByVal Value As BorderStyle)
            m_LBBorderStyle = Value
            InitGui()
        End Set

    End Property

    Public ReadOnly Property DropBoxItems() As _
	ListBox.ObjectCollection
        Get
            Return lbDropDown.Items
        End Get

    End Property

    Public ReadOnly Property DropBoxSelectedItems() As _
	ListBox.SelectedObjectCollection
        Get
            Return lbDropDown.SelectedItems
        End Get

    End Property

    Public Sub DropBoxItemsClear()
        lbDropDown.Items.Clear()
        lblTextArea.Text = ""

    End Sub

    Public Overrides Property Font() As Font
        Get
            Return m_Font
        End Get

        Set(ByVal Value As Font)
            m_Font = Value
            InitGui()
        End Set

    End Property

    Public Overrides Property Text() As String
        Get
            Return lblTextArea.Text
        End Get

        Set(ByVal Value As String)
            'can only be set to an available value
            If lbDropDown.Items.Contains(Value) Then
                lbDropDown.Text = Value
                lblTextArea.Text = lbDropDown.Text
            End If

        End Set

    End Property

#End Region

#Region "Private Methods"

    Private Sub AddParentMoveHandlers(ByVal c As Control)
        AddHandler c.Move, AddressOf MyParents_Move
        AddHandler c.Resize, AddressOf MyParents_Move
        If c Is Me.ParentForm Then
            Exit Sub

        Else
            AddParentMoveHandlers(c.Parent)

        End If

    End Sub

    Private Sub GetCurrentX(ByVal c As Control, ByRef iOffset As Integer)
        If c.Parent Is Me.ParentForm Then Exit Sub
        iOffset += c.Parent.Left
        GetCurrentX(c.Parent, iOffset)

    End Sub

    Private Sub InitGui()
        lblButton.Width = m_ButtonWidth
        lblButton.Top = 1
        lblButton.Left = (MyBase.Width - lblButton.Width) - 1
        lblTextArea.Left = 1
        lblTextArea.Top = 1
        lblTextArea.Width = (MyBase.Width - lblButton.Width) - 3
        lblTextArea.Height = MyBase.Height - 2
        lblButton.Height = MyBase.Height - 2
        lbDropDown.BorderStyle = m_LBBorderStyle
        lblButton.BackColor = m_ColorButtonBackUnFocused
        lblButton.ForeColor = m_ColorClosedButtonFore
        lblTextArea.ForeColor = m_ColorTextAreaForeUnFocused
        lbDropDown.ForeColor = m_ColorTextAreaForeFocused
        lblTextArea.BackColor = m_ColorTextAreaBackUnFocused
        lbDropDown.BackColor = m_ColorTextAreaBackFocused

        If m_DropDownListWidth = 0 Then
            lbDropDown.Width = MyBase.Width
        Else
            lbDropDown.Width = m_DropDownListWidth
        End If
        lblTextArea.Font = m_Font
        lbDropDown.Font = m_Font

        MyBase.BackColor = m_ColorBorderUnFocused

    End Sub

    Private Sub SetPlacement()

        Dim ioffset As Integer = Me.Left
        GetCurrentX(Me, ioffset)

        With lbDropDown
            .Top = Me.Top + Me.Height
            .Left = ioffset
            If m_DropDownListWidth = 0 Then
                .Width = Me.Width
            Else
                .Width = m_DropDownListWidth
            End If
            lbDropDown.Visible = True
            SetParent(lbDropDown.Handle.ToInt32, _
	       MyBase.ParentForm.Handle.ToInt32)
            lbDropDown.Focus()

        End With
        lblTextArea.BackColor = m_ColorTextAreaBackFocused
        lblTextArea.ForeColor = m_ColorTextAreaForeFocused
        lblButton.BackColor = m_ColorOpenButtonBack
        lblButton.ForeColor = m_ColorOpenButtonFore

    End Sub

#End Region

#Region "Event Handlers"
    Private Sub MyParents_Move(ByVal sender As Object, _
        ByVal e As System.EventArgs)

        lbDropDown.Visible = False
        lblButton.BackColor = m_ColorButtonBackUnFocused  
        lblButton.ForeColor = m_ColorClosedButtonFore
        lblTextArea.BackColor = m_ColorTextAreaBackUnFocused
        lblTextArea.ForeColor = m_ColorTextAreaForeUnFocused
        MyBase.BackColor = m_ColorBorderUnFocused

    End Sub

#End Region

#Region "Base Handlers"

    Private Sub Labels_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles lblButton.Click, lblTextArea.Click

        If lbDropDown.Visible Then
            lblButton.BackColor = m_ColorButtonBackUnFocused
            lblButton.ForeColor = m_ColorClosedButtonFore
            lbDropDown.Visible = False
        Else
            SetPlacement()
        End If

    End Sub

    Private Sub Labels_MouseEnter(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles lblButton.MouseEnter, lblTextArea.MouseEnter, _
        MyBase.Enter
        If Not lbDropDown.Visible Then
            lblButton.BackColor = m_ColorButtonBackFocused
            lblButton.ForeColor = m_ColorClosedButtonFore
        Else
            lblButton.BackColor = m_ColorOpenButtonBack
            lblButton.ForeColor = m_ColorOpenButtonFore
        End If

        lblTextArea.BackColor = m_ColorTextAreaBackFocused
        lblTextArea.ForeColor = m_ColorTextAreaForeFocused
        MyBase.BackColor = m_ColorBorderFocused

    End Sub

    Private Sub Labels_MouseLeave(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles lblButton.MouseLeave, lblTextArea.MouseLeave, MyBase.Leave

        If Not lbDropDown.Visible Then
            lblTextArea.BackColor = m_ColorTextAreaBackUnFocused
            lblTextArea.ForeColor = m_ColorTextAreaForeUnFocused
            lblButton.BackColor = m_ColorButtonBackUnFocused
            lblButton.ForeColor = m_ColorClosedButtonFore
            MyBase.BackColor = m_ColorBorderUnFocused
        End If

    End Sub

    Private Sub lbDropDown_SelectedIndexChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles lbDropDown.SelectedIndexChanged

        lblTextArea.Text = lbDropDown.Text
        MyBase.Focus()

    End Sub

    Private Sub lbDropDown_Select()
        lblTextArea.Text = lbDropDown.Text
        lbDropDown.Visible = False
        lblButton.BackColor = m_ColorButtonBackFocused
        lblButton.ForeColor = m_ColorClosedButtonFore
        MyBase.Focus()

    End Sub

    Private Sub lbDropDown_Leave(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles lbDropDown.Leave

        If lbDropDown.Visible Then
            lbDropDown.Visible = False
        End If

    End Sub

    Private Sub lbDropDown_KeyUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.KeyEventArgs) _
        Handles lbDropDown.KeyUp

        If e.KeyCode = Keys.Return Then
            lbDropDown_Select()
        End If

    End Sub

    Private Sub lbDropDown_MouseUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles lbDropDown.MouseUp

        lbDropDown_Select()

    End Sub

    Private Sub svFlatDropDownCombo_KeyUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.KeyEventArgs) _
        Handles MyBase.KeyUp

        If e.KeyCode = Keys.Return Then
            If lbDropDown.Visible Then
                lblButton.BackColor = m_ColorButtonBackUnFocused
                lblButton.ForeColor = m_ColorClosedButtonFore
                lbDropDown.Visible = False
            Else
                SetPlacement()
            End If
        End If

    End Sub

    Private Sub svFlatDropDownCombo_Leave(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Leave

        If lbDropDown.Visible Then
            lbDropDown.Visible = False
            lblButton.BackColor = m_ColorButtonBackUnFocused
            lblButton.ForeColor = m_ColorClosedButtonFore
            MyBase.BackColor = m_ColorBorderUnFocused
        End If

    End Sub

    Private Sub svFlatDropDownCombo_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load

        MyBase.BackColor = m_ColorBorderUnFocused

        AddParentMoveHandlers(Me)

        InitGui()

        'these don't set at initialize in the call to InitGui
        'no biggie
        Me.ColorTextAreaForeFocused = Color.Black
        Me.ColorTextAreaForeUnFocused = Color.Black

        'MsgBox("Hope This Helps!" & vbCrLf & vbCrLf & _
        '    "Robert Smith, Kirkland, WA" & vbCrLf & vbCrLf & _
        '    "www.SmithVoice.com", MsgBoxStyle.Information)

    End Sub

    Private Sub svFlatDropDownCombo_Resize(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Resize

        InitGui()

    End Sub


#End Region

End Class

Robert Smith
Kirkland, WA

(added January 1st, 2003. Happy New Year!)


Print  

pagecomment
  Add Comment



Submit Comment
  View Ratings
50.00%0
40.00%0
30.00%0
20.00%0
10.00%0

Number of Comments 0 , Average of Ratings
  View Comments
No comment.


Privacy Statement  |  Terms Of Use
Copyright 2008 by Robert C. Smith