C#
public void AutoFitListView(ListView lv)
{
for (int i = 0; i <= lv.Columns.Count - 1; i++) { lv.Columns[i].Width = -2; // longest col head and item // -1 for just longest item } }
VB.NET
Public Sub AutoFitListView(ByRef lv As ListView)
For i As Integer = 0 To lv.Columns.Count - 1
lv.Columns(i).Width = -2
Next
End Sub
VB6
Unfortunately, the VB6 solution is a bit more complicated:
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_SETCOLUMNWIDTH As Long = (LVM_FIRST + 30)
Private Const LVSCW_AUTOSIZE As Long = -1
Private Const LVSCW_AUTOSIZE_USEHEADER As Long = -2
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, lParam As Any) As Long
Public Sub AutoFitListView(lv As ListView)
Dim i As Long
For i = 0 To lv.ColumnHeaders.count - 1
Call SendMessage(lv.hwnd, LVM_SETCOLUMNWIDTH, i, ByVal LVSCW_AUTOSIZE_USEHEADER)
Next
End Sub
You can change the LVSCW_AUTOSIZE_USEHEADER to LVSCW_AUTOSIZE if you do not want to consider the Header when adjusting the size.
More examples of the VB6 approach can be found at:
http://vbnet.mvps.org/index.html?code/comctl/lvcolumnautosize.htm
No comments:
Post a Comment