Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

I'm altering a ComboBox control I found online and am having a problem displaying the selected item in the control's editbox. Here's what I have so far:

 

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)

       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
   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)
       Me.Text = MyBase.SelectedItem
       Me.Refresh()
   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

 

Thanks

Dan

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

  • *Experts*
Posted

Hi Mutant,

 

Sorry for not stating this earlier, it displays correctly except in Drop Down List style. Understanding that in this style the control no longer has a "text" function, how can the selected item be displayed in the editbox ?

 

Thanks

Dan

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

  • 2 months later...
Posted

I created a flat combo just like this one. We probably got the code from the same place. I had to add code when painting the control to draw the text in the box if the style was set DropDownList.

 

                  If Me.DropDownStyle = ComboBoxStyle.DropDownList Then
                       Dim rf As RectangleF = New RectangleF(Me.ClientRectangle.X, Me.ClientRectangle.Y, Me.ClientRectangle.Width, Me.ClientRectangle.Height)
                       Dim sf As StringFormat = New StringFormat
                       sf.Alignment = StringAlignment.Near
                       sf.LineAlignment = StringAlignment.Center
                       g.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), rf, sf)
                  End If

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...