How to restrict the input by user to Positive numbers, Zero and Negative numbers into edit box in a GUI?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Sabestian Caine
Posts: 420
Joined: 12 Apr 2015, 03:53

How to restrict the input by user to Positive numbers, Zero and Negative numbers into edit box in a GUI?

24 May 2019, 15:35

Hello friends...


Please look at these codes-

Code: Select all

Gui Add, Edit, x183 y57 w205 h51 number,
Gui Add, Edit, x184 y128 w205 h51 number,
Gui Add, Edit, x185 y199 w205 h51 number,

Gui Show, w547 h338, Window
Return
in the above codes you can see that i have added number keyword in each edit box which means that user can input only numbers in the edit fields. if anyone tries to enter anything else other than number then it shows error like this-
25_05_19 @1_56_32.PNG
25_05_19 @1_56_32.PNG (30.69 KiB) Viewed 815 times
But, here the problem is that when i try to insert any negative value (like -100) in the edit box then it shows the same error also, while I want that it should accept negative numbers also. If anything else other than positive numbers, zero and negative numbers is inserted into edit boxes then it should show the above error.

Please fix this problem and provide any solution...

Thanks a lot...
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
Odlanir
Posts: 514
Joined: 20 Oct 2016, 08:20

Re: How to restrict the input by user to Positive numbers, Zero and Negative numbers into edit box in a GUI?

25 May 2019, 05:02

Code: Select all

Gui, Add, Edit, vEdit w200
Gui, Show

OnMessage(0x102, "WM_CHAR")
return

WM_CHAR(wParam, lParam){	
	If(A_GuiControl = "Edit" and !RegExMatch(Chr(wParam), "^[-0-9\x08]$")) ; x08 is backspace
		Return false
}
esc::
    ExitApp
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
Sabestian Caine
Posts: 420
Joined: 12 Apr 2015, 03:53

Re: How to restrict the input by user to Positive numbers, Zero and Negative numbers into edit box in a GUI?

25 May 2019, 13:41

Odlanir wrote:
25 May 2019, 05:02

Code: Select all

Gui, Add, Edit, vEdit w200
Gui, Show

OnMessage(0x102, "WM_CHAR")
return

WM_CHAR(wParam, lParam){	
	If(A_GuiControl = "Edit" and !RegExMatch(Chr(wParam), "^[-0-9\x08]$")) ; x08 is backspace
		Return false
}
esc::
    ExitApp

Excellent dear Odlanir.... This is what i was wanting ...

Dear odlanir, I want one more thing- when a user enters any value except negative and positive numbers in edit field then it should show error in such way-

25_05_19 @1_56_32.PNG
25_05_19 @1_56_32.PNG (30.69 KiB) Viewed 729 times


I add one line of code to show error msgbox when any user enters any other value except negative and positive values, like this-

Code: Select all

Gui, Add, Edit, vEdit w200
Gui, Show

OnMessage(0x102, "WM_CHAR")
return

WM_CHAR(wParam, lParam){	
	If(A_GuiControl = "Edit" and !RegExMatch(Chr(wParam), "^[-0-9\x08]$")) ; x08 is backspace
	{
		
		MsgBox, 16,,Unacceptable Character ; this shows msgbox when anyone enters any other value
		Return false
	}
	}
In the above codes you can see that i added one line of code which shows error msgbox but i want the error message should be shown as demonstrated in the above image which looks more professional. Is it possible? Please help and guide.. Thanks a lot..
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
swagfag
Posts: 2905
Joined: 11 Jan 2017, 17:59

Re: How to restrict the input by user to Positive numbers, Zero and Negative numbers into edit box in a GUI?

25 May 2019, 15:24

Code: Select all

#Persistent
Gui Add, Edit, HwndhEdit
Gui Show

Edit_ShowBalloonTip(hEdit, "You can only type a number here.", "Unnacceptable Character", "ERROR")
Return

GuiClose:
GuiEscape:
	ExitApp

