Add new line to Edit box, instead of changing it? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
michalb93
Posts: 46
Joined: 29 Sep 2022, 04:35

Add new line to Edit box, instead of changing it?

Post by michalb93 » 06 Oct 2022, 07:45

Basically, my code works like that (its soo long to put it there):

Code: Select all

Click
Wait
(...)
[if successfull, add log "All X readings completed!" to Edit box (I call it log box)

Click
[do something]
Click
[if successfull, add "All Y readings good!" to Edit]

Click
[some more actions]
[if successfull, add "All Z readings good!" to Edit]
The problem is, instead of adding NEW line, it just REPLACES Y with X, or Z with Y, so no new lines appear. I've tried with something like that:

Code: Select all

Reading:     
 {
 Actions
 Clicks
gui, submit, nohide   
txt .= "Reading X good!`n"
guicontrol,, txt, %txt% 
guicontrol,focus,txt
GuiControl,, Testss, %txt%

More actions
More Clicks
gui, submit, nohide   
txt2 .= "Reading Y good!`n"
guicontrol,, txt2, %txt2% 
guicontrol,focus,txt2
GuiControl,, Testss, %txt2%
 }
return

 Actions
 Clicks
gui, submit, nohide   
txt3 .= "Reading Z good!`n"
guicontrol,, txt3, %txt3% 
guicontrol,focus,txt3
GuiControl,, Testss, %txt3%
Didn't work, it was still replacing lines, instead of adding new ones. This example from above I found somewhere else, this is original code which works:

Code: Select all

var := "some text"

gui, add, edit, x0 y0 w200 h200 vtxt, % var
gui, add, button, x0 y+5 gadd, add some more
gui, show
return

add:
gui, submit, nohide
txt .= " and some more`n"
guicontrol,, txt, %txt% 
guicontrol,focus,txt
send, ^{end}
return
Well, I'm out of ideas what I'm doing wrong...

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Add new line to Edit box, instead of changing it?

Post by mikeyww » 06 Oct 2022, 09:26

It works, but your subroutine does not stop with GuiControl; instead, it proceeds to the next line. Use Return where needed.

Code: Select all

txt := "some text"
Gui, Font, s10
Gui, Add, Edit, w200 h200 vtxt, %txt%
Gui, Add, Button, gAdd, Add some more
Gui, Show
Return

LButton::
Add:
Gui, Submit, NoHide
GuiControl,, txt, % txt .= "`nand some more"
Return

michalb93
Posts: 46
Joined: 29 Sep 2022, 04:35

Re: Add new line to Edit box, instead of changing it?

Post by michalb93 » 06 Oct 2022, 12:37

mikeyww wrote:
06 Oct 2022, 09:26
It works, but your subroutine does not stop with GuiControl; instead, it proceeds to the next line. Use Return where needed.

Code: Select all

txt := "some text"
Gui, Font, s10
Gui, Add, Edit, w200 h200 vtxt, %txt%
Gui, Add, Button, gAdd, Add some more
Gui, Show
Return

LButton::
Add:
Gui, Submit, NoHide
GuiControl,, txt, % txt .= "`nand some more"
Return

I was using Return at end of g-label actions, so it looked more like:

Code: Select all

Reading:
{
Action1
Add log1 after success

Action2
Add log2 after success

Action3
Add log3 after success

}
return
My script works in a way that it clicks buttons in some application and adds log into GUI with Edit box. When everything is done, GUI is being maximized (and shows 1 log, instead of 3...). So shall I use Return at every end of Action1/Action2 etc?

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Add new line to Edit box, instead of changing it?

Post by mikeyww » 06 Oct 2022, 13:10

I cannot answer your question based on the information that you have provided in your posts. You are welcome to post your script in a reply below. The approach is basic: the subroutine in my script adds text to the control. You can call it whenever it is needed. You can test what is posted first, to see if it works.

michalb93
Posts: 46
Joined: 29 Sep 2022, 04:35

Re: Add new line to Edit box, instead of changing it?

Post by michalb93 » 06 Oct 2022, 13:23

mikeyww wrote:
06 Oct 2022, 13:10
I cannot answer your question based on the information that you have provided in your posts. You are welcome to post your script in a reply below. The approach is basic: the subroutine in my script adds text to the control. You can call it whenever it is needed. You can test what is posted first, to see if it works.
Here is one of g-labelled Button's action (This 'Testss' in 'GuiControl,, Testss,' means Edit box where "logs" are added.

Code: Select all

Reading:     
 {
Click, 460 55 ;Data tab
Sleep 700
Click, 161 436 ;Module status tab on list
Sleep 400
Click, 348 145  ;green load button
WinWaitActive, Communication in progress...
Sleep 100
WinWaitClose, Communication in progress...
Sleep 750

Click 1025 932
Sleep 150
Send ^a
Sleep 150
Send ^c
Sleep 150
GuiControl,, Testss, Module status has been read!

IF Clipboard NOT contains FAILED
     GuiControl,, progress1, +100

Click, 150 355 ;event logs tab on left
Sleep 450
Click, 349 179  ;green load button 1st tab
WinWaitActive, Communication in progress...
Sleep 100
WinWaitClose, Communication in progress...
Sleep 750
Click, 351 237   ;2nd green load
WinWaitActive, Communication in progress...
Sleep 100
WinWaitClose, Communication in progress...
Sleep 750

Click 1025 932
Sleep 150
Send ^a
Sleep 150
Send ^c
Sleep 150
IF Clipboard NOT contains FAILED
     GuiControl,, progress2, +33

Click, 556 137  ;2nd tab
Sleep 400
Click, 349 179  ;green load button 2nd tab
WinWaitActive, Communication in progress...
Sleep 100
WinWaitClose, Communication in progress...
Sleep 500

Click 1025 932
Sleep 150
Send ^a
Sleep 150
Send ^c
Sleep 150
IF Clipboard NOT contains FAILED
     GuiControl,, progress2, +33
Sleep 800

Sleep 750
Click, 729 136  ;3rd tab
Sleep 300
Click, 353 182  ;green load button
WinWaitActive, Communication in progress...
Sleep 100
WinWaitClose, Communication in progress...
Sleep 750
Click, 349 234  ;2nd click green
WinWaitActive, Communication in progress...
Sleep 100
WinWaitClose, Communication in progress...
Sleep 750

Click 1025 932
Sleep 150
Send ^a
Sleep 150
Send ^c
Sleep 150

GuiControl,, Testss, All logs have been read!
IF Clipboard NOT contains FAILED
     GuiControl,, progress2, +34
Sleep 800

;profiles
Click, 158 323  ;load profiles on list
Sleep 500
Click, 349 181  ;green load button
WinWaitActive, Communication in progress...
Sleep 100
WinWaitClose, Communication in progress...
Sleep 980

Click 1025 932
Sleep 150
Send ^a
Sleep 150
Send ^c
Sleep 150
IF Clipboard NOT contains FAILED
     GuiControl,, progress3, +16
Sleep 600

Click, 496 136  ;profile 2
Sleep 500
Click, 351 184  ;green button
WinWaitActive, Communication in progress...
Sleep 100
WinWaitClose, Communication in progress...
Sleep 980

Click 1025 932
Sleep 150
Send ^a
Sleep 150
Send ^c
Sleep 150
IF Clipboard NOT contains FAILED
     GuiControl,, progress3, +16
Sleep 600

Click, 617 138  ;profile 3
Sleep 500
Click, 354 190  ;green button
WinWaitActive, Communication in progress...
Sleep 100
WinWaitClose, Communication in progress...
Sleep 980

Click 1025 932
Sleep 150
Send ^a
Sleep 150
Send ^c
Sleep 150
IF Clipboard NOT contains FAILED
     GuiControl,, progress3, +16
Sleep 600

Click, 744 139  ;profile 4
Sleep 500
Click, 351 183  ;green button
WinWaitActive, Communication in progress...
Sleep 100
WinWaitClose, Communication in progress...
Sleep 980

Click 1025 932
Sleep 150
Send ^a
Sleep 150
Send ^c
Sleep 150
IF Clipboard NOT contains FAILED
     GuiControl,, progress3, +16
Sleep 600

Click, 850 138  ;profile 5
Sleep 500
Click, 351 183  ;green button
WinWaitActive, Communication in progress...
Sleep 100
WinWaitClose, Communication in progress...
Sleep 980

Click 1025 932
Sleep 150
Send ^a
Sleep 150
Send ^c
Sleep 150
IF Clipboard NOT contains FAILED
     GuiControl,, progress3, +16
Sleep 600

Click, 974 132  ;profile 6
Sleep 500
Click, 350 182  ;gren button
WinWaitActive, Communication in progress...
Sleep 100
WinWaitClose, Communication in progress...
Sleep 6400

Click 1089 932
Sleep 150
Send ^a
Sleep 150
Send ^c
Sleep 300
if Clipboard NOT contains FAILED
{
     GuiControl,, progress3, +20
     GuiControl,, Testss, All profiles have been read! No errors were detected.
}
ELSE
GuiControl,, Testss, Something went wrong!

Click, 152 52  ;disconnect

 }
return

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Add new line to Edit box, instead of changing it?

Post by mikeyww » 06 Oct 2022, 14:46

You have posted a script that shows how to append text to a variable, and then use GuiControl to update the control with the new text. That is what you would want to do each time.

just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Add new line to Edit box, instead of changing it?

Post by just me » 07 Oct 2022, 05:01

For example:

Code: Select all

#NoEnv
Gui, +AlwaysOnTop
Gui, Add, Edit, w300 r20 vMyEdit hwndHED ; you need the HWND of the control
Gui, Show, , Test

Loop, 25
{
   GuiEditAppend(HED, "Line " . A_Index . "`r`n") ; needs `r`n for a proper line break
   Sleep, 500
}
Return

GuiCLose:
ExitApp

GuiEditAppend(HED, Append) {
   SendMessage, 0x00B1, -2, -1, , ahk_id %HED%           ; EM_SETSEL (original idea by SKAN): sets the caret to the end of text
   SendMessage, 0x00C2, False, &Append, , ahk_id %HED%   ; EM_REPLACESEL
   ; The following command might be needed on some older versions of Windows to scroll the appended line into view
   ; SendMessage, 0x00B7, 0, 0, , ahk_id %HED%             ; EM_SCROLLCARET
}

michalb93
Posts: 46
Joined: 29 Sep 2022, 04:35

Re: Add new line to Edit box, instead of changing it?

Post by michalb93 » 07 Oct 2022, 05:47

just me wrote:
07 Oct 2022, 05:01
For example:

Code: Select all

#NoEnv
Gui, +AlwaysOnTop
Gui, Add, Edit, w300 r20 vMyEdit hwndHED ; you need the HWND of the control
Gui, Show, , Test

Loop, 25
{
   GuiEditAppend(HED, "Line " . A_Index . "`r`n") ; needs `r`n for a proper line break
   Sleep, 500
}
Return

GuiCLose:
ExitApp

GuiEditAppend(HED, Append) {
   SendMessage, 0x00B1, -2, -1, , ahk_id %HED%           ; EM_SETSEL (original idea by SKAN): sets the caret to the end of text
   SendMessage, 0x00C2, False, &Append, , ahk_id %HED%   ; EM_REPLACESEL
   ; The following command might be needed on some older versions of Windows to scroll the appended line into view
   ; SendMessage, 0x00B7, 0, 0, , ahk_id %HED%             ; EM_SCROLLCARET
}
That did the job, thanks! I didn't even know that something like HED/HWND exists and what is it for. I was aiming for easier version, but that works well, thanks :salute:

michalb93
Posts: 46
Joined: 29 Sep 2022, 04:35

Re: Add new line to Edit box, instead of changing it?

Post by michalb93 » 10 Oct 2022, 02:37

@just me
Well, new problem now. When I want to add time to line, it just shows %TimeString% instead of, let's say, 15:00:30. I was using:

Code: Select all

FormatTime, TimeString, T12, Time
GuiEditAppend(HED, "[%TimeString%] Module status has been read! "  "`r`n")
But it shows no time to line... Is it because of this hwndHED/HED?

just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Add new line to Edit box, instead of changing it?  Topic is solved

Post by just me » 10 Oct 2022, 03:21

@michalb93,

you need to use Expressions to pass parameters to a function:

Code: Select all

GuiEditAppend(HED, "[" . TimeString . "] Module status has been read!`r`n")

Post Reply

Return to “Ask for Help (v1)”