Edit Numbers With . [AHK_L 1.1]

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
maestrith
Posts: 825
Joined: 16 Oct 2013, 13:52

Edit Numbers With . [AHK_L 1.1]

23 Aug 2017, 08:45

Acuena on the Discord asked if there was a way to create an Edit control that accepted Numbers and "." So I wrote this class to help.

Code: Select all

/*
	Input values:
	Edit1:=new NumberEdit(1,"w200","Numbers Only",1)
	|                     |    |          |       |-Negative Possible
	|                     |    |          |---------Pre-Entered Text
	|                     |    |--------------------Control Options
	|                     |-------------------------Window Name/Number
	|-----------------------------------------------Object to get values from
	Getting Information:
	Value:=Edit1[]
	Setting Values:
	Edit1.Value(44) ;Changes the value
*/
Edit1:=new NumberEdit(1,"w200","Numbers Only")
Edit2:=new NumberEdit(1,"w200","Numbers Only")
Edit3:=new NumberEdit(1,"w200","Numbers Only")
Gui,Add,Button,gShowValues Default,Show Values
Gui,Add,Button,gSetValue Default,Set Value To 23
Gui,Show
return
SetValue:
Edit1.Value(23)
Edit2.Value(23)
Edit3.Value(23)
return
ShowValues:
MsgBox,% Edit1[] "`n" Edit2[] "`n" Edit3[]
return
GuiClose:
GuiEscape:
ExitApp
return

Code: Select all

Class NumberEdit{
	__New(Gui,Options,Text="",Negative:=0){
		static Count:=0
		static
		Gui,%Gui%:Add,Edit,% Options " gNumberEdit.Notify hwndHWND vNumber" ++Count,%Text%
		this.HWND:=HWND,this.Gui:=Gui,obj:=NumberEdit["Number" Count]:=[],obj.Negative:=Negative,obj.HWND:=HWND,obj.GUI:=Gui
		return this
	}__Get(){
		if(WinExist("ahk_id" this.HWND)){
			ControlGetText,Number,,% "ahk_id" this.HWND
			if(RegExReplace(Number,"(,|-|\.)")~="(\D)"=0)
				return Number
		}
	}Value(Value:=""){
		GuiControl,% this.Gui ":",% this.HWND,%Value%
	}Notify(){
		this:=NumberEdit[A_GuiControl]
		ControlGetText,Number,,% "ahk_id" this.HWND
		SendMessage,0xB0,,,,% "ahk_id" this.HWND
		pos:=ErrorLevel&0xff,RegExReplace(Number,"-",,CheckNegative)
		if(this.Negative)
			(Mod(CheckNegative,2)?(Negative:="-",pos++):(pos--))
		Number:=RegExReplace(Number,"[^\d|\.]",,Count)
		if(Count){
			GuiControl,% this.Gui ": +g",% this.HWND
			GuiControl,,% this.HWND,%Negative%%Number%
			SendMessage,0xB1,% pos-1,% pos-1,,% "ahk_id" this.hwnd
			GuiControl,% this.Gui ": +gNumberEdit.Notify",% this.HWND
		}
	}
}
Hope it helps someone else :)
Last edited by maestrith on 23 Aug 2017, 10:57, edited 9 times in total.
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
Acuena
Posts: 20
Joined: 27 Jan 2014, 14:56

Re: Edit Numbers With . [AHK_L 1.1]

23 Aug 2017, 09:24

Thanks, it helped alot :)

To change the value, use the following code:

<Object to get values from>.Value(<New value>)
Last edited by Acuena on 23 Aug 2017, 10:40, edited 2 times in total.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Edit Numbers With . [AHK_L 1.1]

23 Aug 2017, 09:47

Very nice :thumbup:
There was a similar question asked here: https://autohotkey.com/boards/viewtopic.php?f=5&t=31251
The function there takes a different approach and accepts negative input as well.
User avatar
maestrith
Posts: 825
Joined: 16 Oct 2013, 13:52

Re: Edit Numbers With . [AHK_L 1.1]

23 Aug 2017, 09:50

wolf_II wrote:Very nice :thumbup:
There was a similar question asked here: https://autohotkey.com/boards/viewtopic.php?f=5&t=31251
The function there takes a different approach and accepts negative input as well.
OH yea, numbers can be negative....duh Updated
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
Guest

Re: Edit Numbers With . [AHK_L 1.1]

23 Aug 2017, 16:41

Perhaps also of interest:

Validate characters as you type in an edit control by jsherk:
https://autohotkey.com/board/topic/6818 ... t-control/
User avatar
maestrith
Posts: 825
Joined: 16 Oct 2013, 13:52

Re: Edit Numbers With . [AHK_L 1.1]

24 Aug 2017, 08:37

Guest wrote:Useful, thanks :)
No Problem.
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: Edit Numbers With . [AHK_L 1.1]

24 Aug 2017, 10:04

It might be useful to replace the parameter 'Negative' with "Allow" or "Disallow" to let the caller decide which characters to accept and reject. It could be a list to be parsed or a regex expression. This way it would be easy to allow, say "$" for currency. It would need a new class name but it would be general purpose.

Relayer

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 235 guests