Test if Gui Edit field is empty or not Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
CaptainProton
Posts: 46
Joined: 06 May 2017, 13:38

Test if Gui Edit field is empty or not

27 May 2017, 09:50

I'm working on my note-taking script. When I start taking notes, the GUI gets created with a large text area. In that text area, whenever I press enter, I receive a timestamp, after which I get to input a note. Once I hit enter again, I get two linebreaks, and again, a timestamp.

That is, at least, how I would like it to work. In reality, I get two line breaks even if this is my first entry, so instead of getting a result that looks like this:
Image

I get this:
Image

I tried solving it by using GuiControlGet, MyEdit and immediately afterwards checking with if (!MyEdit), so that I could send a different command depending on whether the edit field is empty or not, but it looks like I don't know how to do that properly. Here's the code of that failed attempt:

Code: Select all

#SingleInstance Force
global callStartTime, MyEdit

callStartTime := A_TickCount

WriteNotes:
	RunNoteWriter()
	Hotkey, IfWinActive, ahk_class AutoHotkeyGUI
	Hotkey, Enter, TimeStamp, On
	return

TimeStamp:
	callElapsedTime := A_TickCount - callStartTime
	callElapsedTime := calcCallTime(callElapsedTime)
	GuiControlGet, MyEdit
	
	if (!MyEdit)
		SendInput, %callElapsedTime%{enter}
	else
		SendInput, {enter 2}%callElapsedTime%{enter}
	return


RunNoteWriter()
	{
	Gui, new, +Resize +MinSize600x600, Insert notes
	Gui, Add, Edit, r2 vMyEdit VScroll
	Gui, Show
	
	Return
	
	GuiSize: 
	  If A_EventInfo = 1  ; The window has been minimized.  No action needed.
		Return
	  ; Otherwise, the window has been resized or maximized. Resize the controls to match.
	  GuiControl Move, MyEdit, % "H" . (A_GuiHeight-20) . " W" . (A_GuiWidth-20)
	Return
	}


calcCallTime(ms)
{
	year = 2017 ;any year above 1600
	year += Floor(ms / 1000), SECONDS
	FormatTime, time, %year%, HH:mm:ss
	return time
}
Can somebody explain what I'm doing wrong? Also, Is there maybe a better way?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Test if Gui Edit field is empty or not

27 May 2017, 10:27

Correct me if I'm wrong, I guess it's not necessary to use SendInput to add new content to a GUI Control?!
GuiControl,, MyEdit, New text line 1.`nNew text line 2.

(Blank): Leave Sub-command blank to put new contents into the control via Param3. Specifically: [...]
Text/[...] Specify for Param3 the control's new text. Since the control will not expand automatically, use GuiControl, [...]
Cosmetics (that won't necessarily solve your issue) ...

Code: Select all

TimeStamp:
	callElapsedTime := calcCallTime(A_TickCount - callStartTime)
	GuiControlGet, currentContent,, MyEdit
	
	updatedContent := if (currentContent != "" ) ?  "`n" . currentContent : currentContent ; edit this ternary operator to your liking
        GuiControl,, MyEdit, % updatedContent
Good luck :)
CaptainProton
Posts: 46
Joined: 06 May 2017, 13:38

Re: Test if Gui Edit field is empty or not

27 May 2017, 11:13

BoBo wrote:Correct me if I'm wrong, I guess it's not necessary to use SendInput to add new content to a GUI Control?!
GuiControl,, MyEdit, New text line 1.`nNew text line 2.

(Blank): Leave Sub-command blank to put new contents into the control via Param3. Specifically: [...]
Text/[...] Specify for Param3 the control's new text. Since the control will not expand automatically, use GuiControl, [...]
Cosmetics (that won't necessarily solve your issue) ...

Code: Select all

TimeStamp:
	callElapsedTime := calcCallTime(A_TickCount - callStartTime)
	GuiControlGet, currentContent,, MyEdit
	
	updatedContent := if (currentContent != "" ) ?  "`n" . currentContent : currentContent ; edit this ternary operator to your liking
        GuiControl,, MyEdit, % updatedContent
Good luck :)

Thanks but I really don't understand anything here... :D

Could you please explain a little bit more?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Test if Gui Edit field is empty or not

27 May 2017, 11:24

