Flat Combobox and font ...

Hi all,

I need to create a flat style combo  in vb.net. Here is mode code so far below, it works ok but I can’t figure out how to set the font ??

Any ideas ??

Thanks in advance,

Imports System.Drawing.Drawing2D

Public Class ComboBox2
  Inherits ComboBox

  Public Property BorderColour As Color
      Return _BorderColour
    End Get
    Set(value As Color)
      _BorderColour = value
    End Set
  End Property

  Private _BorderColour As Color = Color.FromArgb(64, 64, 64)
  Private _ButtonColour As Color = Color.LightGray
  Private _Arrowcolour As Color = Color.Black

  Public Sub New()
    Me.SetStyle(ControlStyles.UserPaint, True)
    Me.SetStyle(ControlStyles.ResizeRedraw, True)
    Me.SetStyle(ControlStyles.DoubleBuffer, True)
  End Sub

  Private ButtonSize As Size = Nothing
  Private ButtonRectangle As Rectangle = Nothing
  Private ButtonPath As GraphicsPath = Nothing

  Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)

    With e.Graphics
      .SmoothingMode = SmoothingMode.HighSpeed
      .InterpolationMode = InterpolationMode.High
      .DrawRectangle(New Pen(New SolidBrush(_BorderColour), 1), New Rectangle(0, 0, Me.Width - 1, Me.Height - 1))
      .FillRectangle(New SolidBrush(_ButtonColour), ButtonRectangle)
      .DrawLine(New Pen(New SolidBrush(_BorderColour), 1), ButtonRectangle.Left, 0, ButtonRectangle.Left, Me.Height)
      .SmoothingMode = SmoothingMode.HighSpeed
      .InterpolationMode = InterpolationMode.High
    End With

    Dim ArrowSize As New Size(9, 5)
    Dim path As GraphicsPath = New GraphicsPath
    Dim iLeft As Integer = ButtonRectangle.Left + ((ButtonSize.Width / 2) - (ArrowSize.Width / 2))
    Dim iTop As Integer = ButtonRectangle.Top + ((ButtonSize.Height / 2) - (ArrowSize.Height / 2))
    path.AddPolygon(New Point() {New Point(iLeft, 0 + iTop), New Point(ArrowSize.Width + iLeft, iTop), New Point((ArrowSize.Width / 2) + iLeft, ArrowSize.Height + iTop), New Point(iLeft, 0 + iTop)})
    e.Graphics.FillPath(New SolidBrush(Color.Black), path)
  End Sub

  Protected Overrides Sub OnResize(e As System.EventArgs)
    ButtonSize = New Size(19, Me.Height - 2)
    ButtonRectangle = New Rectangle(Me.Width - ButtonSize.Width - 1, 1, ButtonSize.Width, ButtonSize.Height)
  End Sub

End Class

Open in new window

LVL 10
Who is Participating?
Jacques Bourgeois (James Burger)PresidentCommented:
The text is drawn when you call MyBase.OnPaint.

You then call FillRectangle to fill the control, overwriting the text that is already drawn.

Move the code to fill the background to OnPaintBackground.
You can remove the MyBase.OnPaint(e) and draw the string yourself using Graphics.DrawString which also takes a Font parameter


Kinger247Author Commented:
I'll be coming back to this one next week.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.