Jump to content

Listbox question


Recommended Posts

I was creating a listbox to add items into which was working fine but I have a problem if the user adds the item in the wrong order i need them to be able to move an item up or down on the list. This is proving to be much more difficult than I thought. Any ideas or is there something blatent that I am missing.

Also I am using a function call from CyperSlugs wonderful list that will delete a selected item from the list but now the variable I used to populate the list is no longer valid so I must read in all the data from that listbox before continuing on. I can read just a selected item but how do I read in all of them into a variable?



Link to comment
Share on other sites


How about just reading in all the information from the listbox?

If I could read in all the data I could probably do a string search for the selected item. delete it from the string and insert it one item earlier and then re-set the data to the list box, but I still can't figure out how to read all the items from the list box. probably because I haven't figured out the whole GUICtrlSendMsg thing yet... :lmao:

Link to comment
Share on other sites

; Script generated by AutoBuilder 0.5 Prototype

#include <GuiConstants.au3>

If Not IsDeclared('WS_CLIPSIBLINGS') Then Global $WS_CLIPSIBLINGS = 0x04000000

$title = "Type in box and hit Enter..."
$GUI = GuiCreate($title, 392, 323,(@DesktopWidth-392)/2, (@DesktopHeight-323)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

$List = GuiCtrlCreateList("", 120, 20, 120, 201, 0x0) ;we don't want sorting!
    GuiCtrlSetData($List, "some|preset|data|for|you")
$Input_Item = GuiCtrlCreateInput("", 30, 270, 130, 20)
    GuiCtrlSetState($Input_Item, $GUI_FOCUS)
$Button_Add = GuiCtrlCreateButton("Insert Item", 190, 260, 90, 50)
    GuiCtrlSetState($Button_Add, $GUI_DEFBUTTON)
$Button_Up = GuiCtrlCreateButton("UP", 270, 50, 40, 50)
$Button_Down = GuiCtrlCreateButton("DOWN", 270, 120, 40, 50)

While 1
    Local $selectedItem = _GuiLB_GetCurSel($List)
    $msg = GuiGetMsg()
    Case $msg = $GUI_EVENT_CLOSE
    Case $msg = $Button_Add
        Local $itemText = GuiCtrlRead($Input_Item)
        If $itemText <> "" Then
            _GuiLB_AddString($List, $itemText)
            GuiCtrlSetData($Input_Item, "")
            GuiCtrlSetState($Input_Item, $GUI_FOCUS)

    Case $msg = $Button_Up
        If $selectedItem > 0 Then
            _GuiLB_SwapString($List, $selectedItem, $selectedItem - 1)
            GuiCtrlSetState($List, $GUI_FOCUS)
            GuiCtrlSendMsg($List, 0x0185, 1, $selectedItem);LB_SETSEL
            ControlCommand($GUI, "", "ListBox1", "SetCurrentSelection", $selectedItem - 1)
    Case $msg = $Button_Down
        If $selectedItem < _GuiLB_GetCount($List)-1 And $selectedItem >= 0 Then
            _GuiLB_SwapString($List, $selectedItem, $selectedItem + 1)
            ControlCommand($GUI, "", "ListBox1", "SetCurrentSelection", $selectedItem + 1)


; If list box lacks LBS_SORT style, string is added to end of the list. Otherwise, string is inserted and list is re-sorted.
Func _GuiLB_AddString($ref, $string)
    Return GuiCtrlSendMsg($ref, 0x0180, 0, $string);LB_ADDSTRING

Func _GuiLB_GetCurSel($ref)
    Return GuiCtrlSendMsg($ref, 0x0188, 0, 0);LB_GETCURSEL

Func _GuiLB_SwapString($ref, $j, $k)
  Local $itemJ = GuiCtrlRecvMsg($ref, 0x0189, $j, 1);LB_GETTEXT
  Local $itemK = GuiCtrlRecvMsg($ref, 0x0189, $k, 1);LB_GETTEXT
  GuiCtrlSendMsg($ref, 0x0182, $j, 0);LB_DELETESTRING
  GuiCtrlSendMsg($ref, 0x181, $j, $itemK);LB_INSERTSTRING
  GuiCtrlSendMsg($ref, 0x0182, $k, 0);LB_DELETESTRING
  GuiCtrlSendMsg($ref, 0x181, $k, $itemJ);LB_INSERTSTRING

Func _GuiLB_GetCount($ref)
    Return GuiCtrlSendMsg($ref, 0x018B, 0, 0);LB_GETCOUNT

; Set's the selection set of the specified index; $flag == 0 means unselect, $flag == 1 means select
; Perhaps I could also have $flag == 2 to mean toggle.....
; An index of -1 means to apply $flag to all items.
Func _GuiLB_SetSel($ref, $flag, $index)
    If $flag == 2 Then
        If $index == -1 Then
            For $index = 0 To GuiCtrlSendMsg($ref, 0x018B, 0, 0);0 to LB_GETCOUNT
                If GuiCtrlSendMsg($ref, 0x0187, $index, 0) Then; If LB_GETSEL (state)...
                    GuiCtrlSendMsg($ref, 0x0185, 0, $index);LB_SETSEL
                    GuiCtrlSendMsg($ref, 0x0185, 1, $index);LB_SETSEL
            If GuiCtrlSendMsg($ref, 0x0187, $index, 0) Then;If Selected Then
                Return GuiCtrlSendMsg($ref, 0x0185, 0, $index);LB_SETSEL
                Return GuiCtrlSendMsg($ref, 0x0185, 1, $index);LB_SETSEL
        Return GuiCtrlSendMsg($ref, 0x0185, $flag, $index);LB_SETSEL -- Then main function

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

Thanks alot cyber slug, o:)

It would have taken me at least another 8 hours to figure all that out.

I had only gotten to the point of reading in the selected item, deleteing it, read in the remaining items, then tack the item to the end of the list using the guictrlsetdata. :lmao:

you method is a whole lot better huh..

Thanks again,


By the way, where do you keep getting all that hex stuff from for the GuiCtrlSendMsg?

Edited by MikeOsdx
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

  • Recently Browsing   0 members

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