Jump to content

Previous data is removed after using GUICtrlSetData


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 post
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 post
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 post
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 post
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 post
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 post
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 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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By chacoya121
      Please help calculate the secs variable i can't get it right after 60s, it go into -1 and on....
      Func CountDown() Local $count = 5 Local $newcount = $count * 60 ; 300 sec Local $time = TimerInit() While 1 Sleep(1000) Local $timediff = TimerDiff($time)/1000 ; turn into sec Local $mins= Floor(($newcount - $timediff)/60) Local $secs = Floor($newcount/$count - $timediff); Need help this line can't calculate after 60s ToolTip("Mins = " & $mins & " Secs = " & $secs) WEnd EndFunc  
    • By TheDcoder
      Hi everyone,
      I am bit stumped as to why I am not able to set the time in the Date control
      #include <DateTimeConstants.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> Example() Func Example() GUICreate("My GUI get date", 200, 200, 800, 200) Local $idDate = GUICtrlCreateDate("1953/04/25", 10, 10, 185, 20, $DTS_TIMEFORMAT) ; to select a specific default format Local $sStyle = "HH:mm:ss" GUICtrlSendMsg($idDate, $DTM_SETFORMATW, 0, $sStyle) ; Set time Local $iRet = GUICtrlSetData($idDate, '13:33:37') ConsoleWrite('GUICtrlSetData returned ' & ($iRet = 1 ? 'success' : 'failure') & @CRLF) GUISetState(@SW_SHOW) ; Loop until the user exits. While GUIGetMsg() <> $GUI_EVENT_CLOSE WEnd MsgBox($MB_SYSTEMMODAL, "Time", GUICtrlRead($idDate)) EndFunc ;==>Example The documentation for GUICtrlSetData clearly mentions that it uses the same format as GUICtrlRead:
      But I get failure
      What gives?
      Thanks for the help in advance!
    • By 232showtime
      im getting strange output in array display,$split_[1] is not properly aligned to other arrays and why guictrlsetdata is not writting any data if i put comma(,) at the end of the text in $Input1???
       
      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Array.au3> #Region ### START Koda GUI section ### Form=C:\Users\user\Desktop\Script\StringSplit.kxf $Form1 = GUICreate("Form1", 623, 449, 192, 114) $Input1 = GUICtrlCreateInput("50UGITQ421X, 50UGITQ422X, 50UGITQ423X, 50UGITQ427X, 50UGITQ431X, 50UGITQ435X, 50UGITQ436X, 50UGITQ437X, 50UGITQ441X, 50UGITQ445X, 50UGITQ449X, 50UGITQ453X, 50UGITQ454X, 50UGITQ455X, 50UGITQ459X", 24, 16, 553, 21) $Split = GUICtrlCreateButton("Split", 24, 48, 75, 25) $List1 = GUICtrlCreateList("", 24, 96, 553, 97) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Split $read = GUICtrlRead($Input1) $split_ = StringSplit($read, ",") $Max = UBound($split_, 1) For $i = 1 To UBound($split_) - 1 ConsoleWrite($split_[$i] & @CRLF) ;~ ControlSetText($Form1, "", $List1, $split_[$i]) GUICtrlSetData($List1, $split_[$i]) ;~ GUICtrlSetData($List1, $split_[$i]) ;~ GUICtrlSetData($List1, $i) Next _ArrayDisplay($split_) EndSwitch WEnd  
    • By kashamalasha
      Hello.
      Could you help me find the answer for my issue. I'm trying to set fomatted data wich is selected from SQL to GUICtrlCreateEdit field.
      And the GUICtrlSetData function is inserting it in one line. But when I'm trying to do the same using MsgBox the data looks fine.
      I'm trying to not use _GUICtrlRichEdit UDF.
      Thanks in advance. Here is my test code.
      #include <GUIConstants.au3> #include <GUIListBox.au3> #include <SQLite.au3> #include <SQLite.dll.au3> Opt("GUIOnEventMode", 1) $hMainGUI = GUICreate("Test", 520, 240) $hListBox = _GUICtrlListBox_Create($hMainGUI, "", 10, 10, 180, 80) $hEdit = GUICtrlCreateEdit("", 10, 80, 500, 150) GUISetState(@SW_SHOW, $hMainGUI) GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") Local $hQuery, $aRow _SQLite_Startup() $hDB = _SQLite_Open('MyDB.sqlite') _SQLite_Query(-1, "SELECT ID ||"". "" || Name FROM Templates ORDER BY ID;", $hQuery) While _SQLite_FetchData($hQuery, $aRow) = $SQLITE_OK _GUICtrlListBox_AddString($hListBox, $aRow[0]) WEnd GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSE_Button") While 1 Sleep(100) WEnd Func _WM_COMMAND($hWnd, $iMsg, $wParam, $lParam) Local $hWndFrom, $iIDFrom, $iCode, $hWndListBox If Not IsHWnd($hListBox) Then $hWndListBox = GUICtrlGetHandle($hListBox) $hWndFrom = $lParam $iIDFrom = BitAND($wParam, 0xFFFF) ; Low Word $iCode = BitShift($wParam, 16) ; Hi Word Switch $hWndFrom Case $hListBox, $hWndListBox Switch $iCode Case $LBN_DBLCLK Select_Template(_GUICtrlListBox_GetCurSel($hListBox) + 1) Return 0 EndSwitch EndSwitch EndFunc ;==>_WM_COMMAND Func CLOSE_Button() _SQLite_Close() _SQLite_Shutdown() Exit EndFunc ;==>CLOSE_Button Func Select_Template($sListItem) _SQLite_QuerySingleRow($hDB, _ "SELECT Content " & _ "FROM Templates " & _ "WHERE ID = " & $sListItem & ";", $aRow) ;~ MsgBox(64, "Test: " & $sListItem, $aRow[0]) GUICtrlSetData($hEdit, $aRow[0]) WinSetTitle($hMainGUI, "", "Test: " & $sListItem) EndFunc ;==>Select_Template  


    • By Reizvoller
      Hello!
      When utilizing GUICtrlSetData to edit what is displayed in a GUICtrlCreateEdit field, is there a way to just make it write new lines rather than clearing what's there and writing the new information?
      For example ... 
      GUICtrlSetData ($editField, "Line one"&@CRLF , "") GUICtrlSetData ($editField, "Line two"&@CRLF , "") GUICtrlSetData ($editField, "Line three"&@CRLF , "") What happens now if those three things run one after another, the Edit field displays "Line One" , then that is replaced with "Line Two" and so on with the previous information disappearing and the subsequent taking its place. I'd like for it to say all three, one after another. Any documents on how I may accomplish this?
      Thanks!
      -Reiz
×
×
  • Create New...