BUG: ListView.Columns[0].TextAlign ignored

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.");
}
}
 
C

Claes Bergefall

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
 
C

Christoph Nahr

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top