Jump to content

How do I allow for repeats in a GUI list?


ShikiPiki
 Share

Go to solution Solved by BrewManNH,

Recommended Posts

#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GUIListBox.au3>
#include <EditConstants.au3>

Test()
Func Test()
   Global $input, $list, $myGUI
   
   $myGUI = GUICreate("Test", 200, 200)
   GUICtrlCreateLabel("Number of items", 5, 5, 100, 20, $SS_CENTER)
   $itemNums = GUICtrlCreateInput("", 5, 25, 100, 20, $ES_NUMBER )
   $addNumber = GUICtrlCreateButton("Add &Number", 110, 5, 75, 40)
   $numberList = GUICtrlCreateList("", 5, 50, 190, 145, BitOR($WS_BORDER, $WS_VSCROLL, $WS_HSCROLL, _
      $LBS_EXTENDEDSEL))
   GUISetState()
   While 1
      $msg = GUIGetMsg()
      Select
         Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
         Case $msg = $addNumber
            $addItemNum = GUICtrlRead($itemNums)
            GUICtrlSetData($numberList, $addItemNum)
            GUICtrlSetData($itemNums, "")
      EndSelect
   WEnd
   GUIDelete()
EndFunc
When I first add numbers such as 1, 5, 10 they all add fine. But say my fourth number is 10, it does not add it to the end of the list. I cannot seem to find anything in the list box styles either that allows for repeats. Is there something special I have to do to allow for repeats?

Edited by ShikiPiki

Thanks! -ShikiPiki

Link to comment
Share on other sites

You can add duplicates, note the pipe I added: $addItemNum & "|"

EDIT: If $addItemNum <> '' Then GUICtrlSetData($numberList, $addItemNum & "|")

Adding a check to see if $addItemNum is blank so you do not clear the list if you click the add button while the input is empty

#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GUIListBox.au3>
#include <EditConstants.au3>

Test()
Func Test()
   Global $input, $list, $myGUI

   $myGUI = GUICreate("Test", 200, 200)
   GUICtrlCreateLabel("Number of items", 5, 5, 100, 20, $SS_CENTER)
   $itemNums = GUICtrlCreateInput("", 5, 25, 100, 20, $ES_NUMBER )
   $addNumber = GUICtrlCreateButton("Add &Number", 110, 5, 75, 40)
   $numberList = GUICtrlCreateList("", 5, 50, 190, 145, BitOR($WS_BORDER, $WS_VSCROLL, $WS_HSCROLL, _
      $LBS_EXTENDEDSEL))
   GUISetState()
   While 1
      $msg = GUIGetMsg()
      Select
         Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
         Case $msg = $addNumber
            $addItemNum = GUICtrlRead($itemNums)
            If $addItemNum <> '' Then GUICtrlSetData($numberList, $addItemNum & "|")
            GUICtrlSetData($itemNums, "")
      EndSelect
   WEnd
   GUIDelete()
EndFunc
Edited by danwilli
Link to comment
Share on other sites

  • Solution

Try this change to your script.

#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GUIListBox.au3>
#include <EditConstants.au3>
#include <GUIConstantsEX.au3>

Test()
Func Test()
   Local $input, $list, $myGUI, $addItemNum = "|"

   $myGUI = GUICreate("Test", 200, 200)
   GUICtrlCreateLabel("Number of items", 5, 5, 100, 20, $SS_CENTER)
   $itemNums = GUICtrlCreateInput("", 5, 25, 100, 20, $ES_NUMBER )
   $addNumber = GUICtrlCreateButton("Add &Number", 110, 5, 75, 40)
   $numberList = GUICtrlCreateList("", 5, 50, 190, 145, BitOR($WS_BORDER, $WS_VSCROLL, $WS_HSCROLL, _
      $LBS_EXTENDEDSEL))
   GUISetState()
   While 1
      $msg = GUIGetMsg()
      Select
         Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
         Case $msg = $addNumber
            $addItemNum &= GUICtrlRead($itemNums) & "|"
            GUICtrlSetData($numberList, $addItemNum)
            GUICtrlSetData($itemNums, "")
      EndSelect
   WEnd
   GUIDelete()
EndFunc

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I solved it with a ListView:

#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GUIListBox.au3>
#include <EditConstants.au3>

