Only resize certain listview column if it is greater than current width
Hi Guys,
I am using the following code to resize listview columns according to their contents.
Code:
For Col2Adjust = 1 To Lv.ColumnHeaders.count - 1
If (Col2Adjust = 2) Then
Debug.Print Lv.ColumnHeaders(2).Width
If (Lv.ColumnHeaders(2).Width > 6423.875) Then
Call SendMessage(Lv.hWnd, _
LVM_SETCOLUMNWIDTH, _
Col2Adjust, _
ByVal LVSCW_AUTOSIZE_USEHEADER)
End If
Else
Call SendMessage(Lv.hWnd, _
LVM_SETCOLUMNWIDTH, _
Col2Adjust, _
ByVal LVSCW_AUTOSIZE_USEHEADER)
End If
Next
As you can see I want to skip column 2. I only want to resize column 2 if the width is greater than what I have specified there. But it isn't working.
Any ideas.:confused:
Re: Only resize certain listview column if it is greater than current width
I tried to simulate your code like this....it is working
Code:
Dim col2Adjust As Integer
For col2Adjust = 1 To Me.ListView1.ColumnHeaders.Count - 1
If (col2Adjust = 2) Then
Debug.Print "before-> " & Me.ListView1.ColumnHeaders(col2Adjust).Width
If Me.ListView1.ColumnHeaders(col2Adjust).Width > 1000 Then
Me.ListView1.ColumnHeaders(col2Adjust).Width = Me.ListView1.ColumnHeaders(col2Adjust).Width + 1000
' Debug.Print "After-> " & Me.ListView1.ColumnHeaders(col2Adjust).Width
End If
Debug.Print "After-> " & Me.ListView1.ColumnHeaders(col2Adjust).Width
End If
Next
End Sub
try running again using above code. If you still have problem it might be something in the SendMessage which I don't know :cry:
Re: Only resize certain listview column if it is greater than current width
Quote:
Originally Posted by
Nitesh
Hi Guys,
I am using the following code to resize listview columns according to their contents.
Code:
For Col2Adjust = 1 To Lv.ColumnHeaders.count - 1
If (Col2Adjust = 2) Then
Debug.Print Lv.ColumnHeaders(2).Width
If (Lv.ColumnHeaders(2).Width > 6423.875) Then
Call SendMessage(Lv.hWnd, _
LVM_SETCOLUMNWIDTH, _
Col2Adjust, _
ByVal LVSCW_AUTOSIZE_USEHEADER)
End If
Else
Call SendMessage(Lv.hWnd, _
LVM_SETCOLUMNWIDTH, _
Col2Adjust, _
ByVal LVSCW_AUTOSIZE_USEHEADER)
End If
Next
As you can see I want to skip column 2. I only want to resize column 2 if the width is greater than what I have specified there. But it isn't working.
Any ideas.:confused:
I know this is an old question, but for me the importance is the understanding of using the API, for other users.
The API column is zero-based not one-based like the object uses.. so it should actually be this:
Code:
Call SendMessage(Lv.hWnd, _
LVM_SETCOLUMNWIDTH, _
Col2Adjust-1, _
ByVal LVSCW_AUTOSIZE_USEHEADER)