Sign in to follow this  
Followers 0
Garp99HasSpoken

[Solved] List item won't remove or update properly in dual list

3 posts in this topic

#1 ·  Posted (edited)

I'm having a problem with two lists to update. A populated volunteer list on the right and an empty worker list on the left.

The idea is to Add a worker from volunteer list into the worker list and this should remove that selection from the volunteer list so that they cannot be selected again. So at all times, the worker + volunteer lists should equal the starting volunteer list.

And Removing a worker from the list will add them back into the volunteer list.

The code below runs but even though MsgBox is returning values, the lists are not being updated properly.

When I add a worker, the volunteer list still shows them and in fact the list is duplicated (2 copies).

When I add second worker, I get WorkerA and WorkerAWorkerB in the list, and now volunteer list is 3 copies.

When removing WorkerA, they remain in the list.

I'm fairly certain my regexp is good 'cause I tested it with RegExp.exe program.

What have I missed?

#include <ButtonConstants.au3>
#include <DateTimeConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#include <GuiMonthCal.au3>
#include <Array.au3>

Opt("GUIOnEventMode", 1)


Global $defVolunteer = "Bob Barker"

Local $workers, $volunteers
ClrEvent()

#Region ### START Koda GUI section ### Form=r:\autoit examples\koda\forms\volunteeraddevent.kxf
Local $FormAddEvent = GUICreate("Add Event", 422, 481, 302, 137)
GUISetFont(12, 400, 0, "MS Sans Serif")
GUISetBkColor(0xA6CAF0)
GUISetOnEvent($GUI_EVENT_CLOSE, "FormAddEventClose")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "")
GUISetOnEvent($GUI_EVENT_RESTORE, "")

GUICtrlCreateLabel("Volunteer(s):", 160, 200, 112, 28, 0)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
Local $VolWorked = GUICtrlCreateList("", 24, 176, 129, 246, BitOR($LBS_NOTIFY,$LBS_SORT,$WS_BORDER))
GUICtrlSetResizing(-1, $GUI_DOCKHEIGHT)
Local $ButtonAdd = GUICtrlCreateButton("<< &Add", 168, 256, 97, 33, $BS_NOTIFY)
GUICtrlSetOnEvent(-1, "ButtonAddClick")
Local $ButtonRemove = GUICtrlCreateButton("&Remove >>", 168, 312, 97, 33, $BS_NOTIFY)
GUICtrlSetOnEvent(-1, "ButtonRemoveClick")
Local $VolList = GUICtrlCreateList("", 272, 176, 129, 246, BitOR($LBS_NOTIFY,$LBS_SORT,$WS_BORDER))
GUICtrlSetData(-1, $volunteers, $defVolunteer)
GUICtrlSetResizing(-1, $GUI_DOCKHEIGHT)
Local $ButtonEventDone = GUICtrlCreateButton("&Done", 90, 432, 97, 40, $BS_NOTIFY)
GUICtrlSetOnEvent(-1, "ButtonEventDoneClick")
Local $ButtonEventCancel = GUICtrlCreateButton("&Cancel", 235, 432, 97, 40, $BS_NOTIFY)
GUICtrlSetOnEvent(-1, "ButtonEventCancelClick")
GUISetState(@SW_HIDE)
#EndRegion ### END Koda GUI section ###


Func ClrEvent()
$workers = ""
$volunteers = "Bob Barker|Al Muh|Noele Muh|Carol Tartin|Don Floods|Lora Floods"
EndFunc

Func FormAddEventClose()
GUISetState(@SW_HIDE)
Exit
EndFunc

Func ButtonEventCancelClick()
if (MsgBox(36, "Are You Sure?", "Cancel all changes and exit?") = 6) Then FormAddEventClose()
EndFunc

Func ButtonEventDoneClick()
If (StringLen($workers) = 0) Then
MsgBox(16, "Missing Workers", "A volunteer must be added to the list on the left." & @CRLF & "Select from Volunteer list on right and use the Add button.", 10)
Else
MsgBox(0, "Results", "Workers: " & $workers & @CRLF & @CRLF)
EndIf
EndFunc

