Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Mar 26, 1999

Set the Width of a ListView Column

The ListView Windows control has some additional features that haven't been exposed in the OCX provided with VB. One feature can shrink or enlarge columns automatically to ensure that data in each column is visible and that no screen space is wasted. Use this function to make an API call:
 
Public Declare Function SendMessage Lib "user32" Alias _
	"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As _
	Long, ByVal wParam As Long, lParam As Any) As Long

Const LVM_SETCOLUMNWIDTH = &H1000 + 30
Const LVSCW_AUTOSIZE = -1
Const LVSCW_AUTOSIZE_USEHEADER = -2

Sub AdjustColumnWidth(LV As ListView, AccountForHeaders _
	As Boolean)
	Dim col As Integer, lParam As Long
	If AccountForHeaders Then
		lParam = LVSCW_AUTOSIZE_USEHEADER
	Else
		lParam = LVSCW_AUTOSIZE
	End If
	' Send the message to all the columns
	For col = 0 To LV.ColumnHeaders.Count - 1
		SendMessage LV.hwnd, LVM_SETCOLUMNWIDTH, col, ByVal lParam
	Next
End Sub
You can resize all columns, taking the text in column headers into account by passing True as the second argument:
 
AdjustColumnWidth ListView1, True
If you pass False as the second argument, the text in column headers is ignored in determining the correct width.
Marco Losavio
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date