Module StringGadget() filter mask

Share your advanced PureBasic knowledge/code with the community.
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 796
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Module StringGadget() filter mask

Post by Zebuddi123 »

Hi to all Here is a small module to add a masked filter to a StringGadget() very simple to use.

digit mask defined by _ ie ____.__ 1234.12 delimiters can be added in the first line of Mask_StringGadget(Mask$)

Simple Implementation:

Code: Select all

 Case #PB_Event_Gadget
                  Select EventGadget()
                     Case 1
                        Mask_StringGadget(usemask$)  ; Where usemask$ = "____.__/____"   ie: 1234.23/2345
originally Masked_Input - by TerryHough - May 26, 2004, modified by infratec 2012

useful Masks :

PHONE.s = "___-___-____"
MMDDYYYY.s = "__/__/____"
MMYYYY.s = "__/____"
DDMMMYYYY.S = "__ ___, ____"
SSN.s = "___-__-____"
EIN.s = "__-_______"
TIME.s = "__:__:__"
ZIPCODE.s = "_____-____"
CURRENCY.s = "__________.__"

Hope some find it useful!

Zebuddi. :)

Code: Select all

DeclareModule gadgetfilter
	Declare Mask_StringGadget(mask$)
EndDeclareModule

Module gadgetfilter
	Procedure FilterIt(gHandle,gID, Filter$, Mask$)
		SendMessage_(gHandle, #EM_GETSEL, @cPos, 0)
		;ExamineKeyboard()
		;If KeyboardPushed(#PB_Key_Delete) ; Delete key pressed
		If GetAsyncKeyState_(#VK_DELETE)
			Debug "Delete"
			cEnt = SendMessage_(gHandle, #EM_GETLIMITTEXT, 0, 0)
			SendMessage_(gHandle, #EM_SETLIMITTEXT, cEnt - 1, 0)
			SendMessage_(gHandle, #EM_SETSEL, cPos, cPos)
			ProcedureReturn
		EndIf 
		validate_it$ = RTrim(GetGadgetText(gID))
		; Validate the last character entered in the StringGadget
		;   Quickest method, BUT allows pasting of unacceptable characters
		If FindString(Filter$, Right(validate_it$, 1),1)
			; Character is accepted by the filter, now compare with mask
			If Len(Mask$)
				If Right(validate_it$, 1) = Mid(Mask$, Len(validate_it$), 1)
					; Character matched the mask placeholder, so accept it
				ElseIf Right(validate_it$, 1) = "." And FindString(Mask$, ".", 1)
					;        If Len(validate_it$) <> FindString(Mask$, ".", 1)
					; Align the decimal received with the decimal in the Mask$, used for Currency
					validate_it$ = RSet(validate_it$, FindString(Mask$, ".", 1), " ")
					SetGadgetText(gID, validate_it$)
					;          MessageRequester("Debug",validate_it$+Chr(10)+Mask$+Chr(10)+Str(Len(validate_it$))+Chr(10)+Str(FindString(Mask$,".",1)))
					;        EndIf
				ElseIf (Mid(validate_it$, Len(validate_it$), 1) <> Mid(Mask$, Len(validate_it$), 1)) And Mid(Mask$, Len(validate_it$), 1) <> "_"
					; Character doesn't match the Mask placeholder
					;   so insert the placeholder into the string
					validate_it$ = Left(validate_it$, Len(validate_it$) -1) + Mid(Mask$, Len(validate_it$), 1) + Right(validate_it$, 1)
					; Replace text in StringGadget
					SetGadgetText(gID, validate_it$)
				EndIf
			EndIf
		Else
			; (WinAPI) Alert on bad character received
			; Beep_(1000,3)   ; Too slow, kills keyboard entry
			; Perhaps use Sound system if alert is necessary and desired.
			;PlaySound(0)
			; Character not accepted by the filter, drop it
			SetGadgetText(gID, Left(validate_it$, Len(validate_it$)-1))
		EndIf
		; (WinAPI) Get the modified text field's length and reset the cursor
		cPos = SendMessage_(gHandle, #EM_LINELENGTH, 0, 0)
		SendMessage_(gHandle, #EM_SETSEL, cPos+1, cPos+1)
	EndProcedure
	
	Procedure Mask_StringGadget(Mask$)
		Protected Filter$ = "0123456789" + "." + "-"+"_"+"/"+"\"+"|"	; Verify input characters through a filter controlled by a mask
		If Len(Mask$)
			SendMessage_(GadgetID(EventGadget()) , #EM_SETLIMITTEXT, Len(Mask$), 0) ; Limit the number of characters accepted to the length of the mask
		EndIf
		FilterIt(GadgetID(EventGadget()), EventGadget(), Filter$, Mask$) ; Pass to Filter procedure the gadget handle, ID, the chosen Filter and Mask
	EndProcedure
EndModule	

;---------------------- Example Code: --------------------------
;---------------------------------------------------------------------------

EnableExplicit
 
UseModule gadgetfilter
		
		Define usemask$ = "____.__.___"
		
		If OpenWindow(0, 0, 0, 500, 80, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
			StringGadget(1, 10, 10, 210, 20, "", 0)
			ButtonGadget(2, 10, 40, 130, 20, "Show Gadget Content", 0)
			TextGadget(3, 230, 10, 200,20, "Mask$: " + usemask$)
			Repeat
				Select WaitWindowEvent()
					Case #PB_Event_Gadget
						Select EventGadget()
							Case 1
								Mask_StringGadget(usemask$)   
								
							Case 2
								MessageRequester("Gadget Content", GetGadgetText(1))
								
						EndSelect
					Case #PB_Event_CloseWindow
						End
				EndSelect
			ForEver
		EndIf
UnuseModule gadgetfilter
malleo, caput, bang. Ego, comprehendunt in tempore
startup
Enthusiast
Enthusiast
Posts: 105
Joined: Wed Feb 25, 2015 5:55 pm

Re: Module StringGadget() filter mask

Post by startup »

hi,

this is really nice. it would be great so, if one could see the mask in the edit field.
Post Reply