littlebigman Posted January 3, 2024 Posted January 3, 2024 Hello, I notice something odd in the following code where the editbox is filled by whatever items are selected in the listbox: The first time the button is clicked, the editbox is cleared and then filled as expected… but not from the second time on, where items are simply appended to the editbox. As shown, it makes no difference if I clear it with either GUICtrlSetData($Edit1, "") or _GUICtrlEdit_SetText($Edit1, ""), and the problem doesn't occur when setting lines directly with GUICtrlSetData() or _GUICtrlEdit_AppendText(). What am I doing wrong? Thank you. expandcollapse popup#include <array.au3> #include <Constants.au3> #include <WindowsConstants.au3> #include <MsgBoxConstants.au3> #include <GuiConstants.au3> #include <GUIConstantsEx.au3> #include <EditConstants.au3> #include <GuiEdit.au3> #include <ButtonConstants.au3> #include <GUIListBox.au3> #include <ListBoxConstants.au3> #include <GuiButton.au3> Global $sTest = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent elementum dolor" #Region ### START Koda GUI section ### $Form1 = GUICreate("Form1", 615, 437, 270, 124) $Edit1 = GUICtrlCreateEdit("", 16, 16, 585, 137, BitOR($ES_MULTILINE, $ES_READONLY, $WS_VSCROLL, $ES_AUTOVSCROLL)) $List1 = GUICtrlCreateList("", 16, 160, 585, 214,BitOR($LBS_STANDARD, $LBS_EXTENDEDSEL)) $BUILD = GUICtrlCreateButton("Build", 224, 400, 75, 25) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### ;set editbox to default text GUICtrlSetData($Edit1, "Started") ;fill listbox For $i = 1 To 10 _GUICtrlListBox_InsertString ( $List1,StringFormat("%02u %s",$i,$sTest)) Next ;when user clicks on button, copy selected items from listbox into editbox While True $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $BUILD ;BUG why isn't cleared after the first click? ;GUICtrlSetData($Edit1, "") ;NO DIFF _GUICtrlEdit_SetText($Edit1, "") ; Get selected item(s) Local $aItems = _GUICtrlListBox_GetSelItemsText($List1) Local $sItems For $iI = 1 To $aItems[0] $sItems &= @CRLF & $aItems[$iI] Next ;paste to editbox GUICtrlSetData($Edit1, $sItems & @CRLF) ;BAD _GUICtrlEdit_AppendText($Edit1, $sItems & @CRLF) ;this works ;GUICtrlSetData($Edit1, $sTest & @CRLF) ;_GUICtrlEdit_AppendText($Edit1, $sTest & @CRLF) EndSwitch WEnd
Solution Nine Posted January 3, 2024 Solution Posted January 3, 2024 (edited) Because $sItems is not reset to "" ps. You should not declare your variables inside loop (or any other block). It doesn't cause bugs, but you may misinterpret what it is doing (and it is not amongst best practices). Edited January 3, 2024 by Nine littlebigman 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
ioa747 Posted January 3, 2024 Posted January 3, 2024 (edited) For $iI = 1 To $aItems[0] $sItems &= $aItems[$iI] & @CRLF Next Edited January 3, 2024 by ioa747 Correction littlebigman 1 I know that I know nothing
littlebigman Posted January 3, 2024 Author Posted January 3, 2024 Thanks. I assumed the variable was re-created with every click. Should I declare + define them outside the While/Wend loop? Local $aItems = Null, $sItems = Null While True … $sItems = Null For $iI = 1 To $aItems[0] $sItems &= $aItems[$iI] & @CRLF Next … WEnd
Nine Posted January 3, 2024 Posted January 3, 2024 1 minute ago, littlebigman said: I assumed the variable was re-created with every click Yes I figured that was you were thinking. And the code you showed is exactly what I meant. littlebigman 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
ioa747 Posted January 3, 2024 Posted January 3, 2024 For $iI = 1 To $aItems[0] $sItems &= $aItems[$iI] & @CRLF Next instead For $iI = 1 To $aItems[0] $sItems &= @CRLF & $aItems[$iI] Next I know that I know nothing
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