Sign in to follow this  
Followers 0
Stoex

for/next to add items to listbox

15 posts in this topic

Here is a piece of my code that is throwing me for a loop.

Using a successful 2D array, I am trying to populate a listbox.

ARRAY:

col 0    col1

smith    misc

frank    new

dole     old

I am expecting to output in a listbox:

smith   misc

frank   new

dole    old

I am getting as output"

smith

misc

frank

new

dole

old

know something is not right but what.  can anyone point out what must be obvious to the more experienced coders?  thanks, you all are great at what you do.

_ArrayDisplay ($NameList, "Payee") ;Good to go ; 2D Array

 $sData = ""
For $i = 0 To UBound($NameList) - 1
    For $j = 0 To UBound($NameList,2)-1
     $sData &= $NameList[$i][$j] & "|"

Next

Next
GUICtrlSetData($hList, $sData)
    $iCurrIndex = -1
    _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)

Share this post


Link to post
Share on other sites



Stoex,

ListBoxes only have 1 column - if you require 2 columns then you need a ListView, a very different beast. ;)

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

M

I am so sorry for the goof up,  I meant Listview.  A list here, a list there and pretty soon my newbie head is exploding.

Share this post


Link to post
Share on other sites

Stoex,

Does that mean you now know what to do or do you still require guidance? :huh:

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

M23

NO, I am still lost and doing something wrong.

I just also tried to use a similar approach to load a combo box and got the same type of results.

I think my "for"/ "next" is wrong or the "|" is missed used.

Share this post


Link to post
Share on other sites

M23

regarding my first post and subsequent communications I found the code (attached) that got me started in this direction.

It is from a different thread and I did like the approach to populating the box albeit - hardcoded was the array.

So I have learned how to do arrays and successfully got it to the point of needing to populate the box similar to what this attached code does.  The difference is it was for a 1D array and I wanted to kick it up a notch and do a 2D array thus my first post.

So i am looking for direction as to what I am doing wrong given the result as posted.

#include <GUIConstantsEx.au3>
#include <Array.au3>
#Include <GuiListBox.au3>

Global $hGUI, $hInput, $hList, $sPartialData,$asKeyWords,$sData



$hGUI = GUICreate("Example", 200, 400)
$hInput = GUICtrlCreateInput("", 5, 5, 190, 20)
$hList = GUICtrlCreateList("", 5, 30, 190, 325, BitOR(0x00100000, 0x00200000))
Keywords()
$hButton = GUICtrlCreateButton("Read", 60, 360, 80, 30)
$hUP = GUICtrlCreateDummy()
$hDOWN = GUICtrlCreateDummy()
$hENTER = GUICtrlCreateDummy()
GUISetState(@SW_SHOW, $hGUI)

; Set accelerators for Cursor up/down and Enter
Dim $AccelKeys[3][2]=[["{UP}", $hUP], ["{DOWN}", $hDOWN], ["{ENTER}", $hENTER]]
GUISetAccelerators($AccelKeys)

$sCurr_Input = ""
$iCurrIndex = -1

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hList
            $sChosen = GUICtrlRead($hList)
            If $sChosen <> "" Then GUICtrlSetData($hInput, $sChosen)
        Case $hButton
            If $sPartialData <> "" Then
                $sFinal = GUICtrlRead($hInput)
                If _ArraySearch($asKeyWords, $sFinal) > 0 Then
                    MsgBox(0, "Chosen", $sFinal)
                EndIf
            EndIf
        Case $hUP
            If $sPartialData <> "" Then
                $iCurrIndex -= 1
                If $iCurrIndex < 0 Then $iCurrIndex = 0
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
        Case $hDOWN
            If $sPartialData <> "" Then
                $iTotal = _GUICtrlListBox_GetCount($hList)
                $iCurrIndex += 1
                If $iCurrIndex > $iTotal - 1 Then $iCurrIndex = $iTotal - 1
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
        Case $hENTER
    If $iCurrIndex <> -1 Then
        $sText = _GUICtrlListBox_GetText($hList, $iCurrIndex)
        GUICtrlSetData($hInput, $sText)
        $iCurrIndex = -1
        _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
    EndIf

    EndSwitch

    ; If input has changed, refill list with matching items
    If GUICtrlRead($hInput) <> $sCurr_Input Then
        CheckInputText()
        $sCurr_Input = GUICtrlRead($hInput)
    If GUICtrlRead($hInput) = "" Then
        GUICtrlSetData($hList, "")
        GUICtrlSetData($hList, $sData)
        Endif
    EndIf

