Results 1 to 8 of 8

Thread: Listview Column Resize

  1. #1

    Thread Starter
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Listview Column Resize

    Does anyone know of an easy way to resize the ListView columns so that all the text is visible. Like when you double click on the resize bar and it automatically resizes it?

    Thanks,

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    I don't know of an easy way, but this might work:

    Create a graphics object.
    Loop all items in the listview
    Use the MeasureString function to get the width of the each items text for that particular column.
    Remember the widest.
    Set the columnwidth to that.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    You can also try API...
    VB Code:
    1. Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, _
    2.                                                                             ByVal wMsg As Integer, _
    3.                                                                             ByVal wParam As Integer, _
    4.                                                                             ByVal lParam As Integer) _
    5.                                                                             As Integer
    6.  
    7.     Private Const LVM_FIRST As Integer = &H1000
    8.     Private Const LVM_SETCOLUMNWIDTH As Integer = (LVM_FIRST + 30)
    9.  
    10.     Private Const LVSCW_AUTOSIZE As Integer = -1
    11.     Private Const LVSCW_AUTOSIZE_USEHEADER As Integer = -2
    12.  
    13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.  
    15.         For x As Integer = 1 To 100
    16.             Dim li As New ListViewItem(StrDup(x, "I"))
    17.             li.SubItems.Add(StrDup(x, "T"))
    18.             li.SubItems.Add(StrDup(x, "4"))
    19.             ListView1.Items.Add(li)
    20.         Next
    21.     End Sub
    22.  
    23.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    24.  
    25.         For Each c As ColumnHeader In ListView1.Columns
    26.             SendMessageLong(ListView1.Handle, LVM_SETCOLUMNWIDTH, c.Index, LVSCW_AUTOSIZE)
    27.         Next
    28.  
    29.     End Sub

    Button1_Click is just there to filll the listview with stuff.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    set the width to -1 or -2

    it autosizes the column

  5. #5

    Thread Starter
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Wow! Thanks that is perfect.

  6. #6
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by marvinklein
    set the width to -1 or -2

    it autosizes the column
    heh weird is that documented officially anywhere?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    In the help. I guess it pays to look stuff up, but I can't be bothered to.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by crptcblade
    In the help. I guess it pays to look stuff up, but I can't be bothered to.

    hehe agreed I hate that msdn help that ships with vs.net. I have always had problems with it. Numerious times after installing/reinstalling I couldnt get it to work at all (it would just return an "empty index entry" message for whatever I searched). I've had the same problems with the more recent versions of MSDN help too. The website kinda works, but I'm too lazy to go there
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

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