locked
Best fit all column for a listview RRS feed

  • Question

  • Hello.

    I noticed that when you double click at the end of a column in a listview (details style) the column changes the width so that all the text should fit.
    Is there any way I can do this at runtime?
    So I would like to do something like BestFitAllColumns() for a list view.

    Thanks for your future help (I hope ).
    Tuesday, December 20, 2005 11:46 AM

Answers

  • Hahaaaah I've found a solution!



    [DllImport( "User32.dll" )]
    public extern static int SendMessage( IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam );
    private void button1_Click(object sender, System.EventArgs e)
    {
     SendMessage( listView1.Handle, 0x101e, new IntPtr( 0 ),new IntPtr( -2 ) ); // 0x101e = LVM_SETCOLUMNWIDTH
    }

     

    You need to send the LVM_SETCOLUMNWIDTH message to the listview. The message is capable of  cropping the column using LVSCW_AUTOSIZE (-1), or to LVSCW_AUTOSIZE_USEHEADER (-2). This is the last parameter (lParam). You can also pass the actual width as wParam parameter. The index of the column is passed as wParam.

    Tuesday, December 20, 2005 1:40 PM

All replies

  • Yes, check the sample in ListView.AutoResizeColumn (2.0 framework only).
    Tuesday, December 20, 2005 12:13 PM
  • Oh, I forgot to say ... I am using framework 1.0.

    Thanks for your quick answer, but it will not help me
    Tuesday, December 20, 2005 12:14 PM
  • I assume you're using framework 1.1?

    Anyway, there is no support for this in framework 1.0 or 1.1. I've been digging and digging on how to solve this nicely, but I can't find a way :(

    You should handle this yourself and set the width of the columns manually depending on the maximum width of the items in the column. You should measure the width of the string in the box and add the icon size etc to it.

    There must be a better way :(

    Tuesday, December 20, 2005 1:20 PM
  • Hahaaaah I've found a solution!



    [DllImport( "User32.dll" )]
    public extern static int SendMessage( IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam );
    private void button1_Click(object sender, System.EventArgs e)
    {
     SendMessage( listView1.Handle, 0x101e, new IntPtr( 0 ),new IntPtr( -2 ) ); // 0x101e = LVM_SETCOLUMNWIDTH
    }

     

    You need to send the LVM_SETCOLUMNWIDTH message to the listview. The message is capable of  cropping the column using LVSCW_AUTOSIZE (-1), or to LVSCW_AUTOSIZE_USEHEADER (-2). This is the last parameter (lParam). You can also pass the actual width as wParam parameter. The index of the column is passed as wParam.

    Tuesday, December 20, 2005 1:40 PM
  • Thanks a lot. It works perfectly!
    Tuesday, January 10, 2006 11:01 AM