Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

loop through all columns and set width to -1 after adding content.

After adding the following routine to your code then call it from any procedure/function. Do not use it in your "Form_Load" procedure though. Only call it after you have added an item to your ListView (or if you are making multiple additions, call it once at the end of all the additions):

    Private Sub AutoSizeListViewColumns(oListView As ListView)
        Dim nCol As Integer = 0
        SuspendLayout()
        For nCol = 0 To (oListView.Columns.Count - 1)
            oListView.Columns(nCol).Width = -1  'forces autosizing on column
        Next
        oListView.Refresh()
        ResumeLayout()
    End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top