Wrote a solution / example to handle the edit fields as my desire above. (and I think it works)
- Increased the number of edit fields to four.
- The number of characters remaining is displayed below the message result.
However, failed to handle
Enter and the
GUI button (with a window name as an variable)
So this desire have I ignore.
What I still found tricky to deal with, was counting characters and providing informative messages if the limit of maximum number characters being exceeded.
Another thing that seems difficult to solve is the
pling sound along with the GUI.
sometimes no
pling is heard, sometimes the
pling is cuted, and of course, sometimes the
pling sounds as desired
This is what the test code looks like now .:
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance force
#Persistent
WinName = Counting Characters!
GroupAdd hkgroup, %WinName% ; To use with #IfWinActive ahk_group hkgroup
MaxChar = 60 ; 47(Head+Foot) + 13 free characters
MessHead := "Hello!`nSome text "
MessFoot := "`nThe end of the message`nRegards"
LenChar := StrLen(MessHead) + StrLen(MessFoot)
; GUI handle
Gui 1: Font, cgray s12 Normal, Arial
Gui 1: Add, text, x20 y20, Information .:
Gui 1: Font, cBlue s22 Normal, Arial
Gui 1: Add, Edit, x30 y+0 h35 w260 vText1 gGetCount
Gui 1: Add, Edit, x30 y+2 h35 w260 vText2 gGetCount
Gui 1: Add, Edit, x30 y+2 h35 w260 vText3 gGetCount
Gui 1: Add, Edit, x30 y+2 h35 w260 vText4 gGetCount
Gui 1: Font, cGreen s16 Normal, Arial
Gui 1: Add, text, x400 y20 h150 w300 vResult, %MessHead% %MessFoot%
Gui 1: Font, cGreen s14 Normal, Arial
Gui 1: Add, text, x400 y200 h20 w300 vCount, % "Characters left .: " MaxChar - LenChar
Gui 1: Font, cred s14 Bold, Arial
Gui 1: Add, text, x400 y220 h20 w300 vCountMess hidden, % "No characters left"
Gui 1: Font, s18 Bold, Arial
Gui 1: Add, Button, x100 y200 h40 w100 center vButtNext gReady, Ready!
Gui 1: Show, x100 y100 w700 h250, %WinName%
; WinGet WinID,, %WinName%
; MsgBox % WinID
Return
; #IfWinActive(WinName)
; #IfWinActive("ahk_id " WinID)
#IfWinActive ahk_group hkgroup
~NumpadEnter::
~Enter::
GuiControlGet FocusOn, FocusV ; Kontrollera var Focus finns
Loop 3
{ If ( FocusOn = "Text" A_Index )
{ NextField := A_Index + 1
GuiControl 1: Focus, Text%NextField% ; Sätt fokus på nästa fält
Break
}
}
Return
GetCount:
Gui 1: Submit, NoHide
RowCount = 0
Message := MessHead
Loop 4 ; Number of EditFields
{ GuiControlGet InpText%A_Index%, 1:, Text%A_Index% ; Catch the text that is being written in each editfields
If (StrLen(Text%A_Index%) > 0)
{ RowCount += 1
If (RowCount = 1)
Message .= Text%A_Index%
else
{ ; If a new edit field has been entered, a comma will also be added (two characters)
If ( MaxChar - StrLen(Message) > 2 )
Message .= "," Text%A_Index%
else ; Message too long
{ GuiControl 1: Show, CountMess ; Maximum number of characters
SoundBeep 500, 500
}
}
}
}
Message .= MessFoot
CharCount := MaxChar - StrLen(Message)
GuiControl 1: , Count, % "Characters left .: " CharCount
If (CharCount >= 0)
{ GuiControl 1: , Result, % Message
Gui 1: Font, s16 cGreen, Normal, Arial
GuiControl 1: Font, Count
If(CharCount > 0)
GuiControl 1: Hide, CountMess
else
GuiControl 1: Show, CountMess ; Maximum number of characters
}
else
{ Gui 1: Font, s16 cRed, Bold, Arial
GuiControl 1: Font, Count
GuiControl 1: Show, CountMess ; Maximum number of characters
SoundBeep 500, 500
}
Return
ESC::
EndProg:
GuiClose:
GuiEscape:
MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%,
( LTrim
ESC is pressed! or `n
Windows is closed! `n`n
This program will be terminated!
)
MsgBox ,,, Program is terminated!, 1
ExitApp
Return
Ready:
MsgBox ,, %A_ScriptName% - Rad %A_LineNumber%, Text1 .: %Text1% `nText2 .: %Text2% `nCharCount .: %CharCount%
ExitApp
Return