Solved

Listview Question

Posted on 2001-08-08
8
248 Views
Last Modified: 2008-02-26
Hi People !

I have a listview, which has 4 columns - the size of the data in each column varies - I need to set the width of the columns to the widest entry made

Anyone have any snippet of code I can study from or an example else where ??

Regards

KhrisE
0
Comment
Question by:KhrisE
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 6363836
Ok, what you need to do is this, assuming that the font of each column is the same and is the same as the font set on the form (if it is different then it is still achievable but needs an extra step).

Go through each row in the listview for each column and calculate the following value for each column's text:

Me.TextWidth(ListView1.SubItems(2)) 'Assuming column 2

Determine the max value of this and add a little (say 100) for luck. Then set the width of this column equal to that value.
0
 
LVL 1

Author Comment

by:KhrisE
ID: 6363848
a small example mabe ?
0
 
LVL 11

Expert Comment

by:Otana
ID: 6363853
Try this:

Dim MaxWidth As Long

MaxWidth = 0

For i = 1 To ListView1.ColumnHeaders.Count
    If ListView1.ColumnHeaders(i).Width > MaxWidth Then
        MaxWidth = ListView1.ColumnHeaders(i).Width
    End If
Next i

For i = 1 To ListView1.ColumnHeaders.Count
    ListView1.ColumnHeaders(i).Width = MaxWidth
Next i
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6363865
Dim dblMaxWidth As Double

For intCol = 1 To ListView1.ColumnHeaders.Count
  dblMaxWidth = 0
  For intRow = 1 To ListView1.ListItems.Count
    If Me.TextWidth(ListView1.ListItems(intRow).SubItems(intCol) > dblMaxWidth Then
      dblMaxWidth = Me.TextWidth(ListView1.ListItems(intRow).SubItems(intCol)
    End If
  Next
  ListView1.ColumnHeaders(intCol).Width = dblMaxWidth + 100
Next
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:Otana
ID: 6363871
I seem to have misread the question. I'm sorry.
0
 
LVL 1

Accepted Solution

by:
Vinda earned 20 total points
ID: 6363883
This should perform what u require....


Private Const LVSCW_AUTOSIZE = -1
Private Const LVSCW_AUTOSIZE_USEHEADER = -2
Private Const LVM_FIRST                          As Long = &H1000
Private Const LVM_SETCOLUMNWIDTH                 As Long = LVM_FIRST + 30

Private Enum AutoSizeStyles
   UseNoHeader = LVSCW_AUTOSIZE
   UseHeader = LVSCW_AUTOSIZE_USEHEADER
End Enum

Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" _
                                                    (ByVal hwnd As Long, _
                                                     ByVal wMsg As Long, _
                                                     ByVal wParam As Long, _
                                                     ByVal lParam As Long) As Long

Private Sub Command2_Click()
    'Pass the ListView object
    AdjustHeaderWidth ListView1
End Sub

Private Function AdjustHeaderWidth(lv As ListView) As Boolean
    Dim iIndex              As Long
    Dim lAutoSizeOn         As Long
    Dim lColCount           As Long
    Dim SizeToHeader        As Boolean

    'initially set the return value to false
    AdjustHeaderWidth = False

    'get the column count
    lColCount = lv.ColumnHeaders.Count - 1
    'get the resize flag
    lAutoSizeOn = LVSCW_AUTOSIZE_USEHEADER

    'resize each column
    For iIndex = 0 To lColCount
        Call SendMessageLong(lv.hwnd, LVM_SETCOLUMNWIDTH, iIndex, ByVal lAutoSizeOn)
    Next

    'return
    AdjustHeaderWidth = True

End Function
0
 
LVL 1

Expert Comment

by:Vinda
ID: 6363886
This should perform what u require....


Private Const LVSCW_AUTOSIZE = -1
Private Const LVSCW_AUTOSIZE_USEHEADER = -2
Private Const LVM_FIRST                          As Long = &H1000
Private Const LVM_SETCOLUMNWIDTH                 As Long = LVM_FIRST + 30

Private Enum AutoSizeStyles
   UseNoHeader = LVSCW_AUTOSIZE
   UseHeader = LVSCW_AUTOSIZE_USEHEADER
End Enum

Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" _
                                                    (ByVal hwnd As Long, _
                                                     ByVal wMsg As Long, _
                                                     ByVal wParam As Long, _
                                                     ByVal lParam As Long) As Long

Private Sub Command2_Click()
    'Pass the ListView object
    AdjustHeaderWidth ListView1
End Sub

Private Function AdjustHeaderWidth(lv As ListView) As Boolean
    Dim iIndex              As Long
    Dim lAutoSizeOn         As Long
    Dim lColCount           As Long
    Dim SizeToHeader        As Boolean

    'initially set the return value to false
    AdjustHeaderWidth = False

    'get the column count
    lColCount = lv.ColumnHeaders.Count - 1
    'get the resize flag
    lAutoSizeOn = LVSCW_AUTOSIZE_USEHEADER

    'resize each column
    For iIndex = 0 To lColCount
        Call SendMessageLong(lv.hwnd, LVM_SETCOLUMNWIDTH, iIndex, ByVal lAutoSizeOn)
    Next

    'return
    AdjustHeaderWidth = True

End Function
0
 
LVL 1

Author Comment

by:KhrisE
ID: 6363908
Mnay thanks for the assistance Vinda - it was a great help <s> - many thanks to the other guys for putting in the effort

KhrisE
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now