BoBo wrote:
GuiControl,, MyEdit, New text line 1.`nNew text line 2.

(Blank): Leave Sub-command blank to put new contents into the control via Param3. Specifically: [...]
Text/[...] Specify for Param3 the control's new text. Since the control will not expand automatically, use GuiControl, [...]
The above is an excerpt from AHK's help file. Let's assume you've checked it already :)

Check the code for comments ...

Code: Select all

TimeStamp:
	callElapsedTime := calcCallTime(A_TickCount - callStartTime) ; same as yours but "embedded" the logic of your previous line directly within the function call 
	GuiControlGet, currentContent,, MyEdit ; same as yours but let's set a specific variable for better "visability"
	
	updatedContent := if (currentContent != "" ) ?  currentContent "`n" callElapsedTime  : callElapsedTime ; if condition is true do this, else do that (the ternary operator. Check AHK's help for details)
        GuiControl,, MyEdit, % updatedContent ; update the control with (new) content
Hope that helps. Btw, not tested!
CaptainProton
Posts: 46
Joined: 06 May 2017, 13:38

Re: Test if Gui Edit field is empty or not

27 May 2017, 11:39

BoBo wrote:
BoBo wrote:
GuiControl,, MyEdit, New text line 1.`nNew text line 2.

(Blank): Leave Sub-command blank to put new contents into the control via Param3. Specifically: [...]
Text/[...] Specify for Param3 the control's new text. Since the control will not expand automatically, use GuiControl, [...]
The above is an excerpt from AHK's help file. Let's assume you've checked it already :)

Check the code for comments ...

Code: Select all

TimeStamp:
	callElapsedTime := calcCallTime(A_TickCount - callStartTime) ; same as yours but "embedded" the logic of your previous line directly within the function call 
	GuiControlGet, currentContent,, MyEdit ; same as yours but let's set a specific variable for better "visability"
	
	updatedContent := if (currentContent != "" ) ?  currentContent "`n" callElapsedTime  : callElapsedTime ; if condition is true do this, else do that (the ternary operator. Check AHK's help for details)
        GuiControl,, MyEdit, % updatedContent ; update the control with (new) content
Hope that helps. Btw, not tested!
I tried it, but it didn't work - when I hit enter, nothing happens. I understood what you were doing but assumed you expect me to modify it somehow to get it to work. That is why I asked for further clarification.

I also tried to reduce the code in order to isolate the problem, and for some reason even this doesn't work:

Code: Select all

#SingleInstance Force

Gui, new, +Resize +MinSize600x600, Insert notes
Gui, Add, Edit, r2 vMyEdit VScroll
Gui, Show

q::
GuiControlGet, currentContent,, MyEdit
MsgBox, %currentContent%
I keep hitting "q" and all I get is the empty msgbox, no matter how much text I write into the edit field.

Any idea what's wrong? I've looked at other examples on the forum and, based on that, this should be working.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Test if Gui Edit field is empty or not

27 May 2017, 11:57

Code: Select all

#SingleInstance, Force

; Gui, new, +Resize +MinSize600x600, Insert notes	; throws an error for me bc I use an "outdated" AHK release
Gui, Add, Edit, w180 h180 vMyEdit VScroll			; added w+h for testing
Gui, Show, w200 h200						; added w+h for testing
Return									; check out sample code within AHKs help why to use this



q::
    callElapsedTime := A_Hour ":" A_Min			; for testing
    GuiControlGet, currentContent,, MyEdit
    MsgBox % currentContent					; for testing
    updatedContent := currentContent != "" ?  currentContent "`n" callElapsedTime  : callElapsedTime "`n" ; to be edited by you
    GuiControl,, MyEdit, % updatedContent			; will update the content
    Return									; check out sample code within AHKs help why to use this
That's working fine for me :)
And of course, you should listen to A_AhkUser's more detailed statement :thumbup:
Last edited by BoBo on 27 May 2017, 12:20, edited 2 times in total.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Test if Gui Edit field is empty or not  Topic is solved

27 May 2017, 12:04

Hi,

Bobo made a typo; there's no if before the ternary operation.
Concerning your code note that (!MyEdit) is different from (MyEdit <> "") (the first is true if the text of your edit control is 0).
As Bobo already says there's no need to use SendInputcommand; consider using GuiControl (or Control, EditPaste) instead.
Your callStartTime doesn't need (unless it is just a part of your code) to be super global since you don't try to access it from within a function.
Instead of uisng ahk_class AutoHotkeyGUI as wintitle param for the Hotkeycommand I suggest you first storing the GUI ID by means
of GUI, +hwndID command and use it as identifier.
Then , you should avoid IMO define label inside functions - especially as here nothing justify it.
Finally, try use A_GUI, specify the GUI name or number etc. the more often you can to make your code more reliable.


