BUG: ListView.Columns[0].TextAlign ignored

  • Thread starter Thread starter Christoph Nahr
  • Start date Start date
C

Christoph Nahr

As the subject line says, the TextAlign property is ignored for the
first column of a Windows Forms ListView control. The text is always
left-aligned, regardless of which HorizontalAlignment value is used.

The following sample program demonstrates the bug with the .NET
Framework version 1.1. Note that both columns are set to right
alignment yet the first column is left-aligned:

using System.Windows.Forms;

public class MainClass {

public static void Main() {
Form form = new Form();

ListView list = new ListView();
list.Columns.Add("First", 100, HorizontalAlignment.Right);
list.Columns.Add("Second", 100, HorizontalAlignment.Right);

ListViewItem item = new ListViewItem(
new string[] { "foo", "bar" });
list.Items.Add(item);

list.Dock = DockStyle.Fill;
list.GridLines = true;
list.View = View.Details;

form.Controls.Add(list);
form.Show();
MessageBox.Show("Click OK to quit.");
}
}
 
This is by design (and yes, it's stupid)
Read the documentation for the LVCOLUMN structure:

"If a column is added to a list-view control with index 0 (the leftmost
column) and with LVCFMT_RIGHT or LVCFMT_CENTER specified, the text is not
right-aligned or centered. The text in the index 0 column is left-aligned.
Therefore if you keep inserting columns with index 0, the text in all
columns are left-aligned. If you want the first column to be right-aligned
or centered you can make a dummy column, then insert one or more columns
with index 1 or higher and specify the alignment you require. Finally delete
the dummy column."

/claes
 
This is by design (and yes, it's stupid)
Read the documentation for the LVCOLUMN structure:

So it's an old Common Controls problem! Thanks for the reply, I'm
going to mail our posts to the .NET Framework docs team.

There's probably no chance that this will be fixed in .NET since the
underlying Common Controls are broken, but at least the documentation
should be updated with the same caveat.
 
Back
Top