Jump to content

Autosizing ListView columns


Hawk
 Share

Recommended Posts

Hey there!

I use the example script from the _GUICtrlListView_GetItemParam help file with 2 added lines to show my problem.

After line 31 I add:

_GUICtrlListView_SetColumnWidth($hListView, 0, $LVSCW_AUTOSIZE)

The full script:

#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include 
#include 
Opt('MustDeclareVars', 1)
$Debug_LV = False ; Check ClassName being passed to ListView functions, set to True and use a handle to another control to see it work
; Warning do not use SetItemParam on items created with GuiCtrlCreateListViewItem
; Param is the controlId for items created with built-in function
Example_UDF_Created()
Func Example_UDF_Created()
Local $GUI, $hListView

$GUI = GUICreate("(UDF Created) ListView Get Item Param", 400, 300)
$hListView = _GUICtrlListView_Create($GUI, "", 2, 2, 394, 268)
GUISetState()

; Add columns
_GUICtrlListView_AddColumn($hListView, "Items", 100)
; Add items
_GUICtrlListView_AddItem($hListView, "Item 1")
_GUICtrlListView_AddItem($hListView, "Item 2")
_GUICtrlListView_AddItem($hListView, "Item 3")
; Set item 2 parameter
_GUICtrlListView_SetItemParam($hListView, 1, 1234)
;MsgBox(4160, "Information", "Item 2 Parameter: " & _GUICtrlListView_GetItemParam($hListView, 1))
_GUICtrlListView_SetColumnWidth($hListView, 0, $LVSCW_AUTOSIZE)
; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
EndFunc   ;==>Example_UDF_Created

Running this program will create a listview where the items of the first column are autosized. The problem is, that the header is not autosized to it's respective size (it shows "Ite...")

Now if we change $LVSCW_AUTOSIZE to $LVSCW_AUTOSIZE_USEHEADER and run the program again we have the column autosized to the end of the listview instead of just autosized so that "Items" can be shown fully.

I know that this is written in the helpfile and in the WinAPI.

I, though, want to size the column exactly how it needs to be, not to the end of the listview.

I thought about 2 dirty workarounds:

  • 1) After the last real column, create a new fake column, use _GUICtrlListView_SetColumnWidth to autosize the (now) pre-last column, then delete the fake column (becaue _GUICtrlListView_SetColumnWidth only sized the last column to full size)
  • 2) Get the text, the font face and the font size of the last column's header, find a pre-written function that calculates the width of a text given the string, font and size and use _GUICtrlListView_SetColumnWidth with that width instead.

I am just exhausted from programming at the moment to find/write one of these workarounds, and want to ask if there is an easier method/workaround!

TIA,

Hawk

Edited by Hawk
Link to comment
Share on other sites

  • Moderators

Hawk,

Easy - just create a second column like this: ;)

#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>

Opt('MustDeclareVars', 1)

Example_UDF_Created()

Func Example_UDF_Created()
    Local $GUI, $hListView, $sHeader = "Full header of any width at all"

    $GUI = GUICreate("(UDF Created) ListView Get Item Param", 400, 300)
    $hListView = _GUICtrlListView_Create($GUI, "", 2, 2, 394, 268)
    GUISetState()

    _GUICtrlListView_AddColumn($hListView, $sHeader)
    _GUICtrlListView_AddColumn($hListView, "") ; <<<<<<<<<<<<<<<<<<<<<<<<<<

    ; Add items
    _GUICtrlListView_AddItem($hListView, "Item 1")
    _GUICtrlListView_AddItem($hListView, "Item 2")
    _GUICtrlListView_AddItem($hListView, "Item 3")

    _GUICtrlListView_SetColumnWidth($hListView, 0, $LVSCW_AUTOSIZE_USEHEADER)
    _GUICtrlListView_SetColumnWidth($hListView, 1, 1) ; <<<<<<<<<<<<<<<<<<<<<<<<<<

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example_UDF_Created

Works for me - is it what you wanted? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Yes that was my first idea. The last column is totally invisible as long as you don't size it with your mouse, that's great!

So I will probably crate one, size it immediately to 0 size, and leave it in the program as nobody notices it anyways!

Thanks for the input again Melba! :)

(Edit: That doesn't mean that I'd not like to have a tweak, that automatically solves that problem without a workaround ;))

Edited by Hawk
Link to comment
Share on other sites

  • Moderators

Hawk,

That doesn't mean that I'd not like to have a tweak, that automatically solves that problem without a workaround

Well, if you are going to be fussy I will not bother to post solutions any more.... :)

M23

P.S. To show I am not really serious I have solved the NM_RETURN/ENTER problem in your other thread. ;)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hawk,

Well, if you are going to be fussy I will not bother to post solutions any more.... ;)

M23

P.S. To show I am not really serious I have solved the NM_RETURN/ENTER problem in your other thread. ;)

Sorry :) It's just my perfectionism why I can't use a ("dirty") workaround and finish the case. I always think that when people using the program find out about the workaround, they will think worse of the program. Of course I am thankful to have the solution working though :)

Link to comment
Share on other sites

If you'd like to do it with out the second column, you can use the by our esteemed member Melba23 to assist in autosizing the header size. :)

This is a demonstration of how it could work.

#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <stringsize.au3>
Opt('MustDeclareVars', 1)

Example_UDF_Created()

Func Example_UDF_Created()
    Local $GUI, $hListView, $sHeader = "Full header of any width at all"
    Local $aHeaderSize = _StringSize($sHeader, 8, 400, 0) ;<<<<<<<<<<<<<<<<<<<< Get header text size
    $GUI = GUICreate("(UDF Created) ListView Get Item Param", 400, 300)
    $hListView = _GUICtrlListView_Create($GUI, "", 2, 2, 394, 268)
    GUISetState()

    _GUICtrlListView_AddColumn($hListView, $sHeader)

    ; Add items
    _GUICtrlListView_AddItem($hListView, "Item 1")
    _GUICtrlListView_AddItem($hListView, "Item 2")
    _GUICtrlListView_AddItem($hListView, "Item 3")

    _GUICtrlListView_SetColumnWidth($hListView, 0, $aHeaderSize[2]);<<<<<<<<<<<<<<<<<<< Use the target width of the string from the array to set the size.

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example_UDF_Created

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Moderators

BrewManNH,

I did think of using that myself, but I decided to go for the other solution. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thought a different approach might be helpful if anyone is adamant about not using the workaround of a hidden second column.

Took some tweaking with the settings to get it right, mainly because I didn't know what the font figures should be for the header text, so I went with the default font size information rather than the AutoIt defaults that I tried first.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Moderators

BrewManNH,

That was why I did not go down the StringSize route - it seemed too unreliable given the possibility of differences in the system fonts between machines. :)

Besides I thought getting Windows to do it on its own was more fun. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...