Results 1 to 3 of 3

Thread: Only resize certain listview column if it is greater than current width

  1. #1

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    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.

  2. #2
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    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

  3. #3
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    264

    Re: Only resize certain listview column if it is greater than current width

    Quote Originally Posted by Nitesh View Post
    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.
    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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width