Func ButtonAddClick()
Local $picked = GUICtrlRead($VolList)
; MsgBox(0, "Adding Before", "Worker picked: " & $picked & @CRLF & @CRLF & "Volunteer list: " & $volunteers & @CRLF & @CRLF, 10)

$workers &= $picked
StringRegExpReplace($volunteers, "(\|" & $picked & "$)", "") ; remove last item matched
StringRegExpReplace($volunteers, "(" & $picked & "\|)", "") ; remove anywhere else
GUICtrlSetData($VolList, $volunteers)
GUICtrlSetData($VolWorked, $workers)
MsgBox(0, "Adding Worker", "Worker picked: " & $picked & @CRLF & @CRLF & "Volunteer list: " & $volunteers & @CRLF & @CRLF, 10)
EndFunc

Func ButtonRemoveClick()
Local $picked = GUICtrlRead($VolWorked)
MsgBox(0, "Removing Before", "Volunteer removed: " & $picked & @CRLF & @CRLF & "Worker list: " & $workers & @CRLF & @CRLF, 10)

$volunteers &= $picked
StringRegExpReplace($workers, "(\|" & $picked & "$)", "")
StringRegExpReplace($workers, "(" & $picked & "\|)", "")
GUICtrlSetData($VolList, $volunteers)
GUICtrlSetData($VolWorked, $workers)
MsgBox(0, "Removing Worker", "Volunteer removed: " & $picked & @CRLF & @CRLF & "Worker list: " & $workers & @CRLF & @CRLF, 10)
EndFunc


GUISetState(@SW_SHOW)

Local $msg

While 1
$msg=GUIGetMsg()

If (($msg = $GUI_EVENT_CLOSE) Or ($msg = $ButtonEventDone) Or ($msg = $ButtonEventCancel)) Then ExitLoop
WEnd

GUISetState(@SW_HIDE)
GUIDelete()
Exit
Edited by Garp99HasSpoken

Share this post


Link to post
Share on other sites



Garp99HasSpoken,

You need to assign the result of the StringRegExpReplace operation to a variable if you want to see the result - you were not doing this. And you do not need a regex for something like this - a simple StringReplace will do. ;)

Taker a look at this modified version of your script:

#include <ButtonConstants.au3>
#include <DateTimeConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#include <GuiMonthCal.au3>
#include <Array.au3>

Opt("GUIOnEventMode", 1)


Global $defVolunteer = "Bob Barker"

Local $workers, $volunteers
ClrEvent()

#Region ### START Koda GUI section ### Form=r:\autoit examples\koda\forms\volunteeraddevent.kxf
    Local $FormAddEvent = GUICreate("Add Event", 422, 481, 302, 137)
    GUISetFont(12, 400, 0, "MS Sans Serif")
    GUISetBkColor(0xA6CAF0)
    GUISetOnEvent($GUI_EVENT_CLOSE, "FormAddEventClose")
    GUISetOnEvent($GUI_EVENT_MINIMIZE, "")
    GUISetOnEvent($GUI_EVENT_MAXIMIZE, "")
    GUISetOnEvent($GUI_EVENT_RESTORE, "")

    GUICtrlCreateLabel("Volunteer(s):", 160, 200, 112, 28, 0)
    GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
    Local $VolWorked = GUICtrlCreateList("", 24, 176, 129, 246, BitOR($LBS_NOTIFY, $LBS_SORT, $WS_BORDER))
    GUICtrlSetResizing(-1, $GUI_DOCKHEIGHT)
    Local $ButtonAdd = GUICtrlCreateButton("<< &Add", 168, 256, 97, 33, $BS_NOTIFY)
    GUICtrlSetOnEvent(-1, "ButtonAddClick")
    Local $ButtonRemove = GUICtrlCreateButton("&Remove >>", 168, 312, 97, 33, $BS_NOTIFY)
    GUICtrlSetOnEvent(-1, "ButtonRemoveClick")
    Local $VolList = GUICtrlCreateList("", 272, 176, 129, 246, BitOR($LBS_NOTIFY, $LBS_SORT, $WS_BORDER))
    GUICtrlSetData(-1, $volunteers, $defVolunteer)
    GUICtrlSetResizing(-1, $GUI_DOCKHEIGHT)
    Local $ButtonEventDone = GUICtrlCreateButton("&Done", 90, 432, 97, 40, $BS_NOTIFY)
    GUICtrlSetOnEvent(-1, "ButtonEventDoneClick")
    Local $ButtonEventCancel = GUICtrlCreateButton("&Cancel", 235, 432, 97, 40, $BS_NOTIFY)
    GUICtrlSetOnEvent(-1, "ButtonEventCancelClick")
    GUISetState(@SW_HIDE)
