[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Flat Combobox and font ...

Posted on 2011-10-12
Medium Priority
Last Modified: 2012-05-12
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

Question by:Kinger247
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 1000 total points
ID: 36957345
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

CodeCruiser earned 1000 total points
ID: 36960908
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

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

872 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