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

前端 未结 1 679
日久生厌
日久生厌 2020-12-10 18:01

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

相关标签:
1条回答
  • 2020-12-10 18:33

    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.

    0 讨论(0)
提交回复
热议问题