#EndRegion ### END Koda GUI section ###


Func ClrEvent()
    $workers = ""
    $volunteers = "|Bob Barker|Al Muh|Noele Muh|Carol Tartin|Don Floods|Lora Floods"
EndFunc   ;==>ClrEvent

Func FormAddEventClose()
    GUISetState(@SW_HIDE)
    Exit
EndFunc   ;==>FormAddEventClose

Func ButtonEventCancelClick()
    If (MsgBox(36, "Are You Sure?", "Cancel all changes and exit?") = 6) Then FormAddEventClose()
EndFunc   ;==>ButtonEventCancelClick

Func ButtonEventDoneClick()
    If (StringLen($workers) = 0) Then
        MsgBox(16, "Missing Workers", "A volunteer must be added to the list on the left." & @CRLF & "Select from Volunteer list on right and use the Add button.", 10)
    Else
        MsgBox(0, "Results", "Workers: " & $workers & @CRLF & @CRLF)
    EndIf
EndFunc   ;==>ButtonEventDoneClick

Func ButtonAddClick()
    Local $picked = GUICtrlRead($VolList)
    MsgBox(0, "Adding Before", "Worker picked: " & $picked & @CRLF & @CRLF & "Volunteer list: " & $volunteers & @CRLF & @CRLF, 10)
    ; Add the name to the relevant list
    $workers &= "|" & $picked
    ; Remove it from the other
    $volunteers = StringReplace($volunteers, "|" & $picked, "")
    ; And reload the lists
    GUICtrlSetData($VolList, $volunteers)
    GUICtrlSetData($VolWorked, $workers)
    MsgBox(0, "Adding Worker", "Worker picked: " & $picked & @CRLF & @CRLF & "Volunteer list: " & $volunteers & @CRLF & @CRLF, 10)
EndFunc   ;==>ButtonAddClick

Func ButtonRemoveClick()
    Local $picked = GUICtrlRead($VolWorked)
    MsgBox(0, "Removing Before", "Volunteer removed: " & $picked & @CRLF & @CRLF & "Worker list: " & $workers & @CRLF & @CRLF, 10)
    $volunteers &= "|" & $picked
    $workers = StringReplace($workers, "|" & $picked, "")
    GUICtrlSetData($VolList, $volunteers)
    GUICtrlSetData($VolWorked, $workers)
    MsgBox(0, "Removing Worker", "Volunteer removed: " & $picked & @CRLF & @CRLF & "Worker list: " & $workers & @CRLF & @CRLF, 10)
EndFunc   ;==>ButtonRemoveClick


GUISetState(@SW_SHOW)

Local $msg

While 1
    $msg = GUIGetMsg()

    If (($msg = $GUI_EVENT_CLOSE) Or ($msg = $ButtonEventDone) Or ($msg = $ButtonEventCancel)) Then ExitLoop
WEnd

GUISetState(@SW_HIDE)
GUIDelete()
Exit

Please ask if you have any questions. :)

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

Thanks a lot, it works as expected.

At least I wasn't too far off... can't believe I forgot to pre-append the "|" when adding an item to the list.

The big thing I didn't understand was how adding "|" to the beginning of the list is ok and doesn't cause an empty line.

But now I know.

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