Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleEasily make ListView column headers programmatically in VB .NET
KeywordsListView, columns, column headers, VB.NET
CategoriesVB.NET, Controls
 
Subroutine ListViewMakeColumnHeaders takes as parameters a ListView control and a ParamArray containing header information. For each column header, the ParamArray contains the column's title text, width, and alignment. The code loops through the ParamArray creating the column headers.
 
' Make the ListView's column headers.
' The ParamArray entries should alternate between
' strings and HorizontalAlignment values.
Private Sub ListViewMakeColumnHeaders(ByVal lvw As _
    ListView, ByVal ParamArray header_info() As Object)
    ' Remove any existing headers.
    lvw.Columns.Clear()

    ' Make the column headers.
    For i As Integer = header_info.GetLowerBound(0) To _
        header_info.GetUpperBound(0) Step 3
        lvw.Columns.Add( _
            DirectCast(header_info(i), String), _
            DirectCast(header_info(i + 1), Integer), _
            DirectCast(header_info(i + 2), _
                HorizontalAlignment))
    Next i
End Sub
 
The program would use this subroutine as in:
 
' Make the ListView column headers.
ListViewMakeColumnHeaders(lvwBooks, _
    "Title", 200, HorizontalAlignment.Left, _
    "URL", 100, HorizontalAlignment.Left, _
    "ISBN", 100, HorizontalAlignment.Left, _
    "Picture", 200, HorizontalAlignment.Left, _
    "Pages", 50, HorizontalAlignment.Right, _
    "Year", 50, HorizontalAlignment.Right)
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated