Sign in to follow this  
Followers 0
VelvetElvis

Listbox problem

5 posts in this topic

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?

Share this post


Link to post
Share on other sites



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: :)

#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._______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

 

Share this post


Link to post
Share on other sites

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: :)

#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!

Share this post


Link to post
Share on other sites

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._______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

 

Share this post


Link to post
Share on other sites

Yes, I have that covered. Thanks again!

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  
Followers 0