Jump to content

Previous data is removed after using GUICtrlSetData


kitoy
 Share

Recommended Posts

I wanted to make a countdown timer without queueing the countdown message to the next line but the previous data is removed after using GUICtrlSetData. Any thoughts on how to display the previous data/message?

Here is an example:

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $_main = GUICreate("", 501, 313, -1, -1)
Global $g_idMemo = GUICtrlCreateEdit("", 2, 2, 496, 274, $ES_AUTOVSCROLL + $ES_READONLY + $WS_VSCROLL)
GUICtrlSetData(-1, "")
GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New")
GUICtrlSendMsg($g_idMemo, $EM_SETREADONLY, True, 0)
GUICtrlSetBkColor($g_idMemo, 0xFFFFFF)
GUICtrlSetCursor($g_idMemo, -1)
GUISetState(@SW_SHOW)

HotKeySet('{esc}', "_close")
Func _close()
    Exit
EndFunc   ;==>_close

Func MemoWrite($sMessage = "")
    GUICtrlSetData($g_idMemo, $sMessage & @CRLF, 1)
EndFunc   ;==>MemoWrite

MemoWrite("test message 1")
Sleep(1000)
MemoWrite("test message 2")
Sleep(1000)
MemoWrite("test message 3")
Sleep(1000)

For $i = 5 to 0 Step -1
    GUICtrlSetData($g_idMemo, "Program will exit in "&$i&" seconds...")
    Sleep(1000)
Next

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

 

Link to comment
Share on other sites

  • Moderators

kitoy,

Use _GUICtrlEdit_AppendText - that way you add to the existing content rather then replacing it. Or else read the existing content, concatenate the required new text and then replace the old with the new. I would recommend the former.

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

Following Melba23's second solution in post #2, here is a modified example of post #1.
Note the convention of having all the user defined functions (UDFs) at the end of the script.

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet('{esc}', "_close")

Global $_main = GUICreate("", 501, 313, -1, -1)
Global $g_idMemo = GUICtrlCreateEdit("", 2, 2, 496, 274, $ES_AUTOVSCROLL + $ES_READONLY + $WS_VSCROLL)
;GUICtrlSetData(-1, "")
GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New")
;GUICtrlSendMsg($g_idMemo, $EM_SETREADONLY, True, 0)
GUICtrlSetBkColor($g_idMemo, 0xFFFFE0)
GUICtrlSetCursor($g_idMemo, -1)
GUISetState(@SW_SHOW)

MemoWrite("test message 1")
Sleep(1000)
MemoWrite("test message 2")
Sleep(1000)
MemoWrite("test message 3")
Sleep(1000)

;Local $sEditText = ControlGetText($_main, "", $g_idMemo)
;Or
Local $sEditText = GUICtrlRead($g_idMemo)
For $i = 5 To 0 Step -1
    GUICtrlSetData($g_idMemo, $sEditText & "Program will exit in " & $i & " seconds...")
    Sleep(1000)
Next

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


Func MemoWrite($sMessage = "")
    GUICtrlSetData($g_idMemo, $sMessage & @CRLF, 1)
EndFunc   ;==>MemoWrite

Func _close()
    Exit
EndFunc   ;==>_close

 

Link to comment
Share on other sites

Well, that answers my problem. Thanks everyone for the help.

@mikell, @Malkey - both are the same but Malkey have another option.

;Local $sEditText = ControlGetText($_main, "", $g_idMemo)

I believe this is now closed?
Edit:  Found a new error.

Edited by kitoy
Link to comment
Share on other sites

Found some error when the lines are already to long. Once the countdown starts, it will go back to the very first line.

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet('{esc}', "_close")

Global $_main = GUICreate("", 501, 313, -1, -1)
Global $g_idMemo = GUICtrlCreateEdit("", 2, 2, 496, 274, $ES_AUTOVSCROLL + $ES_READONLY + $WS_VSCROLL)
;GUICtrlSetData(-1, "")
GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New")
;GUICtrlSendMsg($g_idMemo, $EM_SETREADONLY, True, 0)
GUICtrlSetBkColor($g_idMemo, 0xFFFFE0)
GUICtrlSetCursor($g_idMemo, -1)
GUISetState(@SW_SHOW)

For $i = 1 to 20
    MemoWrite("test message " & $i)
    Sleep(100)
Next

;~ Local $sEditText = ControlGetText($_main, "", $g_idMemo)
;~ Or
Local $sEditText = GUICtrlRead($g_idMemo)
For $i = 10 To 0 Step -1
    GUICtrlSetData($g_idMemo, $sEditText & "Program will exit in " & $i & " seconds..." &@CRLF)
    Sleep(1000)
Next

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


