Calculating ListView width

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
 
L

Len Weaver

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
 

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