Test()
Func Test()
   Global $input, $list, $myGUI

   $myGUI = GUICreate("Test", 200, 200)
   GUICtrlCreateLabel("Number of items", 5, 5, 100, 20, $SS_CENTER)
   $itemNums = GUICtrlCreateInput("", 5, 25, 100, 20, $ES_NUMBER )
   $addNumber = GUICtrlCreateButton("Add &Number", 110, 5, 75, 40)
   $numberList = GUICtrlCreateListView(" ", 5, 50, 190, 145)
   GUISetState(@SW_SHOW)
   While 1
      $msg = GUIGetMsg()
      Select
         Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
         Case $msg = $addNumber
            $addItemNum = GUICtrlRead($itemNums)
            GUICtrlCreateListViewItem($addItemNum, $numberList)
            GUICtrlSetData($itemNums, "")
            GUICtrlSetState($itemNums, $GUI_FOCUS)
      EndSelect
   WEnd
   GUIDelete()
EndFunc

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

BrewManNH, Brilliant! Is there a reason to why that has to be included? or is it just cause?

From the help file for GUCtrlSetData:

 

For Combo or List control :

If the "data" corresponds to an already existing entry it is set as the default.

If the "data" starts with GUIDataSeparatorChar or is an empty string "" the previous list is destroyed.

 

The way that the list is being built with the code I posted is like this.

First the data to be put into the Listbox is created using a string of entries separated by the pipe symbol, which is the GUIDataSeparator character mentioned above. This puts the items on a line by themself.

Plus, the first character in that string is the pipe, which erases the previous list contents, so it doesn't duplicate the list every time you add another item to it.

Then you set the listbox contents using this list, which allows you to duplicate items which wouldn't be possible if you enter them one at a time.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

also commonly could use Listview.

#include <ListviewConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <GuiListView.au3>
#include <EditConstants.au3>

Test()
Func Test()
    Local $myGUI = GUICreate("Test", 200, 200)
    Local $itemNums = GUICtrlCreateInput("", 5, 25, 100, 20, $ES_NUMBER)
    Local $addNumber = GUICtrlCreateButton("Add &Number", 110, 5, 75, 40)
    Local $numberList = GUICtrlCreateListView("", 5, 50, 190, 145, $LVS_NOCOLUMNHEADER)
    _GUICtrlListView_AddColumn($numberList, "", 145)
    GUICtrlCreateLabel("Number of items", 5, 5, 100, 20, $SS_CENTER)

    GUISetState()
    While 1
        $msg = GUIGetMsg()
        Select
             Case $msg = $GUI_EVENT_CLOSE
             ExitLoop
            Case $msg = $addNumber
                $addItemNum = GUICtrlRead($itemNums)
                GUICtrlCreateListViewItem($addItemNum, $numberList)
                GUICtrlSetData($itemNums, "")
        EndSelect
    WEnd
    GUIDelete()
EndFunc   ;==>Test

Edited: :oops:  Sorry water, replied without reading your post 

Edited by Belini
Link to comment
Share on other sites

Thank you everyone for your fast response. As a standalone all the scripts provided did indeed work as I had wanted. The test script I ran to check was the following:

 

#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GUIListBox.au3>
#include <EditConstants.au3>
#include <GUIConstantsEX.au3>

Test()
Func Test()
   Global $input, $list, $myGUI, $addItemNum = "|", $itemNums, $numberList

   $myGUI = GUICreate("Test", 200, 200)
   GUICtrlCreateLabel("Number of items", 5, 5, 100, 20, $SS_CENTER)
   $itemNums = GUICtrlCreateInput("", 5, 25, 100, 20, $ES_NUMBER )
   $addNumber = GUICtrlCreateButton("Add &Number", 110, 5, 75, 40)
   $numberList = GUICtrlCreateList("", 5, 50, 190, 145, BitOR($WS_BORDER, $WS_VSCROLL, $WS_HSCROLL, _
      $LBS_EXTENDEDSEL))
   GUISetState()
   While 1
      $msg = GUIGetMsg()
      Select
         Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
         Case $msg = $addNumber
            Call("add")
      EndSelect
   WEnd
   GUIDelete()
EndFunc

Func add()
   $addItemNum &= GUICtrlRead($itemNums) & "|"
   GUICtrlSetData($numberList, $addItemNum)
   GUICtrlSetData($itemNums, "")
EndFunc
If I added the following numbers in the following order, 10, 10, 5 ,3, 10 it would also show up in that order in the list from top to bottom.

However when I implemented the same method in my full GUI the numbers would group themselves up with eachother, listing it from top to bottom would be 10, 10, 10, 5, 3 instead of 10, 10, 5, 3, 10. I cannot figure out why there is a discrepancy between the two. Any help is much appreciated.

Here is the full GUI:

#include <GUIConstantsEx.au3>
#include <file.au3>
#include <array.au3>
#include <Excel.au3>
#include <WindowsConstants.au3>
#include <TreeViewConstants.au3>
#include <StaticConstants.au3>
#include <GuiTreeView.au3>
#include <GuiConstants.au3>
#include <GUIListBox.au3>
#include <GuiListView.au3>
#include <EditConstants.au3>

