Flat Combobox and font ...

Posted on 2011-10-12
Last Modified: 2012-05-12
Hi all,

I need to create a flat style combo  in 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

Question by:Kinger247
    LVL 40

    Accepted Solution

    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.
    LVL 83

    Assisted Solution

    You can remove the MyBase.OnPaint(e) and draw the string yourself using Graphics.DrawString which also takes a Font parameter

    LVL 10

    Author Comment

    I'll be coming back to this one next week.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now