Jump to content

Edit box not cleared after first click


Go to solution Solved by Nine,

Recommended Posts


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.

 

#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

New.gif

Link to comment
Share on other sites

  • Solution
Posted (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 by Nine
Link to comment
Share on other sites

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. 

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