HotKeySet("{PAUSE}", "EndScript")

Local $sFilePath1 = @ScriptDir & "\test2.csv"
If Not FileExists($sFilePath1) Then
   MsgBox(16, '', $sFilePath1 & 'DOES NOT exists')
   Exit
EndIf
 
Opt("MustDeclareVars", 1)

Global Const $CSVFILE = $sFilePath1
Global Const $DELIM = "," ;the delimiter in the CSV file
Global  $arrContent, $arrLine, $res = 0,  $itemHandle, $iIndex

$res = _FileReadToArray($CSVFILE, $arrContent)
 
Setup()
Func Setup()
   Global $treeView, $Type1, $Type2 ;SECTION NAMES!
   Global $cancel, $addItem, $removeItem, $calculate, $addNumber, $removeNumber ;BUTTONS!
   Global $msg, $branchSelected, $branch, $itemSelected, $addItemNum = "|" ;SELECTIONS!
   Global $itemList, $force, $property, $numberList, $itemNums ;VALUES!
   global $itemData[$arrContent[0]][5], $test[$arrContent[0]], $numOFNums ;DATA ARRAYS!
   global $itemIndex[$arrContent[0]], $iIndex[$arrContent[0]] ;ITEM NAME ARRAYS!
   global $craftGUI ;GUI!
   global $aItems, $sItems ;INDICIES!
   $iIndex[0]=1
   $craftGUI = GUICreate("Set-up", 700, 300)
   GUICtrlCreateLabel("Items of Interest", 310, 5, 100, 20, $SS_CENTER)
   $itemList = GUICtrlCreateList("", 310, 25, 100, 150, BitOR($GUI_SS_DEFAULT_LIST, $LBS_EXTENDEDSEL))
   GUICtrlCreateLabel("Number to repeats", 425, 5, 100, 20, $SS_CENTER)
   $numberList = GUICtrlCreateList("", 425, 25, 100, 150, BitOR($WS_BORDER, $WS_VSCROLL, $LBS_NOTIFY, $GUI_SS_DEFAULT_LIST, $LBS_EXTENDEDSEL))
   GUICtrlCreateLabel("Number of items", 545, 5, 150, 20, $SS_CENTER)
   $itemNums = GUICtrlCreateInput("", 545, 25, 150, 20, $ES_NUMBER )
   GUICtrlCreateLabel("Force", 545, 75, 150, 20, $SS_CENTER)
   $force = GUICtrlCreateInput("", 545, 100, 150, 20, $ES_NUMBER )
   GUICtrlCreateLabel("Cycles", 545, 125, 150, 20, $SS_CENTER)
   $property = GUICtrlCreateInput("", 545, 150, 150, 20, $ES_NUMBER )
   
   $treeView = GUICtrlCreateTreeView( 5, 5, 300, 245, BitOr($TVS_HASBUTTONS, $TVS_HASLINES, _
      $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
   Call("PopulateTree")
;~  ConsoleWrite($itemData[9][1]) 
;~  _ArrayDisplay($itemData) ; Check data

   $cancel = GUICtrlCreateButton("&Cancel", 625, 280, 70, 20)
   $addNumber = GUICtrlCreateButton("Add Re&peat", 545, 50, 75, 20)
   $removeNumber = GUICtrlCreateButton("&Remove", 620, 50, 75, 20)
   $addItem = GUICtrlCreateButton("Add &Item", 5, 255, 100, 40)
   GUICtrlSetFont(-1, 15, 800)
   $removeItem = GUICtrlCreateButton("&Remove Item", 110, 275, 100, 20)
   $calculate = GUICtrlCreateButton("Ca&lculate", 310, 180, 385, 90)
   GUICtrlSetFont(-1, 30, 600)
   
   GUISetState()
   While 1
      $msg = GUIGetMsg()
      Select 
         Case $msg = $cancel Or $msg = $GUI_EVENT_CLOSE
            ExitLoop
         Case $msg = $addItem
            Call("AddingItems")
         Case $msg = $removeItem
            Call("RemovingItems")
         Case $msg = $addNumber
            Call("AddingNumber")
         Case $msg = $removeNumber
            Call("RemovingNumber")
         Case $msg = $calculate
            ;Check data
;~          For $jj = 1 To $iIndex[0]-1
;~             ConsoleWrite($iIndex[$jj] & @LF)
;~             ConsoleWrite($itemData[$iIndex[$jj]][0]  & " " & $itemData[$iIndex[$jj]][1] & @LF)
;~             ConsoleWrite($itemData[9][0] & $itemData[9][1])
;~          Next
            Call("Optimizer")
      EndSelect
   WEnd
   GUIDelete()
EndFunc

Func PopulateTree()
   $Type1 = GUICtrlCreateTreeViewItem("Type 1", $treeView)
   GUICtrlSetColor(-1, 0x0000C0)
   $Type2 = GUICtrlCreateTreeViewItem("Type 2", $treeView)
   GUICtrlSetColor(-1, 0x0000C0)
  If $res = 1 Then
    For $ii = 1 To $arrContent[0]
        $arrLine = StringSplit($arrContent[$ii], $DELIM)
         If $arrLine[1] = 1 Then
            $itemIndex[$ii-1] = GUICtrlCreateTreeViewItem("Product " & $arrLine[1] & ". " & $arrLine[2], $Type1)
            For $jj = 1 To 5
               $itemData[$ii-1][$jj-1]=$arrLine[$jj]
            Next
         Elseif $arrLine[1] = 2 Then
            $itemIndex[$ii-1] = GUICtrlCreateTreeViewItem("Product " & $arrLine[1] & ". " & $arrLine[2], $Type2)
            For $jj = 1 To 5
               $itemData[$ii-1][$jj-1]=$arrLine[$jj]
            Next
         EndIf
      Next 
   Else
       MsgBox(48, "", "Error opening file! "& $sFilePath1 )
    EndIf 
EndFunc

Func GUIChangeItems($hidestart, $hideend, $showstart, $showend)
    Local $idx
    For $idx = $hidestart To $hideend
        GUICtrlSetState($idx, $GUI_HIDE)
    Next
    For $idx = $showstart To $showend
        GUICtrlSetState($idx, $GUI_SHOW)
    Next
EndFunc

Func AddingItems()
   $branchSelected = GUICtrlRead($treeView)
   If $branchSelected = 0 Then
      MsgBox(64, "Error 01", "Error 01: No items currently selected in tree!")
   Else
      $branch = GUICtrlRead($branchSelected, 1)
      GUICtrlSetData($itemList, $branch)
      $itemHandle = _GUICtrlTreeView_GetSelection($treeView)
      For $jj = 1 To $arrContent[0]
         If $itemHandle = _GUICtrlTreeView_GetItemHandle($treeView, $itemIndex[$jj-1]) Then
            $iIndex[$iIndex[0]] = $jj-1
            $iIndex[0] =($iIndex[0]+1)
            ExitLoop
         EndIf
      Next
   EndIf
EndFunc

Func RemovingItems()
   $aItems = _GUICtrlListBox_GetSelItems($itemList)
   if $aItems[0] = 0 Then
      MsgBox(64, "Error 02", "Error 02: No items currently selected in list!")
   Else
       For $iI = 1 To $aItems[0]
           If $iI > 1 Then $sItems &= ", "
           $sItems &= $aItems[$iI]
           $itemSelected = $aItems[1]+1
        Next
     EndIf
;~             _ArrayDisplay($iIndex)
   _GUICtrlListBox_DeleteString($itemList, _GUICtrlListBox_GetCaretIndex($itemList))
;~             ConsoleWrite($itemSelected & @LF)
   _ArrayDelete($iIndex, $itemSelected)
   $iIndex[0] = ($iIndex[0]-1)
;~             _ArrayDisplay($iIndex)   
EndFunc

Func AddingNumber()
   $addItemNum &= GUICtrlRead($itemNums) & "|"
   ConsoleWrite($addItemNum & @LF)
   GUICtrlSetData($numberList, $addItemNum)
   GUICtrlSetData($itemNums, "")
   $numOfNums = $addItemNum
   ConsoleWrite($numOfNums & @LF)
   $numOfNums = StringSplit($addItemNum, "|")
   _ArrayDelete($numOfNums, 1)
   _ArrayDelete($numOfNums, $numOfNums[0]-1)
   _ArrayDisplay($numOfNums)
EndFunc

Func RemovingNumber()
EndFunc

Func Optimizer()
   ReDim $iIndex[$iIndex[0]]
EndFunc

Func EndScript()
   Exit
EndFunc
Here is a few example data points:

1,05341,40,20,300

1,65816,60,20,450

2,80516,80,90,700

2,92682,80,90,965

P.S. I am terribly sorry if my code is messy :/

Edited by ShikiPiki

Thanks! -ShikiPiki

Link to comment
Share on other sites

  • Moderators

ShikiPiki,

The default style $GUI_SS_DEFAULT_LIST contains $LBS_SORT - as is explained in the Help file. Change the line in your script to read like this:

$numberList = GUICtrlCreateList("", 425, 25, 100, 150, BitOR($WS_BORDER, $WS_VSCROLL, $LBS_NOTIFY, $LBS_EXTENDEDSEL))
and you should lose the automatic sort. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

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
 Share

  • Recently Browsing   0 members

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