Autosize columns of listview

  • Thread starter Thread starter Sebastian
  • Start date Start date
S

Sebastian

hello,

is it possible to set the size of a header of a column automatic. if i add
an item thats size is smaller than the headers one, the header size doesn't
change(its so long that you can rea the full text of the header). if i add
an item thats longer than the header size, the header size is so long like
the item.

i search for LVSCW_AUTOSIZE_USEHEADER

regards

Sebastian
 
Hi,
this will do it with unmanaged code. Use it as
Listview_Autosize(mylistview,cols)

where mylistview is your listview, and cols is the number of columns. Supply
0 to size all columns. If your listview has 8 columns, and you call the
routine with '6', columns 7 and 8 will be sized to 0, and therefore hidden

HTH

Pete

Declare Function SendMessage Lib "Coredll" Alias "SendMessageW" (ByVal
hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal
lParam As Integer) As Integer
Declare Function GetFocus Lib "Coredll" () As Integer
Const LVM_SETCOLUMNWIDTH = &H101E


Public Shared Sub Autosize_Listview(ByVal lv As Windows.Forms.ListView,
ByVal Cols As Integer)
lv.Focus()

Dim WkCols As Integer
If Cols = 0 Then
WkCols = lv.Columns.Count - 1
Else
WkCols = Cols - 1
End If
Dim col2adjust As Integer
For col2adjust = 0 To WkCols
Call SendMessage(GetFocus(), _
LVM_SETCOLUMNWIDTH, _
col2adjust, _
-2)
Next
For col2adjust = WkCols + 1 To lv.Columns.Count - 1
lv.Columns(col2adjust).Width = 0
Next

End Sub
 
Thanks


Pete Vickers said:
Hi,
this will do it with unmanaged code. Use it as
Listview_Autosize(mylistview,cols)

where mylistview is your listview, and cols is the number of columns. Supply
0 to size all columns. If your listview has 8 columns, and you call the
routine with '6', columns 7 and 8 will be sized to 0, and therefore hidden

HTH

Pete

Declare Function SendMessage Lib "Coredll" Alias "SendMessageW" (ByVal
hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal
lParam As Integer) As Integer
Declare Function GetFocus Lib "Coredll" () As Integer
Const LVM_SETCOLUMNWIDTH = &H101E


Public Shared Sub Autosize_Listview(ByVal lv As Windows.Forms.ListView,
ByVal Cols As Integer)
lv.Focus()

Dim WkCols As Integer
If Cols = 0 Then
WkCols = lv.Columns.Count - 1
Else
WkCols = Cols - 1
End If
Dim col2adjust As Integer
For col2adjust = 0 To WkCols
Call SendMessage(GetFocus(), _
LVM_SETCOLUMNWIDTH, _
col2adjust, _
-2)
Next
For col2adjust = WkCols + 1 To lv.Columns.Count - 1
lv.Columns(col2adjust).Width = 0
Next

End Sub

--
Pete Vickers
Microsoft Windows Embedded MVP
HP Business Partner
http://www.gui-innovations.com
 
Back
Top