7

By default the items in the C# Combobox are left aligned. Are there any options available to change this justification apart from overriding DrawItem method and setting the combobox drawmode --> DrawMode.OwnerDrawFixed?

Cheers

Pieniadz
  • 653
  • 3
  • 9
  • 22
this-Me
  • 2,139
  • 6
  • 43
  • 70
  • I added winforms tag to your question since I assume from the fact you mentioned DrawItem that you're not referring to WPF. This doesn't really have anything to do with C# though, but I didn't want to remove a tag. – Josh Jun 23 '10 at 06:04

3 Answers3

4

You could just set the control style to RightToLeft = RightToLeft.Yes if you don't mind the drop widget on the other side as well.

or

set DrawMode = OwnerDrawFixed; and hook the DrawItem event, then something like

    private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index == -1)
            return;
        ComboBox combo = ((ComboBox) sender);
        using (SolidBrush brush = new SolidBrush(e.ForeColor))
        {
            e.DrawBackground();
            e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font, brush, e.Bounds, new StringFormat(StringFormatFlags.DirectionRightToLeft));
            e.DrawFocusRectangle();
        }
    }
C.B.
  • 666
  • 3
  • 18
Paul
  • 41
  • 2
  • Hi ... There is no difference i can see even after i implemented this handler. – this-Me Jun 23 '10 at 09:24
  • not sure why, did you remember to set the property of the combo box DrawMode to OwnerDrawFixed. And do you have some items in the list. – Paul Jun 24 '10 at 01:49
2

In WPF this would be as easy as specifying an ItemContainerStyle. In Windows Forms it's a little trickier. Without custom drawing, you could set the RightToLeft property on the ComboBox but this would unfortunately also affect the drop down button.

Since Windows Forms uses a native ComboBox, and Windows doesn't have a ComboBox style like ES_RIGHT that affects the text alignment, I think your only option is to resort to owner draw. It would probably be a good idea to derive a class from ComboBox and add a TextAlignment property or something. Then you would only apply your drawing if TextAlignment was centered or right aligned.

Josh
  • 68,005
  • 14
  • 144
  • 156
  • Hi ... In this case how do i apply the TextAlignment to the control? Are you hinting at the string format here ? – this-Me Jun 23 '10 at 06:27
  • No I'm saying you would need to create a control that derives from ComboBox and add a *new* property called TextAlignment. Then in your OnDrawItem method you can take this property into account, rather than hard coding an alignment. – Josh Jun 23 '10 at 06:35
  • Example: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.drawitem.aspx – Josh Jun 23 '10 at 06:36
1

You must "DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed" and your own draw method like this.

protected virtual void OnDrawItem(object sender, DrawItemEventArgs e)
{
    var comboBox = sender as ComboBox;

    if (comboBox == null)
    {
        return;
    }

    e.DrawBackground();

    if (e.Index >= 0)
    {
        StringFormat sf = new StringFormat();
        sf.LineAlignment = StringAlignment.Center;
        sf.Alignment = StringAlignment.Center;

        Brush brush = new SolidBrush(comboBox.ForeColor);

        if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
        {
            brush = SystemBrushes.HighlightText;
        }

        e.Graphics.DrawString(comboBox.Items[e.Index].ToString(), comboBox.Font, brush, e.Bounds, sf);
    }
}
Kim Ki Won
  • 1,795
  • 1
  • 22
  • 22