Code: Select all

#SingleInstance Force
#Warn
#NoEnv

Gui, 1:New, +Resize +MinSize600x600 +HWNDID, Insert notes ; HWNDID stores the GUI ID in ID
Gui, 1:Add, Edit, r2 vMyEdit hwndCID VScroll
Gui, 1:Show
Hotkey, IfWinActive, ahk_id %ID%
Hotkey, Enter, TimeStamp, On
return

GuiSize: 
If (A_EventInfo = 1)
return
	GuiControl, %A_GUI%:Move, MyEdit, % "H" . (A_GuiHeight-20) . " W" . (A_GuiWidth-20)
return

TimeStamp:
GuiControlGet, MyEdit, 1:
Control, EditPaste, % ((MyEdit == "") ? "" : "`r`n`r`n") . calcCallTime() . "`r`n",, % "ahk_id " . CID
return

calcCallTime() {
	FormatTime, __timeString, % A_NowUTC, dddd d MMMM yyyy hh:mm:ss tt
return __timeString
}
Hope this helps.
my scripts
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Test if Gui Edit field is empty or not

27 May 2017, 12:19

A_AhkUser wrote:Hi,

Bobo made a typo; there's no if before the ternary operation.

[...]

Hope this helps.
Thx, its good to learn from the best :thumbup:
CaptainProton
Posts: 46
Joined: 06 May 2017, 13:38

Re: Test if Gui Edit field is empty or not

27 May 2017, 12:20

A_AhkUser wrote:Hi,

Bobo made a typo; there's no if before the ternary operation.
Concerning your code note that (!MyEdit) is different from (MyEdit <> "") (the first is true if the text of your edit control is 0).
As Bobo already says there's no need to use SendInputcommand; consider using GuiControl (or Control, EditPaste) instead.
Your callStartTime doesn't need (unless it is just a part of your code) to be super global since you don't try to access it from within a function.
Instead of uisng ahk_class AutoHotkeyGUI as wintitle param for the Hotkeycommand I suggest you first storing the GUI ID by means
of GUI, +hwndID command and use it as identifier.
Then , you should avoid IMO define label inside functions - especially as here nothing justify it.
Finally, try use A_GUI, specify the GUI name or number etc. the more often you can to make your code more reliable.


Code: Select all

#SingleInstance Force
#Warn
#NoEnv

Gui, 1:New, +Resize +MinSize600x600 +HWNDID, Insert notes ; HWNDID stores the GUI ID in ID
Gui, 1:Add, Edit, r2 vMyEdit hwndCID VScroll
Gui, 1:Show
Hotkey, IfWinActive, ahk_id %ID%
Hotkey, Enter, TimeStamp, On
return

GuiSize: 
If (A_EventInfo = 1)
return
	GuiControl, %A_GUI%:Move, MyEdit, % "H" . (A_GuiHeight-20) . " W" . (A_GuiWidth-20)
return

TimeStamp:
GuiControlGet, MyEdit, 1:
Control, EditPaste, % ((MyEdit == "") ? "" : "`r`n`r`n") . calcCallTime() . "`r`n",, % "ahk_id " . CID
return

calcCallTime() {
	FormatTime, __timeString, % A_NowUTC, dddd d MMMM yyyy hh:mm:ss tt
return __timeString
}
Hope this helps.
Wow, it helps a lot! Thank you so much!

I understand everything but the control, EditPaste part.
Control, EditPaste, % ((MyEdit == "") ? "" : "`r`n`r`n") . calcCallTime() . "`r`n",, % "ahk_id " . CID ; (MyEdit == "") is different from (!MyEdit)

Could you please explain this a little more? It would be really helpful! I'd like to understand this completely.

Also, how would it be done using GuiControl?
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Test if Gui Edit field is empty or not

27 May 2017, 12:33

CaptainProton wrote:Also, how would it be done using GuiControl?
See Bobo's remarks above:
documentation wrote:GuiControl, Sub-command, ControlID [, Param3]
(...)
Leave Sub-command blank to put new contents into the control via Param3.
So, using GuiControl:

Code: Select all

GuiControl,, MyEdit, % ((MyEdit == "") ? "" : "`r`n`r`n") . calcCallTime() . "`r`n"
Concerning:

Code: Select all

