Count characters - GUI Edit

Get help with using AutoHotkey and its commands and hotkeys
Albireo
Posts: 1311
Joined: 16 Oct 2013, 13:53

Count characters - GUI Edit

06 Mar 2020, 07:26

Trying to count characters in a GUI edit box.
Each new row causes problems with the count of characters.

Wrote an example to show the count problem .:

Code: Select all

#SingleInstance force

MaxChar = 10
NoChr = 0


; Charactercount
Gui Font, cPurple s12 Normal, Arial
Gui Add, text, x50 y30 h30 w150 left vCount, % "( " MaxChar " / " MaxChar " )"

Gui Font, cBlue s22 Normal, Arial
Gui Add, Edit, x20 y60 r7 w420 Limit%MaxChar% vTestTxt gGetCount

Gui Show
Return




GetCount:
	Gui Submit, NoHide
	GuiControlGet myString,, TestTxt
	NoChr := MaxChar - StrLen(myString)
	GuiControl,, Count, %  "( " NoChr " / " MaxChar " )"
	
Return
If I enter 1234567890 (the Edit limit, stops the number of characters. (OK!)
But, If I enter in the edit field the following characters .:
123 (enter)
456 (enter)

The limit stops right now (But the counter shows 2/10)
(I think the problem is `n`r or CR LF and StrLen() can't count as I want)

- How to remove one character? or
- How to count the characters in the GUI Edit field right?
How to count th
just me
Posts: 7414
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Count characters - GUI Edit

06 Mar 2020, 08:24

It's one of the AHK quirks. Edit controls use `r`n (i.e 2 characters) as line breaks. Gui, Submit and GuiControlGet, ... remove the `r when storing the text in a variable.
Albireo
Posts: 1311
Joined: 16 Oct 2013, 13:53

Re: Count characters - GUI Edit

06 Mar 2020, 08:27

But how to count (or must I skip the "limit") ? Limit%MaxChar%
just me
Posts: 7414
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Count characters - GUI Edit

06 Mar 2020, 08:41

I don't understand your question.

Code: Select all

1234567890
123`r`n456`r`n
Both are 10 characters long within the Edit.
User avatar
TLM
Posts: 1589
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: Count characters - GUI Edit

06 Mar 2020, 08:44

NoChr := MaxChar - StrLen( StrReplace( myString, "`n", ".." ) )
Albireo
Posts: 1311
Joined: 16 Oct 2013, 13:53

Re: Count characters - GUI Edit

06 Mar 2020, 11:01

Thanks!
TLM wrote:
06 Mar 2020, 08:44
NoChr := MaxChar - StrLen( StrReplace( myString, "`n", ".." ) )
Yes! (Now the counter counts "right"). But...

Before I use the result I want to remove all `r
I have tried to create a dynamic "limit" - for each "enter" (`r`n) the "limit" increases +1 (But failed :( )
eg. In the example Limit = 10,
  • one "Enter" -> Limit = 11
  • two "Enter" -> Limit = 12
etc.
If a dynamic "limit" had worked, the counting of characters would still work (despite "`r ")


Backgruond .:
I am writing an SMS program with AHK.
An SMS (In Sweden) can hold 160 characters and I want to a calculate how many characters are left before the SMS is full.
The problem with "`r "complicates the calculation.
Before using the SMS-text, all "`r "are deleted with something like that .: TestTxt := StrReplace (TestTxt, "`r", "")
User avatar
TLM
Posts: 1589
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: Count characters - GUI Edit

06 Mar 2020, 15:56

the manual wrote:Any linefeeds (`n) in Value that lack a preceding carriage return (`r) are automatically translated to CR+LF (`r`n) to make them display properly. However, this is usually not a concern because the Gui Submit and GuiControlGet OutputVar commands will automatically undo this translation by replacing CR+LF with LF (`n).
Albireo
Posts: 1311
Joined: 16 Oct 2013, 13:53

Re: Count characters - GUI Edit

06 Mar 2020, 18:56

Yes! (OK!)
But it doesn't solve my desire.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], boiler, doge sayer, effel, LakaiHigh, mikeyww, TXShooter, Xtra, zhainv2013 and 33 guests