Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Edit field 4-digit number by adding leading zeros



  • Please log in to reply
4 replies to this topic
cogitamentum
  • New members
  • 2 posts
  • Last active: Oct 17 2015 09:17 AM
  • Joined: 14 Oct 2015

Hello,

First of all I'd like to say sorry if this topic was already discussed but I couldn't find a proper solution of it.

Therefore I decided to describe problem on my own.

 

So, I need to create an edit field in which you write a number. I would like to add leading zeros to a number such that it always becomes a 4-digit number.

 

Exempli gratia: While you start giving the number, for example 1 - in edit field it should display as 0001 and when you continue giving number like 11, it should display as 0011.

 

Please have in mind I'm just a beginner and I'd like to keep the script as simple as possible.

 

I attached a part of the code.

 

Most important, thank you in advance for any help and time spent on answering this post.

 

Thank you.

Gui, Show, h100 w140, 
Gui, Font, s14, 
Gui, Add, Edit, Center Number x40 y35 h30 w60 Limit4 vXXXX

Return
GuiClose: 
ExitApp


joedf
  • Administrators
  • 986 posts
  • AutoHotkey Foundation
  • Last active: Nov 02 2019 08:38 PM
  • Joined: 20 May 2012
Pre-answered on stackoverflow here:
"Add leading zero to make a 4-digit number in Autohotkey"
http://stackoverflow...24100686/883015
Why ahkscript.org? - autohotkey.com is outdated

Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012
✓  Best Answer

joedf's answer is half of it.

 

You'll also want to use a gLabel to make the Edit box update as the user interacts with it. You can use the GuiControl command to update a control automatically.

 

Edit: Actually, there's a bit more. I'm troubleshooting how you can work around this. Here's my example code:

^8::
Gui, Show, h100 w140,
Gui, Font, s14,
Gui, Add, Edit, Center Number x40 y35 h30 w60 vXXXX gquery

Return
GuiClose:
ExitApp

query:
Gui, Submit, NoHide
GuiControl,,%A_GuiControl%,% SubStr("0000" XXXX,-3)
return

It works great for one digit numbers (I removed the Limit4 because the SubStr will do that for us), but it becomes a bit problematic as focus on the control goes back to the left of the control. If I type a "1", the box shows 0001. But the cursor is at the left side, so if I type a "5", you'd expect to see 50001. But the label query processes it as taking the last four digits of the string 000050001, so you still only see 0001 in the edit field.

 

If you manually move the cursor back to the far right, it processes things perfectly where typing a "5" results in 0015. I'm working to figure out how to make the cursor jump to the end automatically.

 

Edit, this works in 99% of circumstances. But you'll want to add a little bit of processing to make sure things are fine:

^8::
Gui, Show, h100 w140, Sample
Gui, Font, s14,
Gui, Add, Edit, Center Number x40 y35 h30 w60 vXXXX gquery

Return
GuiClose:
ExitApp

query:
Gui, Submit, NoHide
GuiControl,,%A_GuiControl%,% SubStr("0000" XXXX,-3)
ControlSend, Edit1, {End}, Sample
Sleep 50 ; necessary to avoid recursion of the query label
return

It is possible to type faster than the gLabel can be triggered and processed which means numbers longer than 4 digits can be put into the edit box. You can make sure that's fine by checking the StringLength of the variable XXXX when you are ready to use it in your code's main purpose.

 

Someone may know of a better way to show padded zeroes.



cogitamentum
  • New members
  • 2 posts
  • Last active: Oct 17 2015 09:17 AM
  • Joined: 14 Oct 2015

Thank you Exaskryz for so much effort for this problem. I really appreciate your help.

To joedsf: I have read this topic but they did not explain much in it -  and also it wasn't exactly what I am asking here.

 

I think it is now solved.

 

Exaskryz can I contact you somehow if I had any more problems?

 

Thank you again.



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

You're welcome.

 

Feel free to keeping posting over at this forum when you need some help. Soon, a different forum will be available at autohotkey.com/boards/ (note the s in boards), so watch for that as that will become the primary forum for posting (you can currently post to it at ahkscript.org/boards/ if you wanted to; though I rarely visit there). I prefer public posts over private messages due to other members being able to contribute when they can which can expedite solving problems.