Control, EditPaste, % ((MyEdit == "") ? "" : "`r`n`r`n") . calcCallTime() . "`r`n",, % "ahk_id " . CID
documentation wrote:Control, Cmd [, Value, Control, WinTitle, WinText, ExcludeTitle, ExcludeText]
(...)
EditPaste, String: Pastes String at the caret/insert position in an Edit control (this does not affect the contents of the clipboard).
(...)
To operate upon a control's HWND (window handle), leave the Control parameter blank and specify ahk_id %ControlHwnd% for the WinTitle parameter

Concerning the ternary operation:

Code: Select all

((expression) ? "A" : "B")
is the same in-line as a call of this func:

Code: Select all

myFunc() {
if (expression)
return "A"
else
return "B"
}
for instance:

Code: Select all

MsgBox % (true) ? "test" : "blabla" ; display test
MsgBox % (false) ? "test" : "blabla" ; display blabla
my scripts
CaptainProton
Posts: 46
Joined: 06 May 2017, 13:38

Re: Test if Gui Edit field is empty or not

27 May 2017, 12:51

A_AhkUser wrote:
CaptainProton wrote:Also, how would it be done using GuiControl?
See Bobo's remarks above:
documentation wrote:GuiControl, Sub-command, ControlID [, Param3]
(...)
Leave Sub-command blank to put new contents into the control via Param3.
So, using GuiControl:

Code: Select all

GuiControl,, MyEdit, % ((MyEdit == "") ? "" : "`r`n`r`n") . calcCallTime() . "`r`n"
Concerning:

Code: Select all

Control, EditPaste, % ((MyEdit == "") ? "" : "`r`n`r`n") . calcCallTime() . "`r`n",, % "ahk_id " . CID
documentation wrote:Control, Cmd [, Value, Control, WinTitle, WinText, ExcludeTitle, ExcludeText]
(...)
EditPaste, String: Pastes String at the caret/insert position in an Edit control (this does not affect the contents of the clipboard).
(...)
To operate upon a control's HWND (window handle), leave the Control parameter blank and specify ahk_id %ControlHwnd% for the WinTitle parameter

Concerning the ternary operation:

Code: Select all

((expression) ? "A" : "B")
is the same in-line as a call of this func:

Code: Select all

myFunc() {
if (expression)
return "A"
else
return "B"
}
for instance:

Code: Select all

MsgBox % (true) ? "test" : "blabla" ; display test
MsgBox % (false) ? "test" : "blabla" ; display blabla

Man, you're really good at explaining! I love it! I understood everything perfectly.

Btw, why do you use both `r`n ? Aren't they supposed to be the same thing? And if they are, why aren't they both doing the same thing, and instead acting as only one action?
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Test if Gui Edit field is empty or not

28 May 2017, 11:11

To be honest can't say why I use both! :? `r and `n stand respectively for carriage return and linefeed characters; they aren't the same. If you search in notepad++ for this character using the extended mode (where you can search for this characters using \r and \n) each occurrence is highlighted and it appears that linefeed is the character at the end of the line but invisible while carriage return is zero-width character at the beginnig ot the new line above. I saw a while ago that Linux use only the newline character while windows does not need both carriage return and linefeed in order to be identified a newline character. Bad or good habit? Can't really say... I use both for a while and exept from the case of a continuation section:

Code: Select all

var =
(
printemps
été
automne
hiver
)
Loop % (arr:=StrSplit(var, "`n")).length()
MsgBox % arr[a_index]
which use `n as delimiter an some other case where it is explicity mentionned in the documentation that `n is used as delimiter - for instance IniRead when it is question to retrieve the section names:

Code: Select all

IniRead, OutputVarSectionNames, % A_Desktop . "\settings.ini"
Loop % (arr:=StrSplit(OutputVarSectionNames, "`n")).length()
MsgBox % arr[a_index]
...it appears to work.

Concerning the ternary operation. One can believe due to the simplification above that both last elements can only be strings: of course it's not; a general visualisation could be:

Code: Select all

((expression1) ? expression2 : expression3)
For instance, this code below using expressions for the three terms of the ternary operation is possible, off course:

Code: Select all

var := "test"
if ((true) ? false : true)
{
	MsgBox, this MsgBox will never be displayed.
}
else
{
	MsgBox % (StrLen("test") == 10) ? (true and false) : 7*MyFunc(10)
	MsgBox % (StrLen("test") == 4) ? ("blabla" . A_Space . var) : 7*MyFunc(10)
}

MyFunc(__prm) {
return __prm + 1
}
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], ntepa and 108 guests