Calculating ListView width

  • Thread starter Thread starter Simanovsky
  • Start date Start date
S

Simanovsky

Hello all,

Could anybody advice how to calculate the width of the
widest item in the ListView?

I need to resize a ListView according to the size of the
strings stored in it, particulary to avoid horizontal
scrolling. To calculate the new dimensions of the control
I tried both overloads of ListView.GetItemRect() while
setting View to View.List and View.Details. But
GetItemRect seems always to return same value for all
indices and setting control width to that does not ensure
absence of horizontal scrolling.

Thanks in advance,
Simanovsky
 
Hello Simanovsky,
Could anybody advice how to calculate the width of the
widest item in the ListView?

I need to resize a ListView according to the size of the
strings stored in it, particulary to avoid horizontal
scrolling. To calculate the new dimensions of the control

This feature is built in to the .net ListView. After you've added
rows to the ListView, execute this code:

<Untested code>
foreach( ColumnHeader c in lvwMyListView.Columns )
c.Width = -2;
</Untested code>

Setting the width to -2 will resize a column to accomodate the
longest string in that column (in Details view).

Hope this helps,
Len
 
Back
Top