Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET ListView, max number of characters, or maximum column width? Possible to override/expand?

I have a .NET ListView control in which I display stack traces. I used the ListView since I needed to manipulate the font/colors of certain lines.

However, it seems there is some kind of maximum regarding the width of the columns, either the number of characters displayed, or the number of pixels a column can be.

Here is a simple LINQPad example that shows the problem:

void Main()
{
    using (var fm = new Form())
    {
        ListView lv = new ListView();
        fm.Controls.Add(lv);
        lv.Dock = DockStyle.Fill;
        lv.View = View.Details;
        lv.Columns.Add("C", -1, HorizontalAlignment.Left);

        string line = new string('W', 258) + "x";
        lv.Items.Add(line);
        line = new string('W', 259) + "x";
        lv.Items.Add(line);

        lv.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.ColumnContent);
        lv.Columns[0].Width.Dump();

        fm.ShowDialog();
    }
}

Screenshot:

screenshot of listview problem

As you can see, the line containing 258 W's + an X, shows the x, whereas the next line containing one additional W, does not show the x.

The output of the width calculation there shows that the current width of the column is 2864 pixels.

The question is this: Is there anything I can tweak on the ListView to work around this limitation?

like image 945
Lasse V. Karlsen Avatar asked Apr 05 '11 23:04

Lasse V. Karlsen


1 Answers

This behaviour is documented in the MSDN pages for ListViewItem:

The text of the ListViewItem should not exceed 259 characters or unexpected behavior could occur.

According to a Microsoft employee:

Why the length of the text of listview item is limited to 259 character is because that the listview is design for display collection of objects like files and not for general purpose control. So it is similar to the filename length limitation in the windows file system's MAX_PATH.

There is also a Microsoft Support article about this. The ListViewItem does store the full text, it is just the display that is limited in length.


However, it does appear possible to display the full text if you make a custom ListView and set it to OwnerDraw:

public class MyListView : ListView
{
    public MyListView()
    {
        OwnerDraw = true;
        DrawItem += new DrawListViewItemEventHandler(MyListView_DrawItem);
    }

    private void MyListView_DrawItem(object sender, DrawListViewItemEventArgs e)
    {
        e.Graphics.DrawString(e.Item.Text, e.Item.Font, 
                                    new SolidBrush(e.Item.ForeColor), e.Bounds);
    }
}

This displays the full text of each ListViewItem. The disadvantage in doing this is that you will also need to custom draw the other visual states as well (e.g. selected state, focus state, etc...) unless you can somehow route them through to the original drawing code.

I have no idea if there are any other side effects to doing this.

like image 150
adrianbanks Avatar answered Nov 10 '22 11:11

adrianbanks