Public Class FlatCombo
Inherits ComboBox
Private BorderBrush As Brush = New SolidBrush(SystemColors.Window)
Private EditBoxBrush As Brush = New SolidBrush(Me.BackColor)
Private ArrowBrush As Brush = New SolidBrush(SystemColors.ControlText)
Private DropButtonBrush As Brush = New SolidBrush(SystemColors.Control)
Private ButtonColor As Color = SystemColors.Control
Private EnabledColor As Color = MyBase.ForeColor
Private DisabledColor As Color = Color.Gray
#Region " Protected Overrides Events"
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
DropButtonBrush = New SolidBrush(Me.ButtonColor)
Select Case m.Msg
Case &HF
Dim g As Graphics = Me.CreateGraphics
'Dim p As Pen = New Pen(Color.White, 1)
g.FillRectangle(BorderBrush, Me.ClientRectangle)
'Draw the background of the dropdown button
Dim rectDropButton As Rectangle = New Rectangle(Me.Width - 13, 1, 12, Me.Height - 2)
g.FillRectangle(DropButtonBrush, rectDropButton)
'Draw the Edit Box
Dim rectEditBox As Rectangle = New Rectangle(1, 1, Me.Width - 15, Me.Height - 2)
g.FillRectangle(EditBoxBrush, rectEditBox)
'Create the path for the arrow
Dim pth As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath()
Dim TopLeft As PointF = New PointF(Me.Width - 10, (Me.Height - 5) / 2)
Dim TopRight As PointF = New PointF(Me.Width - 4, (Me.Height - 5) / 2)
Dim Bottom As PointF = New PointF(Me.Width - 7, (Me.Height + 1) / 2)
pth.AddLine(TopLeft, TopRight)
pth.AddLine(TopRight, Bottom)
g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
'Determine the arrow's color.
If Me.DroppedDown Then
ArrowBrush = New SolidBrush(Color.White)
Else
ArrowBrush = New SolidBrush(SystemColors.ControlText)
End If
'Draw the arrow
g.FillPath(ArrowBrush, pth)
g.Dispose()
Case Else
Exit Select
End Select
'MsgBox(m.Msg)
End Sub
Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
MyBase.OnLostFocus(e)
BorderBrush = New SolidBrush(SystemColors.Window)
ButtonColor = SystemColors.Control
Me.Invalidate()
End Sub
Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
MyBase.OnGotFocus(e)
BorderBrush = New SolidBrush(SystemColors.Highlight)
ButtonColor = Color.FromArgb(182, 189, 210)
Me.Invalidate()
End Sub
Protected Overrides Sub OnEnabledChanged(ByVal e As System.EventArgs)
If Me.Enabled = True Then
Me.ForeColor = EnabledColor
Me.IntegralHeight = True
EditBoxBrush = New SolidBrush(MyBase.BackColor)
Else
Me.ForeColor = DisabledColor
Me.IntegralHeight = False
EditBoxBrush = New SolidBrush(ButtonColor)
End If
Me.Invalidate()
End Sub
Protected Overrides Sub OnBackColorChanged(ByVal e As System.EventArgs)
MyBase.OnBackColorChanged(e)
EditBoxBrush = New SolidBrush(MyBase.BackColor)
Me.Invalidate()
End Sub
Protected Overrides Sub OnSelectionChangeCommitted(ByVal e As System.EventArgs)
MyBase.OnSelectionChangeCommitted(e)
BorderBrush = New SolidBrush(SystemColors.Window)
ButtonColor = SystemColors.Control
Me.Invalidate()
End Sub
'Auto complete
Private m_LastValue As String = ""
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim FindString As String
Select Case Asc(e.KeyChar)
Case Keys.Escape, Keys.Back
Text = ""
Case Else
Dim txtLen As Integer = Text.Length
Dim As Integer = Me.FindString(Text)
If <> -1 Then
Text = Items( )
SelectionStart = txtLen
SelectionLength = Text.Length
m_LastValue = Text
Else
Text = m_LastValue
SelectionStart = 0
SelectionLength = m_LastValue.Length
End If
End Select
e.Handled = True
End Sub
#End Region
#Region "Mouse Overrides Events"
Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
MyBase.OnMouseEnter(e)
BorderBrush = New SolidBrush(SystemColors.Highlight)
ButtonColor = Color.FromArgb(182, 189, 210)
Me.Invalidate()
End Sub
Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
MyBase.OnMouseLeave(e)
If Me.Focused Then Exit Sub
BorderBrush = New SolidBrush(SystemColors.Window)
ButtonColor = SystemColors.Control
Me.Invalidate()
End Sub
Protected Overrides Sub OnMouseHover(ByVal e As System.EventArgs)
MyBase.OnMouseHover(e)
BorderBrush = New SolidBrush(SystemColors.Highlight)
ButtonColor = Color.FromArgb(182, 189, 210)
Me.Invalidate()
End Sub
#End Region
End Class