Func MemoWrite($sMessage = "")
    GUICtrlSetData($g_idMemo, $sMessage & @CRLF, 1)
EndFunc   ;==>MemoWrite

Func _close()
    Exit
EndFunc   ;==>_close

 

Link to comment
Share on other sites

I  (personally)  would use a listview as an alternative - and easier - way 

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

HotKeySet('{esc}', "_close")

Global $_main = GUICreate("", 501, 313, -1, -1)
;Global $g_idMemo = GUICtrlCreateEdit("", 2, 2, 496, 274, $ES_AUTOVSCROLL + $ES_READONLY + $WS_VSCROLL)

Global $g_idMemo = GUICtrlCreateListView(" ", 2, 2, 496, 274, $LVS_NOCOLUMNHEADER)
_GUICtrlListView_SetColumnWidth ($g_idMemo, 0, 470)

;GUICtrlSetData(-1, "")
GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New")
;GUICtrlSendMsg($g_idMemo, $EM_SETREADONLY, True, 0)
GUICtrlSetBkColor($g_idMemo, 0xFFFFE0)
GUICtrlSetCursor($g_idMemo, -1)
GUISetState(@SW_SHOW)

For $i = 1 to 20
    $last_index = MemoWrite("test message " & $i)
    Sleep(100)
Next

;~ Local $sEditText = ControlGetText($_main, "", $g_idMemo)
;~ Or
; Local $sEditText = GUICtrlRead($g_idMemo)

For $i = 10 To 0 Step -1
   ; GUICtrlSetData($g_idMemo, $sEditText & "Program will exit in " & $i & " seconds..." &@CRLF)
    _GUICtrlListView_DeleteItem($g_idMemo, $last_index)
    $last_index = MemoWrite("Program will exit in " & $i & " seconds...")
    Sleep(1000)
Next

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


Func MemoWrite($sMessage = "")
   ; GUICtrlSetData($g_idMemo, $sMessage & @CRLF, 1)
   $index = _GUICtrlListView_AddItem($g_idMemo, $sMessage )
   _GUICtrlListView_EnsureVisible($g_idMemo, $index)
  Return $index
EndFunc   ;==>MemoWrite

Func _close()
    Exit
EndFunc   ;==>_close

 

Link to comment
Share on other sites

@mikell, thank you very much. This is really great. 

By the way, is it possible to insert the $last_index to the next line if the string exceeds to the column width. I don't want to use the horizontal scroll. I've been looking in the _GUICrtlListView functions or in the parameters but I couldn't find one.

For $i = 1 to 20
    $last_index = MemoWrite($i & ". The quick brown fox jumps over the red lazy dog. Exceeding the text out of the column width.")
    Sleep(100)
Next

 

Link to comment
Share on other sites

As multiline listviewitems are impossible, it needs a workaround   :)
In the code below, the message is splitted in lines of 50 characters maxi - please look at the comments

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

HotKeySet('{esc}', "_close")

Global $_main = GUICreate("", 501, 313, -1, -1)
Global $g_idMemo = GUICtrlCreateListView(" ", 2, 2, 496, 274, $LVS_NOCOLUMNHEADER)
_GUICtrlListView_SetColumnWidth ($g_idMemo, 0, 470)

; this label disables clicks on the lisview, kind of 'readonly'
GUICtrlCreateLabel("", 2, 2, 470, 274)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetState(-1, $GUI_ONTOP)

GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New")
GUICtrlSetBkColor($g_idMemo, 0xFFFFE0)
GUISetState(@SW_SHOW)

For $i = 1 to 20
    MemoWrite("test message " & $i)
    Sleep(100)
Next

$message = ". The quick brown fox jumps over the red lazy dog. Exceeding the text out of the column width."
; split the message to an array of lines, each line 50 chars max
$formatted = StringRegExp($message, '.{1,50}', 3)
;_ArrayDisplay($formatted)
For $i = 0 to UBound($formatted)-1
   MemoWrite($formatted[$i])
    Sleep(100)
Next

; this will display an empty line (looks like a @crlf)
MemoWrite()  

; this one is needed because it will be deleted by the first _GUICtrlListView_DeleteItem to come
$last_index = MemoWrite("will be deleted")  

For $i = 5 To 0 Step -1
    _GUICtrlListView_DeleteItem($g_idMemo, $last_index)
    $last_index = MemoWrite("Program will exit in " & $i & " seconds...")
    Sleep(1000)
Next

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

Func MemoWrite($sMessage = "")
   $index = _GUICtrlListView_AddItem($g_idMemo, $sMessage )
   _GUICtrlListView_EnsureVisible($g_idMemo, $index)
  Return $index
EndFunc   ;==>MemoWrite

Func _close()
    Exit
EndFunc   ;==>_close

 

Edited by mikell
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

×
×
  • Create New...