Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

Listview Question

Posted on 2001-08-08
Medium Priority
284 Views
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
Question by:KhrisE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2
• 2
• +1

LVL 43

Expert Comment

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

ID: 6363848
a small example mabe ?
0

LVL 11

Expert Comment

ID: 6363853
Try this:

Dim MaxWidth As Long

MaxWidth = 0

For i = 1 To ListView1.ColumnHeaders.Count
End If
Next i

For i = 1 To ListView1.ColumnHeaders.Count
Next i
0

LVL 43

Expert Comment

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
Next
0

LVL 11

Expert Comment

ID: 6363871
I seem to have misread the question. I'm sorry.
0

LVL 1

Accepted Solution

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

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

Private Enum AutoSizeStyles
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
End Sub

Dim iIndex              As Long
Dim lAutoSizeOn         As Long
Dim lColCount           As Long

'initially set the return value to false

'get the column count
'get the resize flag

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

'return

End Function
0

LVL 1

Expert Comment

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

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

Private Enum AutoSizeStyles
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
End Sub

Dim iIndex              As Long
Dim lAutoSizeOn         As Long
Dim lColCount           As Long

'initially set the return value to false

'get the column count
'get the resize flag

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

'return

End Function
0

LVL 1

Author Comment

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll