Jump to content

Recommended Posts

Posted (edited)

; ------------------------------------------------------------------------------
;
; AutoIt Version: 3.1.1
; Language:    English
; Description:  Functions that assist with Listbox.
;
; ------------------------------------------------------------------------------
; Errors
If(Not IsDeclared("LB_ERR")) Then Global Const $LB_ERR = -1
If(Not IsDeclared("LB_ERRATTRIBUTE")) Then Global Const $LB_ERRATTRIBUTE = -3
If(Not IsDeclared("LB_ERRREQUIRED")) Then Global Const $LB_ERRREQUIRED = -4
If(Not IsDeclared("LB_ERRSPACE")) Then Global Const $LB_ERRSPACE = -2

; Messages to send to listbox
If(Not IsDeclared("LB_ADDSTRING")) Then Global Const $LB_ADDSTRING = 0x180
If(Not IsDeclared("LB_DELETESTRING")) Then Global Const $LB_DELETESTRING = 0x182
If(Not IsDeclared("LB_DIR")) Then Global Const $LB_DIR = 0x18D
If(Not IsDeclared("LB_FINDSTRING")) Then Global Const $LB_FINDSTRING = 0x18F
If(Not IsDeclared("LB_FINDSTRINGEXACT")) Then Global Const $LB_FINDSTRINGEXACT = 0x1A2
If(Not IsDeclared("LB_GETANCHORINDEX")) Then Global Const $LB_GETANCHORINDEX = 0x019D
If(Not IsDeclared("LB_GETCARETINDEX")) Then Global Const $LB_GETCARETINDEX = 0x019F
If(Not IsDeclared("LB_GETCOUNT")) Then Global Const $LB_GETCOUNT = 0x18B
If(Not IsDeclared("LB_GETCURSEL")) Then Global Const $LB_GETCURSEL = 0x188
If(Not IsDeclared("LB_GETLOCALE")) Then Global Const $LB_GETLOCALE = 0x1A6
If(Not IsDeclared("LB_GETSEL")) Then Global Const $LB_GETSEL = 0x0187
If(Not IsDeclared("LB_GETSELCOUNT")) Then Global Const $LB_GETSELCOUNT = 0x0190
If(Not IsDeclared("LB_GETTEXT")) Then Global Const $LB_GETTEXT = 0x0189
If(Not IsDeclared("LB_GETTEXTLEN")) Then Global Const $LB_GETTEXTLEN = 0x018A
If(Not IsDeclared("LB_GETTOPINDEX")) Then Global Const $LB_GETTOPINDEX = 0x018E
If(Not IsDeclared("LB_INSERTSTRING")) Then Global Const $LB_INSERTSTRING = 0x181
If(Not IsDeclared("LB_RESETCONTENT")) Then Global Const $LB_RESETCONTENT = 0x184
If(Not IsDeclared("LB_SELECTSTRING")) Then Global Const $LB_SELECTSTRING = 0x18C
If(Not IsDeclared("LB_SETITEMHEIGHT")) Then Global Const $LB_SETITEMHEIGHT = 0x1A0
If(Not IsDeclared("LB_SELITEMRANGE")) Then Global Const $LB_SELITEMRANGE = 0x19B
If(Not IsDeclared("LB_SELITEMRANGEEX")) Then Global Const $LB_SELITEMRANGEEX = 0x0183
If(Not IsDeclared("LB_SETLOCALE")) Then Global Const $LB_SETLOCALE = 0x1A5
If(Not IsDeclared("LB_SETSEL")) Then Global Const $LB_SETSEL = 0x0185
If(Not IsDeclared("LB_SETTOPINDEX")) Then Global Const $LB_SETTOPINDEX = 0x197

; attributes
If(Not IsDeclared("DDL_ARCHIVE")) Then Global Const $DDL_ARCHIVE = 0x20
If(Not IsDeclared("DDL_DIRECTORY")) Then Global Const $DDL_DIRECTORY = 0x10
If(Not IsDeclared("DDL_DRIVES")) Then Global Const $DDL_DRIVES = 0x4000
If(Not IsDeclared("DDL_EXCLUSIVE")) Then Global Const $DDL_EXCLUSIVE = 0x8000
If(Not IsDeclared("DDL_HIDDEN")) Then Global Const $DDL_HIDDEN = 0x2
If(Not IsDeclared("DDL_READONLY")) Then Global Const $DDL_READONLY = 0x1
If(Not IsDeclared("DDL_READWRITE")) Then Global Const $DDL_READWRITE = 0x0
If(Not IsDeclared("DDL_SYSTEM")) Then Global Const $DDL_SYSTEM = 0x4

;===============================================================================
;
; Description:          _GUICtrlListBoxAddItem
; Parameter(s):     $h_listbox - control id
;                           $s_text - string to add
; Requirement:          None
; Return Value(s):  The return value is the zero-based index of the string in 
;                           the list box. If an error occurs, the return value is LB_ERR.
;                           If there is insufficient space to store the new string, 
;                           the return value is LB_ERRSPACE. 
; User CallTip:     _GUICtrlListBoxAddItem(control id,"string") Add an item to the List (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
; Note(s):              If the list box does not have the LBS_SORT style, the string is added to the
;                           end of the list. Otherwise, the string is inserted into the list and the list
;                           is sorted.
;
;===============================================================================
Func _GUICtrlListBoxAddItem($h_listbox,$s_text)
   Return GUICtrlSendMsg ( $h_listbox, $LB_ADDSTRING , 0, $s_text )
EndFunc

;===============================================================================
;
; Description:          _GUICtrlListBoxDeleteItem
; Parameter(s):     $h_listbox - control id
;                           $i_index - index of item to delete
; Requirement:          None
; Return Value(s):  The return value is a count of the strings remaining in the list.
;                           The return value is LB_ERR if the $i_index parameter specifies an 
;                           index greater than the number of items in the list.
; User CallTip:     _GUICtrlListBoxDeleteItem(control id,index) Delete an Item from the List (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
; Note(s):              None
;
;===============================================================================
Func _GUICtrlListBoxDeleteItem($h_listbox,$i_index)
   Return GUICtrlSendMsg ( $h_listbox, $LB_DELETESTRING , $i_index, 0 )
EndFunc

;===============================================================================
;
; Description:          _GUICtrlListBoxCount
; Parameter(s):     $h_listbox - control id
; Requirement:          None
; Return Value(s):  The return value is the number of items in the list box
;                           or LB_ERR if an error occurs
; User CallTip:     _GUICtrlListBoxCount(control id) return the number of items in the list box (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
; Note(s):              None
;
;===============================================================================
Func _GUICtrlListBoxCount($h_listbox)
   Return GUICtrlSendMsg ( $h_listbox, $LB_GETCOUNT , 0, 0 )
EndFunc

;===============================================================================
;
; Description:          _GUICtrlListBoxFind
; Parameter(s):     $h_listbox - control id
;                           $s_search - string to search for
;                           $i_exact - exact match or not
; Requirement:          None
; Return Value(s):  The return value is the index of the matching item, 
;                           or LB_ERR if the search was unsuccessful.
; User CallTip:     _GUICtrlListBoxFind(control id,"search string"[,exact]) return the index of matching item (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
; Note(s):              find the first string in a list box that begins with the specified string.
;                           if exact is specified find the first list box string that exactly matches
;                   the specified string, except that the search is not case sensitive
;===============================================================================
Func _GUICtrlListBoxFind($h_listbox,$s_search,$i_exact=0)
   If($i_exact) Then
      Return GUICtrlSendMsg ( $h_listbox, $LB_FINDSTRINGEXACT , -1, $s_search )
   Else
      Return GUICtrlSendMsg ( $h_listbox, $LB_FINDSTRING , -1, $s_search )
   EndIf
EndFunc

;===============================================================================
;
; Description:          _GUICtrlListBoxSelectedIndex
; Parameter(s):     $h_listbox - control id
; Requirement:          None
; Return Value(s):  In a single-selection list box, the return value is the zero-based 
;                           index of the currently selected item. If there is no selection, 
;                           the return value is LB_ERR
; User CallTip:     _GUICtrlListBoxSelectedIndex(control id) return the index of selected item (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
; Note(s):              Do not use this with a multiple-selection list box.
;
;===============================================================================
Func _GUICtrlListBoxSelectedIndex($h_listbox)
    Return GUICtrlSendMsg ( $h_listbox, $LB_GETCURSEL , 0, 0 )
EndFunc

;===============================================================================
;
; Description:          _GUICtrlListBoxInsertItem
; Parameter(s):     $h_listbox - control id
;                           $s_text - string to insert
;                           [$i_index] - index to insert at
; Requirement:          None
; Return Value(s):  The return value is the index of the position at which the string was inserted.
;                           If an error occurs, the return value is LB_ERR. If there is insufficient space
;                           to store the new string, the return value is LB_ERRSPACE.
; User CallTip:     _GUICtrlListBoxInsertItem(control id,"string"[,index]) insert a string into the list (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
; Note(s):              If this parameter is 1, the string is added to the end of the list.
;                           Unlike the _GUICtrlListBoxAddItem, this function does not cause a list
;                           with the LBS_SORT style to be sorted.
;
;===============================================================================
Func _GUICtrlListBoxInsertItem($h_listbox,$s_text,$i_index=-1)
   Return GUICtrlSendMsg ( $h_listbox, $LB_INSERTSTRING , $i_index, $s_text )
EndFunc

;===============================================================================
;
; Description:          _GUICtrlListBoxClear
; Parameter(s):     $h_listbox - control id
; Requirement:          None
; Return Value(s):  None
; User CallTip:     _GUICtrlListBoxClear(control id) remove all items from the list box (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
; Note(s):              None
;
;===============================================================================
Func _GUICtrlListBoxClear($h_listbox)
   GUICtrlSendMsg ( $h_listbox, $LB_RESETCONTENT , 0, 0 )
EndFunc

;===============================================================================
;
; Description:          _GUICtrlListBoxSelectString
; Parameter(s):     $h_listbox - control id
;                           $s_text - string to select
;                           [$i_index] - zero-based index of the item before the first item to be searched
; Requirement:          None
; Return Value(s):  If the search is successful, the return value is the index of the selected item. 
;                           If the search is unsuccessful, the return value is LB_ERR and the current selection is not changed.
; User CallTip:     _GUICtrlListBoxSelectString(control id,"string"[,index]) select item useing search string (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
; Note(s):              If index is 1, the entire list box is searched from the beginning
;
;===============================================================================
Func _GUICtrlListBoxSelectString($h_listbox,$s_search,$i_index=-1)
   Return GUICtrlSendMsg ( $h_listbox, $LB_SELECTSTRING , $i_index, $s_search )
EndFunc

Edit: updated with more error checking

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Posted (edited)

;===================================================

; from CyberSlug

; modified for my naming conventions, and debuged as much as I

; cared to at this time

;===================================================

;===============================================================================
;
; Description:            _GUICtrlListBoxGetText
; Parameter(s):        $h_listbox - control id
;                            $i_index - Specifies the zero-based index of the string to retrieve
; Requirement:            None
; Return Value(s):    The return value is the length of the string, in TCHARs, excluding 
;                            the terminating null character. If $i_index does not specify a valid
;                            index, the return value is LB_ERR.
; User CallTip:        _GUICtrlListBoxGetText(control id, index) Returns the item (string) at the specified index (required: <ListBox.au3>)
; Author(s):            CyberSlug
; Note(s):                None
;
;===============================================================================
Func _GUICtrlListBoxGetText($h_listbox, $i_index)
    Return GuiCtrlRecvMsg($h_listbox, $LB_GETTEXT, $i_index, 1)
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxGetCaretIndex
; Parameter(s):        $h_listbox - control id
; Requirement:            multi-select style
; Return Value(s):    The return value is the zero-based index of the selected list box item.
; User CallTip:        _GUICtrlListBoxGetCaretIndex(control id) Return index of item that has the focus rectangle (required: <ListBox.au3>)
; Author(s):            CyberSlug
; Note(s):                To determine the index of the item that has the focus rectangle in a 
;                            multiple-selection list box. The item may or may not be selected
;
;===============================================================================
Func _GUICtrlListBoxGetCaretIndex($h_listbox)
    Return GuiCtrlSendMsg($h_listbox, $LB_GETCARETINDEX, 0, 0)
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxGetSelState
; Parameter(s):        $h_listbox - control id
;                            $i_index - Specifies the zero-based index of the item
; Requirement:            None
; Return Value(s):    If an item is selected, the return value is greater than zero
;                             otherwise, it is zero. If an error occurs, the return value is LB_ERR.
; User CallTip:        _GUICtrlListBoxGetSelState(control id, index) Get the selection state of item (required: <ListBox.au3>)
; Author(s):            CyberSlug
; Note(s):                None
;
;===============================================================================
Func _GUICtrlListBoxGetSelState($h_listbox, $i_index)
    Return GuiCtrlSendMsg($h_listbox, $LB_GETSEL, $i_index, 0)
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxGetSelCount
; Parameter(s):        $h_listbox - control id
; Requirement:            None
; Return Value(s):    The return value is the count of selected items in the list box.
;                            If the list box is a single-selection list box, the return value is LB_ERR.
; User CallTip:        _GUICtrlListBoxGetSelCount(control id) Get the number of items selected (required: <ListBox.au3>)
; Author(s):            CyberSlug
; Note(s):                Retrieve the total number of selected items in a multiple-selection list box. 
;                             Number of selected items (for a control with multi-select style)
;
;===============================================================================
Func _GUICtrlListBoxGetSelCount($h_listbox)
    Return GuiCtrlSendMsg($h_listbox, $LB_GETSELCOUNT, 0, 0)
EndFunc

;===============================================================================
;
; Description:            _GUIListBoxGetSelItems
; Parameter(s):        $h_listbox - control id
; Requirement:            multi-select style
; Return Value(s):    array of selected items indices, first element ($array[0]) contains the number indices returned
;                            If the list box is a single-selection list box, the return value is LB_ERR.
;                            If no items are selected, the return value is LB_ERR.
; User CallTip:        _GUIListBoxGetSelItems(control id) Get item indices of selected items (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
;                    CyberSlug
; Note(s):                DOES NOT WORK WITH SINGLE-SELECTION LIST BOXES
;
;===============================================================================
Func _GUIListBoxGetSelItems($h_listbox)
    Local $i, $list = "", $ret
    For $i = 0 To GuiCtrlSendMsg($h_listbox,$LB_GETCOUNT,0,0) - 1
        $ret = GuiCtrlSendMsg($h_listbox,$LB_GETSEL,$i,0)
        If( $ret > 0 )Then
            If(StringLen($list) > 0) Then
                $list = $list & ',' & $i 
            Else
                $list = $i
            EndIf
        ElseIf( $ret == $LB_ERR) Then 
            Return $LB_ERR
        EndIf
    Next
    If(StringLen($list) > 0) Then
        Return StringSplit($list,",")
    Else
        Return $LB_ERR
    EndIf
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxGetSelItemsText
; Parameter(s):        $h_listbox - control id
; Requirement:            multi-select style
; Return Value(s):    array of selected items text, first element ($array[0]) contains the number items returned
;                            If the list box is a single-selection list box, the return value is LB_ERR.
;                            If no items are selected, the return value is LB_ERR.
; User CallTip:        _GUICtrlListBoxGetSelItemsText(control id) Get the text of selected items (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
;                    CyberSlug
; Note(s):                DOES NOT WORK WITH SINGLE-SELECTION LIST BOXES
;
;===============================================================================
Func _GUICtrlListBoxGetSelItemsText($h_listbox)
    Local $i, $list = "", $a_text[1], $ret
    For $i = 0 To GuiCtrlSendMsg($h_listbox,$LB_GETCOUNT,0,0) - 1;0 to GetCount
        $ret = GuiCtrlSendMsg($h_listbox,$LB_GETSEL,$i,0)
        If( $ret > 0 )Then
            If(StringLen($list) > 0) Then
                $list = $list & ',' & $i 
            Else
                $list = $i
            EndIf
        ElseIf( $ret == $LB_ERR) Then 
            Return $LB_ERR
        EndIf
    Next;$list should now equal the selected items

    If(StringLen($list) > 0) Then
        Local $a_indices = StringSplit( $list, "," );StringSplit(GetSelItems...
        If @error Then Return $LB_ERR
            Redim $a_text[UBound($a_indices)]
            $a_text[0] = UBound($a_text) - 1
        For $i = 1 to $a_indices[0]
            $a_text[$i] = GuiCtrlRecvMsg($h_listbox,$LB_GETTEXT,$a_indices[$i],1)
        Next
        Return $a_text
    Else
        Return $LB_ERR
    EndIf
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxGetTopIndex
; Parameter(s):        $h_listbox - control id
; Requirement:            None
; Return Value(s):    The return value is the index of the first visible item in the list box.
;                            If the list is empty then $LB_ERR is returned.
; User CallTip:        _GUICtrlListBoxGetTopIndex(control id) retrieve the index of the first visible item in a list (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
;                    CyberSlug
; Note(s):                Initially the item with index 0 is at the top of the list box, but if
;                             the list box contents have been scrolled another item may be at the top.
;                             Returns index of the first visible item in the list box
;                             useful since contents for a long list will scroll
;
;===============================================================================
Func _GUICtrlListBoxGetTopIndex($h_listbox)
    If(Not GuiCtrlSendMsg($h_listbox,$LB_GETCOUNT,0,0)) Then
        Return $LB_ERR
    Else
        Return GuiCtrlSendMsg($h_listbox, $LB_GETTOPINDEX, 0, 0)
    EndIf
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxSetTopIndex
; Parameter(s):        $h_listbox - control id
;                            $i_index - Specifies the zero-based index of the item
; Requirement:            None
; Return Value(s):    If an error occurs, the return value is LB_ERR
; User CallTip:        _GUICtrlListBoxSetTopIndex($h_listbox, $i_index) ensure that a particular item in a list box is visible (required: <ListBox.au3>)
; Author(s):            CyberSlug
; Note(s):                None
;
;===============================================================================
Func _GUICtrlListBoxSetTopIndex($h_listbox, $i_index)
    Return GuiCtrlSendMsg($h_listbox, $LB_SETTOPINDEX, $i_index, 0)
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxSetSel
; Parameter(s):        $h_listbox - control id
;                            [$i_index] - Specifies the zero-based index of the item
; Requirement:            multi-select style
; Return Value(s):    If an error occurs, the return value is LB_ERR
; User CallTip:        _GUICtrlListBoxSetSel(control id, flag[, index]) select string(s) in a multiple-selection list box (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
;                    CyberSlug
; Note(s):                DOES NOT WORK WITH SINGLE-SELECTION LIST BOXES
;                             $i_flag == 0 means unselect
;                             $i_flag == 1 means select
;                             $i_flag == 2 to mean toggle.....
;                             An index of -1 means to apply $i_flag to all items.
;
;===============================================================================
Func _GUICtrlListBoxSetSel($h_listbox, $i_flag, $i_index = -1)
    Local $ret
    If $i_flag == 2 Then
        If $i_index == -1 Then; toggle all
            For $i_index = 0 To GuiCtrlSendMsg($h_listbox, $LB_GETCOUNT, 0, 0) - 1
                $ret = GuiCtrlSendMsg($h_listbox,$LB_GETSEL,$i_index,0)
                If($ret == $LB_ERR) Then Return $LB_ERR
                If( $ret > 0 )Then;If Selected Then
                    $ret = GuiCtrlSendMsg($h_listbox, $LB_SETSEL, 0, $i_index)
                Else
                    $ret = GuiCtrlSendMsg($h_listbox, $LB_SETSEL, 1, $i_index)
                EndIf
                If($ret == $LB_ERR) Then Return $LB_ERR
            Next
        Else; toggle state of index
            If GuiCtrlSendMsg($h_listbox, $LB_GETSEL, $i_index, 0) Then;If Selected Then
                Return GuiCtrlSendMsg($h_listbox, $LB_SETSEL, 0, $i_index)
            Else
                Return GuiCtrlSendMsg($h_listbox, $LB_SETSEL, 1, $i_index)
            EndIf
        EndIf
    Else; set state
        Return GuiCtrlSendMsg($h_listbox, $LB_SETSEL, $i_flag, $i_index); Then main function
    EndIf
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxGetAnchorIndex
; Parameter(s):        $h_listbox - control id
; Requirement:            multi-select style
; Return Value(s):    The return value is the index of the anchor item.
;                            If an error occurs, the return value is LB_ERR
; User CallTip:        _GUICtrlListBoxGetAnchorIndex(control id) Get the Anchor Idex (required: <ListBox.au3>)
; Author(s):            CyberSlug
; Note(s):                DOES NOT WORK WITH SINGLE-SELECTION LIST BOXES
;                         This might not always be the first selected item--especially
;                          if you select every other item via Ctrl+Click...
;
;===============================================================================
Func _GUICtrlListBoxGetAnchorIndex($h_listbox)
    Return GuiCtrlSendMsg($h_listbox, $LB_GETANCHORINDEX, 0, 0)
EndFunc
 
;===============================================================================
;
; Description:          _GUICtrlListBoxSelItemRange
; Parameter(s):     $h_listbox - control id
;                           $i_flag - set/remove select
;                           $i_start - zero-based index of the first item to select
;                           $i_stop -zero-based index of the last item to select
; Requirement:          multi-select style
; Return Value(s):  If an error occurs, the return value is LB_ERR
; User CallTip:     _GUICtrlListBoxSelItemRange(control id, flag, start, stop) select range by index in a multiple-selection list box (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
;                   CyberSlug
; Note(s):              DOES NOT WORK WITH SINGLE-SELECTION LIST BOXES
;                           Select items from $i_start to $stop indices (inclusive)
;                           $i_flag == 1 selects
;                           $i_flag == 0 removes select
;
;===============================================================================
Func _GUICtrlListBoxSelItemRange($h_listbox, $i_flag, $i_start, $i_stop)
Return GuiCtrlSendMsg($h_listbox,$LB_SELITEMRANGE,$i_flag,$i_stop* 65536 + $i_start)
EndFunc
;===============================================================================
;
; Description:            _GUICtrlListBoxSelItemRangeEx
; Parameter(s):        $h_listbox - control id
;                            $i_start - zero-based index of the first item to select
;                            $i_stop -zero-based index of the last item to select
; Requirement:            multi-select style
; Return Value(s):    If an error occurs, the return value is LB_ERR
; User CallTip:        _GUICtrlListBoxSelItemRangeEx(control id, start, stop) Selects items from $i_start to $i_stop (required: <ListBox.au3>)
; Author(s):            CyberSlug
; Note(s):                DOES NOT WORK WITH SINGLE-SELECTION LIST BOXES
;                          If $i_start > $i_stop Then items are un-selected
;
;===============================================================================
Func _GUICtrlListBoxSelItemRangeEx($h_listbox, $i_start, $i_stop)
    Return GuiCtrlSendMsg($h_listbox, $LB_SELITEMRANGEEX, $i_start, $i_stop)
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxReplaceString
; Parameter(s):        $h_listbox - control id
;                            $i_index - zero-based index of the item to replace
;                            $s_newString - string to replace old string
; Requirement:            None
; Return Value(s):    If an error occurs, the return value is LB_ERR
; User CallTip:        _GUICtrlListBoxReplaceString(control id, index, "New String") Replaces the text of an item at index (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
;                    CyberSlug
; Note(s):                None
;
;===============================================================================
Func _GUICtrlListBoxReplaceString($h_listbox, $i_index, $s_newString)
  If(GuiCtrlSendMsg($h_listbox, $LB_DELETESTRING, $i_index, 0) == $LB_ERR) Then Return $LB_ERR
  If(GuiCtrlSendMsg($h_listbox, $LB_INSERTSTRING, $i_index, $s_newString) == $LB_ERR) Then Return $LB_ERR
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxSwapString
; Parameter(s):        $h_listbox - control id
;                            $i_indexA - zero-based index item to swap
;                            $i_indexB - zero-based index item to swap
; Requirement:            None
; Return Value(s):    If an error occurs, the return value is LB_ERR
; User CallTip:        _GUICtrlListBoxSwapString(control id, index, index) Swaps the text of two items at the specified indices (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
;                    CyberSlug
; Note(s):                None
;
;===============================================================================
Func _GUICtrlListBoxSwapString($h_listbox, $i_indexA, $i_indexB)
    Local $itemA = GuiCtrlRecvMsg($h_listbox, $LB_GETTEXT, $i_indexA, 1)
    Local $itemB = GuiCtrlRecvMsg($h_listbox, $LB_GETTEXT, $i_indexB, 1)
    If($itemA == $LB_ERR Or $itemB == $LB_ERR) Then Return $LB_ERR
    If(GuiCtrlSendMsg($h_listbox, $LB_DELETESTRING, $i_indexA, 0) == $LB_ERR) Then Return $LB_ERR
    If(GuiCtrlSendMsg($h_listbox, $LB_INSERTSTRING, $i_indexA, $itemB) == $LB_ERR) Then Return $LB_ERR
      
    If(GuiCtrlSendMsg($h_listbox, $LB_DELETESTRING, $i_indexB, 0) == $LB_ERR) Then Return $LB_ERR
    If(GuiCtrlSendMsg($h_listbox, $LB_INSERTSTRING, $i_indexB, $itemA) == $LB_ERR) Then Return $LB_ERR
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxSort
; Parameter(s):        $h_listbox - control id
; Requirement:            None
; Return Value(s):    If an error occurs, the return value is LB_ERR
; User CallTip:        _GUICtrlListBoxSort(control id) Re-sorts list box if it has the LBS_SORT style (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
;                    CyberSlug
; Note(s):                Re-sorts list box if it has the LBS_SORT style
;                          Might be useful if you use InsertString
;
;===============================================================================
Func _GUICtrlListBoxSort($h_listbox)
    Local $bak = GuiCtrlRecvMsg($h_listbox, $LB_GETTEXT, 0, 1)
    If($bak == $LB_ERR) Then Return $LB_ERR
    If(GuiCtrlSendMsg($h_listbox, $LB_DELETESTRING, 0, 0) == $LB_ERR) Then Return $LB_ERR
    Return GuiCtrlSendMsg($h_listbox, $LB_ADDSTRING, 0, $bak)
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxGetTextLen
; Parameter(s):        $h_listbox - control id
;                            $i_index - zero-based index of item
; Requirement:            None
; Return Value(s):    The return value is the length of the string
;                            If the $i_index  parameter does not specify a valid index, the return value is LB_ERR
; User CallTip:        _GUICtrlListBoxGetTextLen(control id, index) alternative to StringLen (required: <ListBox.au3>)
; Author(s):            CyberSlug
; Note(s):                None
;
;===============================================================================
Func _GUICtrlListBoxGetTextLen($h_listbox, $i_index)
    Return GuiCtrlSendMsg($h_listbox, $LB_GETTEXTLEN, $i_index, 0)
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxGetLocale
; Parameter(s):        $h_listbox - control id
; Requirement:            None
; Return Value(s):    Returns the current Local of the listbox
;                             same as @OSLang unless changed
; User CallTip:        _GUICtrlListBoxGetLocale(control id) current Local of the listbox (required: <ListBox.au3>)
; Author(s):            CyberSlug
; Note(s):                None
;
;===============================================================================
Func _GUICtrlListBoxGetLocale($h_listbox)
    Return Hex( GuiCtrlSendMsg($h_listbox, $LB_GETLOCALE, 0, 0) , 4)
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxSetLocale
; Parameter(s):        $h_listbox - control id
;                            $v_locale - locale
; Requirement:            None
; Return Value(s):    returns previous locale or $LB_ERR
; User CallTip:        _GUICtrlListBoxSetLocale($h_listbox, $v_locale) set the locale (required: <ListBox.au3>)
; Author(s):            CyberSlug
; Note(s):                "0409" for U.S. English
;                            see @OSLang for string values
;
;===============================================================================
Func _GUICtrlListBoxSetLocale($h_listbox, $v_locale);$v_locale == "0409" for U.S. English
    Return Hex( GuiCtrlSendMsg($h_listbox, $LB_SETLOCALE, Dec($v_locale), 0) , 4)
EndFunc

;===============================================================================
;
; Description:            _GUICtrlListBoxSetItemHeight
; Parameter(s):        $h_listbox - control id
;                            $i_index - zero-based index of item
;                            $i_height - The height of the item
; Requirement:            None
; Return Value(s):    If the index or height is invalid, the return value is LB_ERR
; User CallTip:        _GUICtrlListBoxSetItemHeight(control id, index, height) set the height of the items (required: <ListBox.au3>)
; Author(s):            CyberSlug
; Note(s):                Affects all items unless the list box has the LBS_OWNERDRAWVARIABLE style = 0x20
;                             However, text is not visible in the list when that style is used....
;
;===============================================================================
Func _GUICtrlListBoxSetItemHeight($h_listbox, $i_index, $i_height)
    Return GuiCtrlSendMsg($h_listbox, $LB_SETITEMHEIGHT, $i_index, $i_height)
EndFunc

and this one CyberSlug started on was "dir" in his attachment

from my testing it works

tested with the following examples:

GUICreate("List Box testing",400,100,-1,-1)
$listbox = GUICtrlCreateList("",140,10,120,80)
MsgBox(0,"",_GUICtrlListBoxAddDir($listbox,"drives,e"))
;MsgBox(0,"",_GUICtrlListBoxAddDir($listbox,"d,e,a","c:\temp\*.au3"))
GUISetState ()
While 1
   $msg = GUIGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    EndSelect
Wend

;===============================================================================
;
; Description:            _GUICtrlListBoxAddDir
; Parameter(s):        $h_listbox - control id
;                            $s_Attributes - comma-delimited string
;                            [$s_file] - what to get i.e *.*
; Requirement:            $s_file for all but drives attribute
; Return Value(s):    zero-based index of the last name added to the list
;                            If an error occurs, the return value is LB_ERR. 
;                            If there is insufficient space to store the new strings, the return value is LB_ERRSPACE
; User CallTip:        _GUICtrlListBoxAddDir(control id,"Attributes"[,"file"]) Add names to the list displayed by the list box (required: <ListBox.au3>)
; Author(s):            Gary Frost (custompcs@charter.net)
;                    CyberSlug
; Note(s):                $s_Attributes is an comma-delimited string
;                             valid values are any of the following:
;                                 A,D,H,RO,RW,S,E,Drives
;
;===============================================================================
Func _GUICtrlListBoxAddDir($h_listbox,$s_Attributes,$s_file="")
    Dim $i, $v_Attributes = "",$is_drives=0
    Dim $a_Attributes = StringSplit($s_Attributes,",")
    For $i = 1 To $a_Attributes[0]
        Select
            Case StringUpper($a_Attributes[$i]) = "A"
                If(StringLen($v_Attributes) > 0) Then
                    $v_Attributes = $v_Attributes + $DDL_ARCHIVE
                Else
                    $v_Attributes = $DDL_ARCHIVE
                EndIf
            Case StringUpper($a_Attributes[$i]) = "D"
                If(StringLen($v_Attributes) > 0) Then
                    $v_Attributes = $v_Attributes + $DDL_DIRECTORY
                Else
                    $v_Attributes = $DDL_DIRECTORY
                EndIf
            Case StringUpper($a_Attributes[$i]) = "H"
                If(StringLen($v_Attributes) > 0) Then
                    $v_Attributes = $v_Attributes + $DDL_HIDDEN
                Else
                    $v_Attributes = $DDL_HIDDEN
                EndIf
            Case StringUpper($a_Attributes[$i]) = "RO"
                If(StringLen($v_Attributes) > 0) Then
                    $v_Attributes = $v_Attributes + $DDL_READONLY
                Else
                    $v_Attributes = $DDL_READONLY
                EndIf
            Case StringUpper($a_Attributes[$i]) = "RW"
                If(StringLen($v_Attributes) > 0) Then
                    $v_Attributes = $v_Attributes + $DDL_READWRITE
                Else
                    $v_Attributes = $DDL_READWRITE
                EndIf
            Case StringUpper($a_Attributes[$i]) = "S"
                If(StringLen($v_Attributes) > 0) Then
                    $v_Attributes = $v_Attributes + $DDL_SYSTEM
                Else
                    $v_Attributes = $DDL_SYSTEM
                EndIf
            Case StringUpper($a_Attributes[$i]) = "DRIVES"
                $is_drives = 1
                If(StringLen($v_Attributes) > 0) Then
                    $v_Attributes = $v_Attributes + $DDL_DRIVES
                Else
                    $v_Attributes = $DDL_DRIVES
                EndIf
            Case StringUpper($a_Attributes[$i]) = "E"
                If(StringLen($v_Attributes) > 0) Then
                    $v_Attributes = $v_Attributes + $DDL_EXCLUSIVE
                Else
                    $v_Attributes = $DDL_EXCLUSIVE
                EndIf
            Case Else
            ; invalid attribute
                SetError(1)
                Return $LB_ERRATTRIBUTE
            EndSelect
        Next
    If(Not $is_drives And StringLen($s_file) == 0) Then
        SetError(1)
        Return $LB_ERRREQUIRED
    EndIf
    Return GuiCtrlSendMsg($h_listbox, $LB_DIR, $v_Attributes, $s_file)
EndFunc

Edit: Added Case Else to _GUICtrlListBoxAddDir

Edit2: Added more error checking, corrected bugs found

Edit3: Fixed _GUICtrlListBoxSelItemRange to do the correct call using the dword for lParam thanks to jpm

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Posted

gafrost, do you have time to get this functions into the "official" AutoIt UDF format? I think a lot of people would benefit if we could get these included with the next major AutoIt release

http://www.autoitscript.com/fileman/users/jdeb/UDFs/udf_standards.htm

Might want to check with JdeB that the standards are up to date.

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
  • Developers
Posted (edited)

Yea these would be very usefull to add..

The standards are up-to-date, only thing that currently doesn't work is the upload to the public area (think that was abandoned due to people messing it up)

Just mail/PM the files to me in a ZIP file ... :)

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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
  • Recently Browsing   0 members

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