7

When ComboBox's DropDownStyle is DropDownList and DrawMode is Normal - it looks good, but when I change DrawMode to OwnerDrawFixed - it looks very bad (similar to TextBox with arrow to drop down). Is there any solution to make it look good when DrawMode isn't Normal?

looks like that: looks like that

I want it to look like that: I want it to look like that

alcohol is evil
  • 686
  • 12
  • 34

2 Answers2

2

I found solution in VB here: how-to-make-a-custom-combobox-ownerdrawfixed-looks-3d-like-the-standard-combobo Added some code for drawing Text and Arrow. It works :)

class MyComboBox: ComboBox
{
    public MyComboBox()
    {
        this.SetStyle(ControlStyles.Opaque | ControlStyles.UserPaint, true);
        Items.Add("lol");
        Items.Add("lol2");  
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (DroppedDown)
            ButtonRenderer.DrawButton(CreateGraphics(), new System.Drawing.Rectangle(ClientRectangle.X - 1, ClientRectangle.Y - 1, ClientRectangle.Width + 2, ClientRectangle.Height + 2), PushButtonState.Pressed);
        else
            ButtonRenderer.DrawButton(CreateGraphics(), new System.Drawing.Rectangle(ClientRectangle.X - 1, ClientRectangle.Y - 1, ClientRectangle.Width + 2, ClientRectangle.Height + 2), PushButtonState.Normal);
        if (SelectedIndex != -1)
        {
            Font font;
            if (SelectedItem.ToString().Equals("lol"))
                font = new Font(this.Font, FontStyle.Bold);
            else
                font = new Font(this.Font, FontStyle.Regular);
            e.Graphics.DrawString(Text, font, new SolidBrush(Color.Black), 3, 3);
        }
        if (DroppedDown)
            this.CreateGraphics().DrawImageUnscaled(new Bitmap("c:\\ArrowBlue.png"), ClientRectangle.Width - 13, ClientRectangle.Height - 12);
        else
            this.CreateGraphics().DrawImageUnscaled(new Bitmap("c:\\ArrowGray.png"), ClientRectangle.Width - 13, ClientRectangle.Height - 12);
        base.OnPaint(e);
    }

I don't know how to remove flicker when mouse is entering and leaving ComboBox. When DoubleBuffering is Enabled, ComboBox is black. But works fine for me.

Community
  • 1
  • 1
alcohol is evil
  • 686
  • 12
  • 34
  • Did you by any chance find how to remove this flickering now? – Otiel Oct 03 '11 at 12:45
  • 2
    Your frequent calls to `CreateGraphics` will leak GDI resources. You should use a single `Graphics` object wrapped in a `using()` block to ensure its `Dispose` method is called. – Dai Jul 03 '17 at 22:27
0

when you change it to OwnerDrawFixed ,you should handle drawing yourself

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
                {
                    //Wrtie your code here
     e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), this.Font, Brushes.Black,e.Bounds);
e.DrawBackground();

                }

See this Link ComboBoxRenderer Class

DeveloperX
  • 4,633
  • 17
  • 22