Edit_ShowBalloonTip(hwnd, pszText, pszTitle := "", ttiIcon := "NONE") {
	static TTI := { NONE:          0
				  , INFO:          1
				  , WARNING:       2
				  , ERROR:         3
				  , INFO_LARGE:    4
				  , WARNING_LARGE: 5
				  , ERROR_LARGE:   6 }

	if !TTI.HasKey(ttiIcon)
	{
		error := A_ThisFunc "() - Invalid argument ""ttiIcon"".`nAccepted values are:`n"
		for icon, value in TTI
			error .= """" icon """`n"

		throw Exception(error, -2)
	}

	static IS_AHK_64 := A_PtrSize == 8
		, SIZE_EBT := IS_AHK_64 ? 32 : 16

	VarSetCapacity(EBT, SIZE_EBT, 0)
	NumPut(SIZE_EBT, EBT, 0, "UInt")
	NumPut(&pszTitle, EBT, IS_AHK_64 ? 8 : 4, "Ptr")
	NumPut(&pszText, EBT, IS_AHK_64 ? 16 : 8, "Ptr")
	NumPut(TTI[ttiIcon], EBT, IS_AHK_64 ? 24 : 12, "Int")

	static EM_SHOWBALLOONTIP := 0x1503
	DllCall("SendMessage", "Ptr", hwnd, "UInt", EM_SHOWBALLOONTIP, "UPtr", 0, "Ptr", &EBT, "Int")
}
Odlanir
Posts: 514
Joined: 20 Oct 2016, 08:20

Re: How to restrict the input by user to Positive numbers, Zero and Negative numbers into edit box in a GUI?

26 May 2019, 03:29

Code: Select all

OnMessage(0x102, "WM_CHAR")
Gui, Add, Edit, vEdit hwndhEdi  w200
Gui, Show
return


WM_CHAR(wParam, lParam){	
   global hEdi
   If(A_GuiControl = "Edit" and !RegExMatch(Chr(wParam), "^[-0-9\x08]$"))  { ; x08 is backspace
      EM_SHOWBALLOONTIP(hEdi, "Input error!", "Only digits are valid!", 3)
      Return false	
   }
}

EM_SHOWBALLOONTIP(HWND, Title, Text, Icon := 0) {
   ; EM_SHOWBALLOONTIP = 0x1503 -> http://msdn.microsoft.com/en-us/library/bb761668(v=vs.85).aspx
   Static Icons := {0: 0, 1: 1, 2: 2, 3: 3, NONE: 0, INFO: 1, WARNING: 2, ERROR: 3}
   NumPut(VarSetCapacity(EBT, 4 * A_PtrSize, 0), EBT, 0, "UInt")
   If !(A_IsUnicode) {
      VarSetCapacity(WTitle, StrLen(Title) * 4, 0)
      VarSetCapacity(WText, StrLen(Text) * 4, 0)
      StrPut(Title, &WTitle, "UTF-16")
      StrPut(Text, &WText, "UTF-16")
   }
   If !Icons.HasKey(Icon)
      Icon := 0
   NumPut(A_IsUnicode ? &Title : &WTitle, EBT, A_PtrSize, "Ptr")
   NumPut(A_IsUnicode ? &Text : &WText, EBT, A_PtrSize * 2, "Ptr")
   NumPut(Icons[Icon], EBT, A_PtrSize * 3, "UInt")
   Return DllCall("SendMessage", "Ptr", HWND, "UInt", 0x1503, "Ptr", 0, "Ptr", &EBT, "Ptr")
}
esc::
    ExitApp
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
just me
Posts: 6484
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to restrict the input by user to Positive numbers, Zero and Negative numbers into edit box in a GUI?

26 May 2019, 04:06

Using only WM_CHAR() only checking the contents of wParam will actually not work. It allows to input any amount of minus signs anywhere in the edit control.
Odlanir
Posts: 514
Joined: 20 Oct 2016, 08:20

Re: How to restrict the input by user to Positive numbers, Zero and Negative numbers into edit box in a GUI?

26 May 2019, 05:51

@just me You're right! This should work:

Code: Select all

WM_CHAR(wParam, lParam){	
   global hEdi, Edit
   Gui, Submit, NoHide
   if ( RegExMatch(Edit, "^-") && Chr(wparam) = "-" ) {
      EM_SHOWBALLOONTIP(hEdi, "Input error!", "Minus sign valid only at the beginning", 3)
      Return false	
   }
   If(A_GuiControl = "Edit" and !RegExMatch(Chr(wParam), "^[-0-9\x08]$"))  { ; x08 is backspace
      EM_SHOWBALLOONTIP(hEdi, "Input error!", "Only digits are valid!", 3)
      Return false	
   }
}
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
just me
Posts: 6484
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to restrict the input by user to Positive numbers, Zero and Negative numbers into edit box in a GUI?

26 May 2019, 06:47

@Odlanir: Still not enough. ;) I'm still able to place the minus sign everywhere in the edit as long as it is not already placed at the beginning.
Odlanir
Posts: 514
Joined: 20 Oct 2016, 08:20

Re: How to restrict the input by user to Positive numbers, Zero and Negative numbers into edit box in a GUI?

26 May 2019, 06:57

Maybe the first RegexMatch should be:

Code: Select all

if ( RegExMatch(Edit, "^-|^\d") && Chr(wparam) = "-" )
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
Sabestian Caine
Posts: 420
Joined: 12 Apr 2015, 03:53

Re: How to restrict the input by user to Positive numbers, Zero and Negative numbers into edit box in a GUI?

26 May 2019, 10:28

Odlanir wrote:
26 May 2019, 06:57
Maybe the first RegexMatch should be:

Code: Select all

if ( RegExMatch(Edit, "^-|^\d") && Chr(wparam) = "-" )

Thanks a lot dear Odlanir.... Now it is working fine...
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
User avatar
Sabestian Caine
Posts: 420
Joined: 12 Apr 2015, 03:53

Re: How to restrict the input by user to Positive numbers, Zero and Negative numbers into edit box in a GUI?

26 May 2019, 10:30

just me wrote:
26 May 2019, 06:47
@Odlanir: Still not enough. ;) I'm still able to place the minus sign everywhere in the edit as long as it is not already placed at the beginning.
Thanks dear just me......
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], teadrinker and 231 guests