I love writing, it’s the center of my life. If you don’t love what you do, you’d better find something else to love. Otherwise, you don’t have a reason for living.
Ray Bradbury

I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can’t see from the center.
Kurt Vonnegut

Mankind is not a circle with a single center but an ellipse with two focal points of which facts are one and ideas the other.
Victor Hugo

The other day, I thought a drop down ComboBox with centered entries would look good.  A ComboBox does not have a property for Alignment.  As usual, I searched the Internet for answers.  I found some half baked thoughts and one code example.  The one example turned out to have a few bugs and was missing a crucial step.  But this example did get me started on the right track.

The rest of this blog will show how to Align a ComboBox to the Center or Right as well as the default Left alignment.

Place a ComboBox on your Windows Application Form.  Attach a method to the DrawItem event.  The next part is critical.  Change the DrawMode Property from Normal to either OwnerDrawFixed or OwnerDrawVariable.  If DrawMode is not changed, the DrawItem event will NOT call your drawing method.  This drawing technique works correctly if the DropDownStyle is DropDownList.  For a DropDownStyle of DropDown, the text box remained left aligned but the expanded drop down was centered.

The following is the source code for the owner draw method.  The comments in the source code explain each step.  The reason the alignment is defined in terms of Far, Near and Center is alignment is used with the property RightToLeft.  I have highlighted some key points of the source code.  Set line 17 to Far alignment, to align to the Right.

// Allow Combo Box to center aligned
private void cbxDesign_DrawItem(object sender, DrawItemEventArgs e)
{
  // By using Sender, one method could handle multiple ComboBoxes
  ComboBox cbx = sender as ComboBox;
  if (cbx != null)
  {
    // Always draw the background
    e.DrawBackground();

    // Drawing one of the items?
    if (e.Index >= 0)
    {
      // Set the string alignment.  Choices are Center, Near and Far
      StringFormat sf = new StringFormat();
      sf.LineAlignment = StringAlignment.Center;
      sf.Alignment = StringAlignment.Center;

      // Set the Brush to ComboBox ForeColor to maintain any ComboBox color settings
      // Assumes Brush is solid
      Brush brush = new SolidBrush(cbx.ForeColor);

      // If drawing highlighted selection, change brush
      if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
        brush = SystemBrushes.HighlightText;

      // Draw the string
      e.Graphics.DrawString(cbx.Items[e.Index].ToString(), cbx.Font, brush, e.Bounds, sf);
    }
  }
}

The following images show the ComboBox in different states.

Disabled.  Highlighted. Expanded

P.S. – Since writing this blog, I found I wanted to right align a ListBox.  This technique will work for ListBox as well.  The code for a right aligned ListBox is shown below.  Mg 21-MAY-2010

// Allow ListBox to Right Align
private void lbxPasswordInt_DrawItem(object sender, DrawItemEventArgs e)
{
  // By using Sender, one method could handle multiple ListBoxes
  ListBox lbx = sender as ListBox;
  if (lbx != null)
  {
    // Always draw the background
    e.DrawBackground();

    // Drawing one of the items?
    if (e.Index >= 0)
    {
      // Set the string alignment.  Choices are Center, Near and Far
      StringFormat sf = new StringFormat();
      sf.LineAlignment = StringAlignment.Far;
      sf.Alignment = StringAlignment.Far;

      // Set the Brush to ListBox ForeColor to maintain any ListBox color settings
      // Assumes Brush is solid
      Brush brush = new SolidBrush(lbx.ForeColor);

      // If drawing highlighted selection, change brush
      if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
        brush = SystemBrushes.HighlightText;

      // Draw the string
      e.Graphics.DrawString(lbx.Items[e.Index].ToString(), lbx.Font, brush, e.Bounds, sf);
    }
  }
}

Tags: ,

Leave a Reply


*