Page 1 of 1

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

Posted: 24 May 2019, 15:35
by Sabestian Caine
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 3991 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...

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

Posted: 24 May 2019, 16:57
by Albireo
I think you have to handle this on your own.
Sends some links in the same topic.

Only allow Alphanumeric entry in an Edit field
(maybe some tip)
If var is [not] type (from AHK-manual)
Validate characters as you type in an Edit control (maybe the best link?)

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

Posted: 25 May 2019, 05:02
by Odlanir

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

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

Posted: 25 May 2019, 13:41
by Sabestian Caine
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 3905 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..

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

Posted: 25 May 2019, 15:24
by swagfag

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")
}

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

Posted: 26 May 2019, 03:29
by Odlanir

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

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

Posted: 26 May 2019, 04:06
by just me
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.

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

Posted: 26 May 2019, 05:51
by Odlanir
@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	
   }
}

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

Posted: 26 May 2019, 06:47
by just me
@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.

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

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

Code: Select all

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

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

Posted: 26 May 2019, 10:28
by Sabestian Caine
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...

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

Posted: 26 May 2019, 10:30
by Sabestian Caine
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......