views:

2031

answers:

3

I've found some examples using the Win32 api or simulating the ^+ button combination (ctrl-+) using SendKeys, but at least with the SendKeys method the listview grabs the cursor and sets it to an hourglass until I hit the start button on my keyboard. What is the cleanest way to do this?

A: 

Can you be more specific about what "auto-resize" means? Do you mean you want them to shuffle around when the window (and list view) change size, or do you want them to resize based on the length of the data they're showing?

Andrew
I want them to resize based on the length of the data they're showing.
Luke
+1  A: 

According to MSDN, if you set the column width to -1 then it will autosize to the widest item

rpetrich
This doesn't seem to work for me. I'm adding listviewitems at run-time and the columns stay skinny.
Luke
Weird. Perhaps the ListView needs to be visible and with the items added already. Or perhaps a call to Application.DoEvents might do the trick?You could always try sending LVSCW_AUTOSIZE yourself using P/Invoke: http://vbnet.mvps.org/index.html?code/comctl/lvcolumnautosize.htm (link for VB6)
rpetrich
Have you tried wrapping the list view updates in BeginUpdate()/EndUpdate() brackets? Maybe the -1 trick works when you specifically end the update, triggering a recalc of the way the list view is displayed.
Andrew
+3  A: 

Looks like a call to myListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent) will do what you want. I would think, just call it after adding an item.

More info here

McN
Should be ColumnHeaderAutoResizeStyle::ColumnContent
Julien