Cheeso
Cheeso

Reputation: 192667

Windows Forms: is it possible to draw the elements of a combobox, right-justified?

In Windows Forms, is it possible to configure a drop-down combobox control so that the items are right justified?

The default is left justified, like this:

Combobox

Upvotes: 1

Views: 434

Answers (3)

Cheeso
Cheeso

Reputation: 192667

I set the DrawItem event on the combobox. Also set

this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;

This is the code I used for DrawItem:

    private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
        var rc = new System.Drawing.Rectangle(e.Bounds.X , e.Bounds.Y,
                                     e.Bounds.Width, e.Bounds.Height);

        var sf = new System.Drawing.StringFormat
        {
            Alignment = System.Drawing.StringAlignment.Far
        };

        string str = (string)comboBox1.Items[e.Index];

        if (e.State == (DrawItemState.Selected | DrawItemState.NoAccelerator
                          | DrawItemState.NoFocusRect) ||
             e.State == DrawItemState.Selected)
        {
            e.Graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.CornflowerBlue), rc);
            e.Graphics.DrawString(str, this.comboBox1.Font, new System.Drawing.SolidBrush(System.Drawing.Color.Cyan), rc, sf);
        }
        else
        {
            e.Graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), rc);
            e.Graphics.DrawString(str, this.comboBox1.Font, new System.Drawing.SolidBrush(System.Drawing.Color.Black), rc, sf);
        }
    }

This is what it looks like :

alt text

Upvotes: 1

olle
olle

Reputation: 4595

It is but you would need to ownerdraw the combobox yourself so you can align the text see http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/0438b63a-5f6b-401c-8ea9-cd9e950ed6e1/ for more information.

Upvotes: 3

Inisheer
Inisheer

Reputation: 20794

Change the ComboBox "RightToLeft" property to TRUE.

Note: The drop-down arrow will now be on the left side of the control.

Upvotes: 1

Related Questions