VelvetElvis Posted April 12, 2012 Share Posted April 12, 2012 I have an array consisting of a set of names and their related email addresses, separated by a pipe character, e.g. Brian Jones|bjones@gmail.com Fred Farkle|ffarkle@aol.com I want to display the names in a listbox, and when selected, return the email address associated with the name. I've split the lines into the appropriate arrays, but can't get the _GUICtrlListBox_SetItemData() to work the way I thought it's supposed to. If I understand _GUICtrlListBox_Create() properly, I can do this. I'm having trouble with the function, and the help file example is way beyond me. Here's a snippet of what I have so far: #include <Array.au3> #include <EditConstants.au3> #include <File.au3> #include <GUIConstantsEx.au3> #include <GuiListBox.au3> #include <ListBoxConstants.au3> #include <WindowsConstants.au3> Global $inputfile = @ScriptDir & "\input.txt" Global $aStaff ; Create GUI $GUI = GUICreate("Multi Texter", 350, 400, Default, Default, $WS_SIZEBOX) $ListBox = _GUICtrlListBox_Create($GUI, "", 10, 30, 300, 90, BitOR($WS_HSCROLL, $WS_VSCROLL, $WS_BORDER, $LBS_HASSTRINGS, $LBS_MULTIPLESEL)) $editbox = GUICtrlCreateEdit("", 10, 125, 300, 135, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL), $WS_SIZEBOX + $WS_SYSMENU) $label = GUICtrlCreateLabel("Select One or More Associates", 10, 10) $SendButton = GUICtrlCreateButton("Send", 10, 285, 100, 30) $ExitButton = GUICtrlCreateButton("Exit", 10, 330, 100, 30) _FileReadToArray($inputfile, $aStaff) For $x = 1 To UBound($aStaff) - 1 $tmp = StringSplit($aStaff[$x], "|") _GUICtrlListBox_AddString($ListBox, $tmp[1]) _GUICtrlListBox_SetItemData($ListBox, $x, $tmp[2]) Next GUISetState() MsgBox(0, "", _GUICtrlListBox_GetItemData($ListBox, 2)) <---- Returns "0" I obviously missed something critical, because my msgbox test returns zeroes. Can someone enlighten me? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 12, 2012 Moderators Share Posted April 12, 2012 VelvetElvis, The "data" referred to in those functions is usually the CommandID associated with the control. As such it is limited to 4 digits - or the binary equivalent of 4 characters as shown here: #include <Array.au3> #include <EditConstants.au3> #include <File.au3> #include <GUIConstantsEx.au3> #include <GuiListBox.au3> #include <ListBoxConstants.au3> #include <WindowsConstants.au3> ; Simulate ;_FileReadToArray($inputfile, $aStaff) Global $aStaff[3] = [2, "Brian Jones|bjon", "Fred Farkle|ffar"] ; Limit the second part to 4 chars Global $inputfile = @ScriptDir & "input.txt" Global $aStaff ; Create GUI $GUI = GUICreate("Multi Texter", 350, 400, Default, Default, $WS_SIZEBOX) $ListBox = _GUICtrlListBox_Create($GUI, "", 10, 30, 300, 90, BitOR($WS_HSCROLL, $WS_VSCROLL, $WS_BORDER, $LBS_HASSTRINGS, $LBS_MULTIPLESEL)) $editbox = GUICtrlCreateEdit("", 10, 125, 300, 135, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL), $WS_SIZEBOX + $WS_SYSMENU) $label = GUICtrlCreateLabel("Select One or More Associates", 10, 10) $SendButton = GUICtrlCreateButton("Send", 10, 285, 100, 30) $ExitButton = GUICtrlCreateButton("Exit", 10, 330, 100, 30) For $x = 1 To UBound($aStaff) - 1 $tmp = StringSplit($aStaff[$x], "|") _GUICtrlListBox_AddString($ListBox, $tmp[1]) _GUICtrlListBox_SetItemData($ListBox, $x - 1, StringToBinary($tmp[2])) Next GUISetState() MsgBox(0, "", BinaryToString(_GUICtrlListBox_GetItemData($ListBox, 1))) ; We get the correct return One way you might go about finding the address is to look in the array you already have for the selected names and then read the associated address from there like this: expandcollapse popup#include <Array.au3> #include <EditConstants.au3> #include <File.au3> #include <GUIConstantsEx.au3> #include <GuiListBox.au3> #include <ListBoxConstants.au3> #include <WindowsConstants.au3> ; Simulate ;_FileReadToArray($inputfile, $aStaff) Global $aStaff[3] = [2, "Brian Jones|bjones@gmail.com", "Fred Farkle|ffarkle@aol.com"] Global $inputfile = @ScriptDir & "input.txt" Global $aStaff ; Create GUI $GUI = GUICreate("Multi Texter", 350, 400, Default, Default, $WS_SIZEBOX) $ListBox = _GUICtrlListBox_Create($GUI, "", 10, 30, 300, 90, BitOR($WS_HSCROLL, $WS_VSCROLL, $WS_BORDER, $LBS_HASSTRINGS, $LBS_MULTIPLESEL)) $editbox = GUICtrlCreateEdit("", 10, 125, 300, 135, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL), $WS_SIZEBOX + $WS_SYSMENU) $label = GUICtrlCreateLabel("Select One or More Associates", 10, 10) $SendButton = GUICtrlCreateButton("Send", 10, 285, 100, 30) $ExitButton = GUICtrlCreateButton("Exit", 10, 330, 100, 30) For $x = 1 To UBound($aStaff) - 1 $tmp = StringSplit($aStaff[$x], "|") _GUICtrlListBox_AddString($ListBox, $tmp[1]) Next GUISetState() ; Simulate selecting a string ConsoleWrite(_GUICtrlListBox_SetSel($ListBox, 0) & @CRLF) ; Read selection text $aSelected = _GUICtrlListBox_GetSelItemsText($ListBox) ; Find it in the array $iIndex = _ArraySearch($aStaff, $aSelected[1], 1, 0, 0, 1) ; Read the associated address $aSplit = StringSplit($aStaff[$iIndex], "|") MsgBox(0, "", $aSplit[2]) Any use? M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
VelvetElvis Posted April 12, 2012 Author Share Posted April 12, 2012 VelvetElvis, The "data" referred to in those functions is usually the CommandID associated with the control. As such it is limited to 4 digits - or the binary equivalent of 4 characters as shown here: <snip> One way you might go about finding the address is to look in the array you already have for the selected names and then read the associated address from there like this: expandcollapse popup#include <Array.au3> #include <EditConstants.au3> #include <File.au3> #include <GUIConstantsEx.au3> #include <GuiListBox.au3> #include <ListBoxConstants.au3> #include <WindowsConstants.au3> ; Simulate ;_FileReadToArray($inputfile, $aStaff) Global $aStaff[3] = [2, "Brian Jones|bjones@gmail.com", "Fred Farkle|ffarkle@aol.com"] Global $inputfile = @ScriptDir & "\input.txt" Global $aStaff ; Create GUI $GUI = GUICreate("Multi Texter", 350, 400, Default, Default, $WS_SIZEBOX) $ListBox = _GUICtrlListBox_Create($GUI, "", 10, 30, 300, 90, BitOR($WS_HSCROLL, $WS_VSCROLL, $WS_BORDER, $LBS_HASSTRINGS, $LBS_MULTIPLESEL)) $editbox = GUICtrlCreateEdit("", 10, 125, 300, 135, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL), $WS_SIZEBOX + $WS_SYSMENU) $label = GUICtrlCreateLabel("Select One or More Associates", 10, 10) $SendButton = GUICtrlCreateButton("Send", 10, 285, 100, 30) $ExitButton = GUICtrlCreateButton("Exit", 10, 330, 100, 30) For $x = 1 To UBound($aStaff) - 1 $tmp = StringSplit($aStaff[$x], "|") _GUICtrlListBox_AddString($ListBox, $tmp[1]) Next GUISetState() ; Simulate selecting a string ConsoleWrite(_GUICtrlListBox_SetSel($ListBox, 0) & @CRLF) ; Read selection text $aSelected = _GUICtrlListBox_GetSelItemsText($ListBox) ; Find it in the array $iIndex = _ArraySearch($aStaff, $aSelected[1], 1, 0, 0, 1) ; Read the associated address $aSplit = StringSplit($aStaff[$iIndex], "|") MsgBox(0, "", $aSplit[2]) Any use? M23 Thank you for the explanation, Melba23. I was hoping it were something like I remember back in my Delphi days, where you could link the text in a list to the actual values you wanted returned. And yes, your solution is perfect. Thanks! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 12, 2012 Moderators Share Posted April 12, 2012 VelvetElvis,Good. Just remember that you get an array returned by _GUICtrlListBox_GetSelItemsText and will need to loop through it if there is more than a single selection - I took some shortcuts for the example above. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
VelvetElvis Posted April 12, 2012 Author Share Posted April 12, 2012 Yes, I have that covered. Thanks again! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now