WEnd

Func Keywords()

global $sData
Local $iMax
Local $i
Global $NameList="Alan|Andrew|Barry|Brian|Colin|Cristopher|Derek|David"
; The string in data will be split into an array everywhere | is encountered
global $asKeyWords = StringSplit($NameList, "|")
If IsArray($asKeyWords) Then
    For $i = 1 to $asKeyWords[0]
    $sData &= $asKeyWords[$i] & "|"
Next
GUICtrlSetData($hList, $sData)
    $iCurrIndex = -1
    _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
EndIf


EndFunc   ;==>Keywords

Func CheckInputText()

    $sPartialData = "|" ; Start with delimiter so new data always replaces old
    Local $sInput = GUICtrlRead($hInput)
    If $sInput <> "" Then
        For $i = 1 to $asKeyWords[0]
            If StringInStr($asKeyWords[$i], $sInput) <> 0 Then $sPartialData &= $asKeyWords[$i] & "|"
        Next
        GUICtrlSetData($hList, $sPartialData)
    EndIf
EndFunc   ;==>CheckInputText

Share this post


Link to post
Share on other sites

In this code you are using a listbox, not a listview as suggested Melba

Please try this

#include <GUIConstantsEx.au3>
#Include <Array.au3>

Local $array[3][2] = [["smith", "misc"], ["frank", "new"], ["dole", "old"]]
_ArrayDisplay($array)

GUICreate("listview items", 200, 150)
$listview = GUICtrlCreateListView("col1   |col2", 10, 10, 150, 100)
For $i = 0 to UBound($array)-1
    GUICtrlCreateListViewItem($array[$i][0] & "|" & $array[$i][1], $listview)
Next
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Share this post


Link to post
Share on other sites

Beginning to be more clear...

so if I make the Listbox change to ListView, what else would change in the code listed in post #6?

Wouldn't something have to change? Does changing to ListView change other lines in the code in #6?

What should I look for?

I made the change in the For/Next as mikell suggested and can get the Listview to populate but when typing into the input box nothing happens as when it does in the original code posted in #6.

Share this post


Link to post
Share on other sites

All your code in post #6 is built around the use of a ListBox

If you use a ListView, I suggest to have a look at the ControlListView and _GuiCtrlListView_*  functions

Share this post


Link to post
Share on other sites

I have done that and still struggling with it.  That code in #6 is from the forum, I just wanted to try it with ListView and you pointed out the first difference I needed to make - that is done.

In #6, when a user types in the input box, the ListBox scrolls to those letters/words.  making the change to ListView - the input box doesn't work the same.  I could not find an auto-complete type function for ListView as does exist with a Combo box.  

Knowing what areas of the balance of the code in #6 that should be changed would help.  I want to try this myself but don't really know which areas are impacted by the change from ListBox to ListView.  Your example on the populating the ListView helped me figure that part out.

Share this post


Link to post
Share on other sites

OK, if you want to keep this code there is a simple alternative to the listview : for the 2nd column you can make a twin listbox and manage the 2 listboxes together

This should work

#include <GUIConstantsEx.au3>
#include <Array.au3>
#Include <GuiListBox.au3>

Global $sPartialData, $sPartialData2, $asKeyWords, $asKeyWords2, $sData, $sData2

Global $array[6][2] = [["Alan", "misc1"], ["Andrew", "misc2"], ["Barry", "new1"], ["Brian", "new2"], ["Colin", "old1"], ["Cristopher", "old2"]]

$hGUI = GUICreate("Example", 200, 400)
$hInput = GUICtrlCreateInput("", 5, 5, 91, 20)
$hInput2 = GUICtrlCreateInput("", 95, 5, 100, 20)
$hList = GUICtrlCreateList("", 5, 30, 91, 325, BitOR(0x00100000, 0x00200000))
$hList2 = GUICtrlCreateList("", 95, 30, 100, 325, BitOR(0x00100000, 0x00200000))
Keywords()
$hButton = GUICtrlCreateButton("Read", 60, 360, 80, 30)
$hUP = GUICtrlCreateDummy()
$hDOWN = GUICtrlCreateDummy()
$hENTER = GUICtrlCreateDummy()
GUISetState(@SW_SHOW, $hGUI)

