Ansis Māliņš
Ansis Māliņš

Reputation: 1704

Customizing the appearance of a Windows Forms combo box

Here's what I do:

Public Class ComboBox
    Inherits System.Windows.Forms.ComboBox

    Public Sub New()
        SetStyle(ControlStyles.OptimizedDoubleBuffer _
        Or ControlStyles.UserPaint, True)
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        e.Graphics.FillRectangle(gradient, ClientRectangle)
        // The preceding line is a simplification of about 50 lines of code.

        If Not String.IsNullOrEmpty(Text) Then
            Dim rect As New Rectangle(2, 0, Width - 2, Height)

            Using format As New StringFormat()
                format.LineAlignment = StringAlignment.Center

                Using brush As New SolidBrush(ForeColor)
                    e.Graphics.DrawString(Text, Font, brush, rect, format)
                End Using
            End Using
        End If

        e.Graphics.FillPolygon(Brushes.Black, New Point() { _
        New Point(Width - 5, Height \ 2 - 1), _
        New Point(Width - 12, Height \ 2 - 1), _
        New Point(Width - 9, Height \ 2 + 3)})
    End Sub
End Class

I have two problems:

  1. Its height is always 24.
  2. The drop down list is rendered with an ugly Windows 3.1 font.

Upvotes: 0

Views: 1299

Answers (3)

ezolotko
ezolotko

Reputation: 1783

To fix the problem with the font, set the UserPaint style in OnControlCreated instead of constructor, like this:

    protected override void OnCreateControl()
    {
        base.OnCreateControl();
        SetStyle(ControlStyles.UserPaint, true);
    }

Upvotes: 1

Sisyphus
Sisyphus

Reputation: 4231

The height problem is almost for sure because you are inheriting from a ComboBox and then using standard API calls to draw most of it, and when Windows draws a combobox the height is fixed by the font size and any changes to a Height property are ignored.

Similarly, Windows is using a default font because you are not changing it or setting it up somewhere in the API call.

Comboboxes are not exactly friendly when it comes to inheriting and making minor modifications. You may have to provide your own custom implementation entirely, that is not trivial either. You may not be able to find 3rd party custom controls that render what you want either for that matter. Your company made a mistake going on an artist's rendition without practical considerations of available controls etc. Sorry ...

Upvotes: 0

Gabriel Magana
Gabriel Magana

Reputation: 4536

Your problem is obviously in the "Full of Code" part. I suggest you solve one problem at a time:

1) The height problem: Could it be that you have the height set to 24 and if you change teh height the control will resize accordingly? Have you looked at the Control.PreferredSize property? Look here for this: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.preferredsize.aspx

2) The font problem. This is impossible to diagnose without looking at the code. All I can say is to make sure you are properly drawing the drop down elements with whatever font you want to use.

Upvotes: 0

Related Questions