Jump to content
Sign in to follow this  
Polyphem

GUICtrlCreateList How to read all values and how add values to the top

Recommended Posts

Polyphem

HiHo,

I'm currently integrating GUICtrlCreateList in my largest project. But it does not behave exactly as i want it to. Two things would make me happy:

1) I deactivated sorting. Is it possible to add new values to the top of the list instead of the bottom?

2) How do I read the all items of the List to a string or an array? GUICtrlRead returns only the value selected.

Thanks and best regards

Polyphem


This post will be edited again by Polyphem: Tomorrow, 11:55 AM

Share this post


Link to post
Share on other sites
Polyphem

Solved. Maybe this is useful for someone else. Needs Beta for ShellExecute.

Btw: Is it possible to suppress the ShellExecute error messages, when the file could not be found ("Windows cannot find...")?

Edit: Updated to execute items only on doubleclick

;===============================================================================
; Description:      Example Script for custom GUICtrlCreateList behavior
;                   - List items are execute on Doubleclick
;                   - List items clicked, which are not executable, are deleted automatically
;                   - New items will be added at the top of the list
;                   - List width is automatically adjusted corresponding to item length 
;                     (used MonoSpace font 'Courier' for letter to pixel conversion)
;                   - Read all list items to string, items delimited by '|'
; Author(s):        Polyphem
; Version:          V1.1, 2006/12/20
;===============================================================================

#include <GUIConstants.au3>
#Include <GuiList.au3>

Global $listctrl_content = "", $listctrl_max_width = 0
Global $font_to_pixel_ratio = 8.4 ; Seems to fit for the used MonoSpace font 'Courier'
Global Const $WM_COMMAND = 0x0111 ; needed for Doubleclick

Opt('RunErrorsFatal',0) ; otherwise not found entries in the list would terminate the script

;================
; GUI
;================

GUICreate('Custom GUICtrlCreateList', 400, 200)

$listctrl = GUICtrlCreateList("", 10, 10, 390, 120, BitOR($WS_BORDER, $WS_VSCROLL, $LBS_NOTIFY, $LBS_DISABLENOSCROLL, $WS_HSCROLL))
GUICtrlSetFont(-1, 8, 400, 4, "Courier"); MonoSpace Font => need for the correct width adjustment 
GUICtrlSetColor(-1, 0x0000ff)
GUICtrlSetData($listctrl,'http://de.wikipedia.org/wiki/Schriftart|msconfig|notepad|Testitem - will be deleted on click|Testitem - to test the automatic width adjustment - Looooooooooooooooooooooooooooooooooooooooooooooooogggggggggggggggg')
listctrl_set_width($listctrl)

$add_item_to_list_button = GUICtrlCreateButton(" Add Item to Top of List ", 240, 130,150)
$add_item_to_list_input = GUICtrlCreateInput("",10,130,200)

$display_list_items_as_string = GUICtrlCreateButton(" Show all items in list ", 240, 170,150)


GUISetState()

GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND") ; needed for Doubleclick

;================
; Main Loop Start
;================

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $add_item_to_list_button
            listCtrl_add_item($listctrl,$add_item_to_list_input)

        case $msg = $display_list_items_as_string
            MsgBox(0,"Display all list items",display_list_items($listctrl))

        Case $msg = $GUI_EVENT_CLOSE
            Exit
            
    EndSelect
WEnd

;================
; Main Loop End
;================

;================
; Functions
;================

Func listCtrl_add_item($a_listctrl,$a_add_item_to_list_input)
    for $i = 0 to _GUICtrlListCount($a_listctrl)-1
        if $i = 0 Then
            $listctrl_content = _GUICtrlListGetText($a_listctrl,$i)
        Else
            $listctrl_content = $listctrl_content & '|' &  _GUICtrlListGetText($a_listctrl,$i)
        EndIf
    Next
    GUICtrlSetData($a_listctrl,"")
    GUICtrlSetData($a_listctrl,GUICtrlRead($a_add_item_to_list_input) & '|' & $listctrl_content)
    listctrl_set_width($a_listctrl)
    $listctrl_content = ""
    $listctrl_max_width = 0
EndFunc

Func listctrl_shellexecute_selected_item($a_listctrl)
    $read = GUICtrlRead($a_listctrl)
    ShellExecute($read)
    If @error Then 
        _GUICtrlListDeleteItem($a_listctrl, _GUICtrlListSelectedIndex($a_listctrl)) ; delete item from list if not executable
        listctrl_set_width($a_listctrl)
    EndIf
EndFunc

Func listctrl_set_width($a_listctrl)
    for $i = 0 to _GUICtrlListCount($a_listctrl)-1
        if $listctrl_max_width < StringLen(_GUICtrlListGetText($a_listctrl,$i)) Then 
            $listctrl_max_width = StringLen(_GUICtrlListGetText($a_listctrl,$i))
        EndIf       
    Next
    GUICtrlSetLimit($a_listctrl,$listctrl_max_width * $font_to_pixel_ratio)
    $listctrl_max_width = 0
EndFunc

Func display_list_items($a_listctrl)
    for $i = 0 to _GUICtrlListCount($a_listctrl)-1
        if $i = 0 Then
            $listctrl_content = _GUICtrlListGetText($a_listctrl,$i)
        Else
            $listctrl_content = $listctrl_content & '|' &  _GUICtrlListGetText($a_listctrl,$i)
        EndIf
    Next
    Return $listctrl_content
EndFunc


;================
; Functions for Doubleclick
;================

Func List_DoubleClick($a_listctrl)
    listctrl_shellexecute_selected_item($a_listctrl)
EndFunc

Func MY_WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    Local $nNotifyCode = _HiWord($wParam)
    Local $nID = _LoWord($wParam)
    Local $hCtrl = $lParam
    Local Const $LBN_DBLCLK = 2;
    Switch $nID
        Case $listctrl
            Switch $nNotifyCode
                Case $LBN_DBLCLK
                    List_DoubleClick($listctrl)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

Func _HiWord($x)
    Return BitShift($x, 16)
EndFunc   ;==>_HiWord

Func _LoWord($x)
    Return BitAND($x, 0xFFFF)
EndFunc   ;==>_LoWord
Edited by Polyphem

This post will be edited again by Polyphem: Tomorrow, 11:55 AM

Share this post


Link to post
Share on other sites
PaulIA

Just for some additional reference, you might want to take a look at A3LListbox.au3 in Auto3Lib. There is a function that I use to adjust the width of the list box and you can use regular fonts. There is also an InsertString function that allows you to insert a string anywhere in the list box that you want.


Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites
Polyphem

Nice, thanks, will have a look. Width seems easy, and setting an item on top also by first querying the amount of items and then set a new one +1.


This post will be edited again by Polyphem: Tomorrow, 11:55 AM

Share this post


Link to post
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
Sign in to follow this  

×