; Set accelerators for Cursor up/down and Enter
Dim $AccelKeys[3][2]=[["{UP}", $hUP], ["{DOWN}", $hDOWN], ["{ENTER}", $hENTER]]
GUISetAccelerators($AccelKeys)

$sCurr_Input = ""
$iCurrIndex = -1

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hList
            $sChosen = GUICtrlRead($hList)
            If $sChosen <> "" Then 
                   For $i = 1 to $asKeyWords[0]
                        If $asKeyWords[$i] = $sChosen Then Exitloop
                   Next
                  GUICtrlSetData($hInput, $asKeyWords[$i])
                  GUICtrlSetData($hInput2, $asKeyWords2[$i])
            EndIf
        Case $hButton
            If $sPartialData <> "" Then
                $sFinal = GUICtrlRead($hInput)
                $index = _ArraySearch($asKeyWords, $sFinal)
                If $index > 0 Then
                    MsgBox(0, "Chosen", $asKeyWords[$index] & " - " & $asKeyWords2[$index])
                EndIf
            EndIf
        Case $hUP
            If $sPartialData <> "" Then
                $iCurrIndex -= 1
                If $iCurrIndex < 0 Then $iCurrIndex = 0
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
        Case $hDOWN
            If $sPartialData <> "" Then
                $iTotal = _GUICtrlListBox_GetCount($hList)
                $iCurrIndex += 1
                If $iCurrIndex > $iTotal - 1 Then $iCurrIndex = $iTotal - 1
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
        Case $hENTER
    If $iCurrIndex <> -1 Then
        $sText = _GUICtrlListBox_GetText($hList, $iCurrIndex)
        $sText2 = _GUICtrlListBox_GetText($hList2, $iCurrIndex)
        GUICtrlSetData($hInput, $sText)
        GUICtrlSetData($hInput2, $sText2)
        $iCurrIndex = -1
        _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
    EndIf

    EndSwitch

    ; If input has changed, refill list with matching items
    If GUICtrlRead($hInput) <> $sCurr_Input Then
        CheckInputText()
        $sCurr_Input = GUICtrlRead($hInput)
    If GUICtrlRead($hInput) = "" Then
       ; GUICtrlSetData($hList, "")
        GUICtrlSetData($hList, $sData)
        Endif
    EndIf

WEnd


Func Keywords()
Dim $asKeyWords[UBound($array)+1]
For $i = 0 to UBound($array)-1
    $asKeyWords[$i+1] = $array[$i][0] 
Next
$asKeyWords[0] = UBound($array)

Dim $asKeyWords2[UBound($array)+1]
For $i = 0 to UBound($array)-1
    $asKeyWords2[$i+1] = $array[$i][1] 
Next

If IsArray($asKeyWords) Then
    For $i = 1 to $asKeyWords[0]
    $sData &= $asKeyWords[$i] & "|"
    $sData2 &= $asKeyWords2[$i] & "|"
Next
GUICtrlSetData($hList, $sData)
GUICtrlSetData($hList2, $sData2)
    $iCurrIndex = -1
    _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
EndIf
EndFunc   ;==>Keywords


Func CheckInputText()
    $sPartialData = "|" ; Start with delimiter so new data always replaces old
    $sPartialData2 = "|" 
    Local $sInput = GUICtrlRead($hInput)
    If $sInput <> "" Then
        For $i = 1 to $asKeyWords[0]
            If StringInStr($asKeyWords[$i], $sInput) <> 0 Then 
                 $sPartialData &= $asKeyWords[$i] & "|"
                 $sPartialData2 &= $asKeyWords2[$i] & "|"
            EndIf
        Next
        GUICtrlSetData($hList, $sPartialData)
        GUICtrlSetData($hList2, $sPartialData2)
    EndIf
EndFunc   ;==>CheckInputText

Share this post


Link to post
Share on other sites

WOW!  I would never have thought of that!  That is actually quite cool.  And easily implemented.

