Does Edit box can be append text?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Bral
Posts: 20
Joined: 04 Apr 2015, 21:24

Does Edit box can be append text?

Post by Bral » 27 May 2020, 09:57

append text to the end of text box, like FileAppend, and always show last line.

User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Does Edit box can be append text?

Post by flyingDman » 27 May 2020, 10:15

try this:

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"
guicontrol,, txt, %txt% 
guicontrol,focus,txt
send, ^{end}
return
14.3 & 1.3.7

teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Does Edit box can be append text?

Post by teadrinker » 27 May 2020, 10:33

Or like this:

Code: Select all

text := " add some more"

Gui, Add, Edit, w200 h200 hwndhEdit
Gui, Add, Button, xp y+5, Add Text
handler := Func("AppendText").Bind(text, hEdit)
GuiControl, +g, Button1, % handler
Gui, Show
Return

AppendText(text, hEdit) {
   static WM_GETTEXTLENGTH := 0x0E
        , EM_SETSEL        := 0xB1
        , EM_REPLACESEL    := 0xC2
   WinWait, ahk_id %hEdit%
   SendMessage, WM_GETTEXTLENGTH
   SendMessage, EM_SETSEL, ErrorLevel, ErrorLevel
   SendMessage, EM_REPLACESEL, true, &text
}

Bral
Posts: 20
Joined: 04 Apr 2015, 21:24

Re: Does Edit box can be append text?

Post by Bral » 27 May 2020, 10:36

teadrinker wrote:
27 May 2020, 10:33
Or like this:

Code: Select all

text := " add some more"

Gui, Add, Edit, w200 h200 hwndhEdit
Gui, Add, Button, xp y+5, Add Text
handler := Func("AppendText").Bind(text, hEdit)
GuiControl, +g, Button1, % handler
Gui, Show
Return

AppendText(text, hEdit) {
   static WM_GETTEXTLENGTH := 0x0E
        , EM_SETSEL        := 0xB1
        , EM_REPLACESEL    := 0xC2
   WinWait, ahk_id %hEdit%
   SendMessage, WM_GETTEXTLENGTH
   SendMessage, EM_SETSEL, ErrorLevel, ErrorLevel
   SendMessage, EM_REPLACESEL, true, &text
}
Thank you very much, this is I need code.

Bral
Posts: 20
Joined: 04 Apr 2015, 21:24

Re: Does Edit box can be append text?

Post by Bral » 27 May 2020, 10:39

flyingDman wrote:
27 May 2020, 10:15
try this:

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"
guicontrol,, txt, %txt% 
guicontrol,focus,txt
send, ^{end}
return
This code is not the best, because it is reload all text every time, text box will flash 0.0001 secend :p

User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Does Edit box can be append text?

Post by flyingDman » 27 May 2020, 11:47

Add:

Code: Select all

#\::
a := A_TickCount
loop,100
	ControlClick, Button1, A,,,1
MsgBox, % A_TickCount - a    	
return
to both scripts. @teadrinker 's version will take about 12.5 seconds to complete adding 100 small strings of text. My simple script does it in 3.125 seconds. @Bral, the 0.0001 second flash (btw it does not flash here...) is insignificant compared to these numbers.
@teadrinker : nice script; I learned something new.
14.3 & 1.3.7

teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Does Edit box can be append text?

Post by teadrinker » 27 May 2020, 13:02

@flyingDman
I'm not sure that your test is correct. Try this:

Code: Select all

text := " add some more"

Gui, Add, Edit, w200 h200 hwndhEdit
Gui, Add, Button, xp y+5, Add Text
handler := Func("AppendText").Bind(text, hEdit)
GuiControl, +g, Button1, % handler
Gui, Show
Return

AppendText(text, hEdit) {
   static WM_GETTEXTLENGTH := 0x0E
        , EM_SETSEL        := 0xB1
        , EM_REPLACESEL    := 0xC2
   start := A_TickCount
   WinWait, ahk_id %hEdit%
   Loop 100 {
      SendMessage, WM_GETTEXTLENGTH
      SendMessage, EM_SETSEL, ErrorLevel, ErrorLevel
      SendMessage, EM_REPLACESEL, true, &text
   }
   MsgBox, % A_TickCount - start
}
Or this:

Code: Select all

text := " add some more"

Gui, Add, Edit, w200 h200 hwndhEdit
Gui, Add, Button, xp y+5, Add Text
handler := Func("AppendText").Bind(text, hEdit)
GuiControl, +g, Button1, % handler
Gui, Show
Return

AppendText(text, hEdit) {
   static WM_GETTEXTLENGTH := 0x0E
        , EM_SETSEL        := 0xB1
        , EM_REPLACESEL    := 0xC2
   start := A_TickCount
   Loop 100 {
      SendMessage, WM_GETTEXTLENGTH,,,, ahk_id %hEdit%
      SendMessage, EM_SETSEL, ErrorLevel, ErrorLevel,, ahk_id %hEdit%
      SendMessage, EM_REPLACESEL, true, &text,, ahk_id %hEdit%
   }
   MsgBox, % A_TickCount - start
}
WinWait was here just to shorten the code.

User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Does Edit box can be append text?

Post by flyingDman » 27 May 2020, 13:26

I think my test more accurately simulate the process of manually adding by clicking the button. I noticed the difference in speed when i was clicking myself. It felt sluggish.
But to make it an apple to apple comparison:

Code: Select all

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

add:
a := A_TickCount
gui, submit, nohide
Loop, 100
	{
	txt .= " add some more"
	guicontrol,, txt, %txt% 
	}
guicontrol,focus,txt
send, ^{end}
MsgBox, % A_TickCount - a    	
return
will result is very similar times. Mine minimally slower. But I don't want to make this a p... contest. Your code shows an impressive way of doing this.
14.3 & 1.3.7

teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Does Edit box can be append text?

Post by teadrinker » 27 May 2020, 13:46

Try your test several times without removing an existing text. :)

Bral
Posts: 20
Joined: 04 Apr 2015, 21:24

Re: Does Edit box can be append text?

Post by Bral » 27 May 2020, 20:23

teadrinker wrote:
27 May 2020, 13:46
Try your test several times without removing an existing text. :)
I tested add 1000 times, your code is faster, But i don't know why need WM_GETTEXTLENGTH and EM_SETSEL? i delete that 2 line and code work fine.

teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Does Edit box can be append text?

Post by teadrinker » 27 May 2020, 22:01

It may work without these commands only if the caret is located at the end of the text.
WM_GETTEXTLENGTH gets the text length, EM_SETSEL sets the caret at the end, EM_REPLACESEL inserts the text at the caret position.

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

Re: Does Edit box can be append text?

Post by just me » 28 May 2020, 04:53


teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Does Edit box can be append text?

Post by teadrinker » 28 May 2020, 07:25

@just me
Interesting idea about SendMessage, 0x00B1, -2, -1, but it doesn't speed up the code.

Post Reply

Return to “Ask for Help (v1)”