As to code in #6 -

if you have some time in the future could you identify what areas need to be changed given changing from list box to list view s you pointed out?

I'm not asking you to code it, just identify the areas.  This is how I am learning all this.

Your original input helped me in populating the listview, but the balance of the code seems to need tweaking as well from that one change.  Its not as easy as just trade one gui for another or so I suspect.

Of course if you decline I understand as well. You already have been helpful and expanded my understanding of all this.

Thanks.

Share this post


Link to post
Share on other sites

In this particular case, there are only 2 columns so managing 2 twin listboxes is quite easy using a bit of copy-paste
But just imagine 5 or 6 columns, in this case the listview is THE solution

A listbox is a unique control and you populate it by using a string containing separators, while a listview is a control populated by listview items which are controls themselves, these items being populated separately
The approach is different so the functions to use and the managing are different
Obviously your code in post #6 can be adapted to use a listview but that means deep changes and the code to be almost completely rewritten

I strongly advise you to have a look at the funcs I mentioned in post #9 and to the many examples in the helpfile and in this forum

When you will know the basics about listviews there will be many people around here to answer your questions  :)

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Here is something to start with - but I didn't put the accelerators  :)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#Include <GuiListview.au3>


Global $array[6][2] = [["Alan", "misc1"], ["Andrew", "misc2"], ["Barry", "new1"], ["Brian", "new2"], ["Colin", "old1"], ["Cristopher", "old2"]]

Global $items[UBound($array)], $sCurr_Input = ""

$gui = GUICreate("Example", 200, 400)
$input = GUICtrlCreateInput("", 5, 5, 190, 20)
$button = GUICtrlCreateButton("Read", 20, 360, 80, 25)
$reset = GUICtrlCreateButton("Reset", 110, 360, 80, 25)
$listview = GUICtrlCreateListView("col1    |col2", 5, 30, 190, 325)
GUICtrlSendMsg($listview, $LVM_SETCOLUMNWIDTH, 0, 85)
GUICtrlSendMsg($listview, $LVM_SETCOLUMNWIDTH, 1, 85)
For $i = 0 to UBound($array)-1
    $items[$i] = GUICtrlCreateListViewItem($array[$i][0] & "|" & $array[$i][1], $listview)
Next
GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")


While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button
            $sFinal = GUICtrlRead($input)
            For $i = 0 to UBound($array)-1
                 If $sFinal = $array[$i][0] Then
                       MsgBox(0, "Chosen", $array[$i][0] & " - " & $array[$i][1])
                       Exitloop
                 EndIf
            Next
        Case $reset
           GuiCtrlSetData($input, "")
           For $i = 0 to UBound($array)-1
                GuiCtrlDelete($items[$i])
           Next
           For $i = 0 to UBound($array)-1
                $items[$i] = GUICtrlCreateListViewItem($array[$i][0] & "|" & $array[$i][1], $listview)
           Next
    EndSwitch

   If GUICtrlRead($input) <> $sCurr_Input Then
        CheckInputText()
        $sCurr_Input = GUICtrlRead($input)
    EndIf
WEnd

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

Func CheckInputText()
    Local $sInput = GUICtrlRead($input)
    If $sInput <> "" Then
        Local $n = StringLen($sInput)
        For $i = 0 to UBound($array)-1
            If StringLeft($array[$i][0], $n) <> $sInput Then
                 GuiCtrlDelete($items[$i])
            EndIf
        Next
    EndIf
EndFunc   ;==>CheckInputText


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iCode, $tNMHDR, $hWndListView, $indexLV, $itemtxt
    $hWndListView = $listview
    If Not IsHWnd($listview) Then $hWndListView = GUICtrlGetHandle($listview)
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
            Case $NM_CLICK
            $indexLV = ControlListView($gui, "", $hWndListView, "GetSelected")
            $itemtxt = ControlListView($gui, "", $hWndListView, "GetText", $indexLV, 0)
            If $indexLV <> "" Then GuiCtrlSetData($input, $itemtxt)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Edited by mikell

Share this post


Link to post
Share on other sites

You're good!  My first task is to look at the difference between yours and the one in #6, so i can learn about what changes produced what.  That along with your comments in #13  - those helped a lot.  But being a newbie it takes me longer!!!

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