Page 1 of 1

Extended StringGadget Decimal-Value

Posted: Wed Nov 26, 2003 8:58 am
by TeddyLM
Hello there

For a few months i posted a request about an extended StringGadget :

;************
What's about adding some new features to the StringGadget ?
For example :
StringGadget(#Gadget, x, y, Width, Height, Content$ [, Length, Flags])
where Length = number of caracters allowed
and a new Flag : #PB_String_DecimalValue ("0123456789.," are accepted)
It would be really helpful not to have to control those things all the time."
;************

I since worked a while that way and i know it would be very difficult to implant such an extension. Thanks to Art Sentinel and his well-documented 'HookIncludeFile Tutorial' i wrote a small application that controls which characters to be send (or not) to a StringGadget.
In this example it accepts these characters : 0123456789 and , (feel free to change them) :


;============================
;StringGadget with KEY-FILTER
;Thanks to Art Sentinel for the Hook-Tutorial
;============================
Global hhook
Global CommaPoint.b : CommaPoint = 0
#StringID1 = 11
#StringID2 = 14
;************
;PROCEDURES
;************
Procedure KeyboardProc(nCode, wParam, lParam)
DefType.b Result
If wParam < 31
Result = 0
ElseIf wParam = 188 And CommaPoint = 0 ;","
Result = 0
ElseIf wParam = 48 ;0
Result = 0
ElseIf wParam = 49 ;1
Result = 0
ElseIf wParam = 50 ;2
Result = 0
ElseIf wParam = 51 ;3
Result = 0
ElseIf wParam = 52 ;4
Result = 0
ElseIf wParam = 53 ;5
Result = 0
ElseIf wParam = 54 ;6
Result = 0
ElseIf wParam = 55 ;7
result = 0
ElseIf wParam = 56 ;8
Result = 0
ElseIf wParam = 57 ;9
Result = 0
Else
Result = 1
EndIf
ProcedureReturn Result
EndProcedure
;***********
;***********
Procedure HookProc(Hooked)
Shared hhook
Shared CommaPoint
Select Hooked
Case 1
hInstance = GetModuleHandle_(0)
lpdwProcessId = GetWindowThreadProcessId_(WindowID, 0)
hhook = SetWindowsHookEx_(#WH_KEYBOARD, @KeyboardProc(), hInstance, lpdwProcessId)
Case 0
UnhookWindowsHookEx_(hhook)
EndSelect
EndProcedure
;************
;************
If OpenWindow(0, 150, 150, 300, 300, #PB_Window_SystemMenu, "Decimal-Filter (Thanks to Art Sentinel)")
;GadgetList
If CreateGadgetList(WindowID())
TextGadget(10, 50, 96, 180, 22, "No Filter :")
StringGadget(#StringID1, 50, 120, 180, 22, "") : SendMessage_(GadgetID(#StringID1), #EM_LIMITTEXT, 20, 0)
TextGadget(12, 50, 166, 180, 22, "Filter Decimal-Value :")
StringGadget(#StringID2, 50, 190, 180, 22, "") : SendMessage_(GadgetID(#StringID2), #EM_LIMITTEXT, 20, 0)
ActivateGadget(#StringID1)

;Main loop
Repeat
Select WaitWindowEvent()
Case #WM_KEYDOWN
If EventwParam() = 13
Select EventGadgetID()
Case #StringID1
text$ = GetGadgetText(#StringID2)
SendMessage_(GadgetID(#StringID2), #EM_SETSEL, 0, Len(text$))
ActivateGadget(#StringID2)
Case #StringID2
text$ = GetGadgetText(#StringID1)
SendMessage_(GadgetID(#StringID1), #EM_SETSEL, 0, Len(text$))
ActivateGadget(#StringID1)
EndSelect
EndIf
Case #PB_EventGadget
HookProc(0)
Select EventGadgetID()
Case #StringID2
Text$=GetGadgetText(#StringID2)
StLength = Len(Text$)
CommaPoint = 0
If StLength > 0
For counter = 1 To StLength
If Mid(Text$,counter,1) = ","
CommaPoint = CommaPoint + 1
EndIf
Next
EndIf
HookProc(1)
EndSelect
Case #WM_CLOSE
Quit = 1
EndSelect
Until Quit = 1
EndIf
EndIf
End
;============================



;May the force be with you !

Re: Extended StringGadget Decimal-Value

Posted: Wed Nov 26, 2003 9:21 am
by PB
> In this example it accepts these characters : 0123456789 and ,

This example isn't foolproof -- you can still right-click the StringGadget
and paste non-digits into it. The best approach is to check when its
contents have been changed, then strip out any non-digits and store
the updated "fixed" value back into it.

Posted: Wed Nov 26, 2003 9:44 am
by TeddyLM
Hi PB

Damn, you're right !
mmmh ... i should think about a new approach.


thanks 2 U

Posted: Wed Nov 26, 2003 10:07 am
by TeddyLM
>you can still right-click the StringGadget and paste non-digits into it

I noticed the same problem when using the StringGadget and the #PB_String_Numeric-Flag.

Posted: Wed Nov 26, 2003 1:41 pm
by TeddyLM
What's about this :
(no more Copy-Paste allowed inside the StringGadget)



;============================
;StringGadget with KEY-FILTER
;Thanks to Art Sentinel for the Hook-Tutorial
;============================
Global hhook
Global hookValue.b : hookValue = 0
Global CommaPoint.b : CommaPoint = 0
#StringID1 = 11
#StringID2 = 14
;************
;PROCEDURES
;************
Procedure KeyboardProc(nCode, wParam, lParam)
DefType.b Result
If wParam < 31
Result = 0
ElseIf wParam = 188 And CommaPoint = 0 ;","
Result = 0
ElseIf wParam = 8 ;Del
Result = 0
ElseIf wParam = 37 ;ArrowLeft
Result = 0
ElseIf wParam = 38 ;ArrowUp
Result = 0
ElseIf wParam = 39 ;ArrowRight
Result = 0
ElseIf wParam = 40 ;ArrowDown
Result = 0
ElseIf wParam = 46 ;Entf
Result = 0
ElseIf wParam = 48 ;0
Result = 0
ElseIf wParam = 49 ;1
Result = 0
ElseIf wParam = 50 ;2
Result = 0
ElseIf wParam = 51 ;3
Result = 0
ElseIf wParam = 52 ;4
Result = 0
ElseIf wParam = 53 ;5
Result = 0
ElseIf wParam = 54 ;6
Result = 0
ElseIf wParam = 55 ;7
result = 0
ElseIf wParam = 56 ;8
Result = 0
ElseIf wParam = 57 ;9
Result = 0
Else
Result = 1
EndIf
ProcedureReturn Result
EndProcedure
;***********
;***********
Procedure HookProc(Hooked)
Shared hhook
Shared CommaPoint
Select Hooked
Case 1
hInstance = GetModuleHandle_(0)
lpdwProcessId = GetWindowThreadProcessId_(WindowID, 0)
hhook = SetWindowsHookEx_(#WH_KEYBOARD, @KeyboardProc(), hInstance, lpdwProcessId)
Case 0
UnhookWindowsHookEx_(hhook)
EndSelect
EndProcedure
;************
;************
WindowID = OpenWindow(0, 150, 150, 300, 300, #PB_Window_SystemMenu, "Decimal-Filter (Thanks to Art Sentinel)")
If WindowID <> 0
;GadgetList
If CreateGadgetList(WindowID())
TextGadget(10, 50, 96, 180, 22, "No Filter :")
StringGadget(#StringID1, 50, 120, 180, 22, "") : SendMessage_(GadgetID(#StringID1), #EM_LIMITTEXT, 20, 0)
TextGadget(12, 50, 166, 180, 22, "Filter Decimal-Value :")
StringGadget(#StringID2, 50, 190, 180, 22, "") : SendMessage_(GadgetID(#StringID2), #EM_LIMITTEXT, 20, 0)
ActivateGadget(#StringID1)

;Main loop
Repeat
Select WaitWindowEvent()
Case #WM_KEYDOWN
If EventwParam() = 13
Select EventGadgetID()
Case #StringID1
text$ = GetGadgetText(#StringID2)
SendMessage_(GadgetID(#StringID2), #EM_SETSEL, 0, Len(text$))
ActivateGadget(#StringID2)
Case #StringID2
text$ = GetGadgetText(#StringID1)
SendMessage_(GadgetID(#StringID1), #EM_SETSEL, 0, Len(text$))
ActivateGadget(#StringID1)
EndSelect
EndIf
Case #PB_EventGadget
Select EventGadgetID()
Case #StringID2
Text$=GetGadgetText(#StringID2)
StLength = Len(Text$)
CommaPoint = 0
If StLength > 0
For counter = 1 To StLength
If Mid(Text$,counter,1) = ","
CommaPoint = CommaPoint + 1
EndIf
Next
EndIf
If hookValue = 0 : hookValue = 1 : HookProc(1) : EndIf
Default
If hookValue = 1 : hookValue = 0 : HookProc(0) : EndIf
EndSelect
Case #WM_CLOSE
Quit = 1
EndSelect
Until Quit = 1
EndIf
EndIf
End
;============================

Posted: Thu Jul 23, 2009 4:13 pm
by Donald
Hi,

Its works under WIN XP

No Works under WIN VISTA
Who can Help ?

Donald

..

Posted: Thu Jul 23, 2009 4:38 pm
by netmaestro
You can try this, I just modded it quickly so no guarantees:

Code: Select all

;============================ 
;StringGadget with KEY-FILTER 
;Thanks to Art Sentinel for the Hook-Tutorial 
;============================ 
Global hhook 
Global hookValue.b : hookValue = 0 
Global CommaPoint.b : CommaPoint = 0 
#StringID1 = 11 
#StringID2 = 14 
;************ 
;PROCEDURES 
;************ 
Procedure KeyboardProc(nCode, wParam, lParam) 
  result.b = 1
  Select wParam
    Case 0 To 30, 37 To 40, 46, 48 To 57 : result=0
    Case 188 : If CommaPoint = 0: result = 0 : EndIf
  EndSelect
  
  ProcedureReturn Result 
EndProcedure 
;*********** 
;*********** 
Procedure HookProc(Hooked) 
  Shared hhook 
  Shared CommaPoint 
  Select Hooked 
    Case 1 
      hhook = SetWindowsHookEx_(#WH_KEYBOARD, @KeyboardProc(), 0, GetCurrentThreadId_()) 
    Case 0 
      UnhookWindowsHookEx_(hhook) 
  EndSelect 
EndProcedure 
;************ 
;************ 
WindowID = OpenWindow(0, 150, 150, 300, 300, "Decimal-Filter (Thanks to Art Sentinel)",#PB_Window_SystemMenu) 

TextGadget(10, 50, 96, 180, 22, "No Filter :") 
StringGadget(#StringID1, 50, 120, 180, 22, "") : SendMessage_(GadgetID(#StringID1), #EM_LIMITTEXT, 20, 0) 
TextGadget(12, 50, 166, 180, 22, "Filter Decimal-Value :") 
StringGadget(#StringID2, 50, 190, 180, 22, "") : SendMessage_(GadgetID(#StringID2), #EM_LIMITTEXT, 20, 0) 
setActiveGadget(#StringID1) 

;Main loop 
Repeat 
  Select WaitWindowEvent() 
    Case #WM_KEYDOWN 
      If EventwParam() = 13 
        Select EventGadget() 
          Case #StringID1 
            text$ = GetGadgetText(#StringID2) 
            SendMessage_(GadgetID(#StringID2), #EM_SETSEL, 0, Len(text$)) 
            setactiveGadget(#StringID2) 
          Case #StringID2 
            text$ = GetGadgetText(#StringID1) 
            SendMessage_(GadgetID(#StringID1), #EM_SETSEL, 0, Len(text$)) 
            setactiveGadget(#StringID1) 
        EndSelect 
      EndIf 
    Case #PB_Event_Gadget 
      Select EventGadget() 
        Case #StringID2 
          Select EventType()
            Case #PB_EventType_Focus 
              HookProc(1) 
            Case #PB_EventType_LostFocus 
              HookProc(0)
            Default
              Text$=GetGadgetText(#StringID2) 
              StLength = Len(Text$) 
              CommaPoint = 0 
              If StLength > 0 
                For counter = 1 To StLength 
                  If Mid(Text$,counter,1) = "," 
                    CommaPoint = CommaPoint + 1 
                  EndIf 
                Next 
              EndIf 
        EndSelect
      EndSelect 
    Case #PB_Event_CloseWindow
      Quit = 1 
  EndSelect 
Until Quit = 1 

End 
;============================

Posted: Thu Jul 23, 2009 4:50 pm
by cas
Now we have this in purebasic:

Code: Select all

If OpenWindow(0, 0, 0, 322, 105, "StringGadget Flags", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(0, 8,  10, 306, 20, "Normal StringGadget...")
  StringGadget(1, 8,  35, 306, 20, "1234567", #PB_String_Numeric)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
So no need for using this old code... :P

Posted: Thu Jul 23, 2009 4:54 pm
by netmaestro
#PB_String_Numeric lacks flexibility. Using the method above you can tune the input any way you want it.

Posted: Thu Jul 23, 2009 5:02 pm
by cas
I knew it was something more :P because everyone knows for #PB_String_Numeric today 8)

True, you can do far more different things with this code, like filtering only certain characters while #PB_String_Numeric filters all characters.

Posted: Fri Jul 24, 2009 5:10 pm
by Donald
Hello,

thanks, its works.

Donald


..

old

Code: Select all

Procedure HookProc(Hooked)
Shared hhook
Shared CommaPoint
Select Hooked
Case 1
hInstance = GetModuleHandle_(0)
lpdwProcessId = GetWindowThreadProcessId_(WindowID, 0)
hhook = SetWindowsHookEx_(#WH_KEYBOARD, @KeyboardProc(), hInstance, lpdwProcessId)
Case 0
UnhookWindowsHookEx_(hhook)
EndSelect
EndProcedure
new

Code: Select all

Procedure HookProc(Hooked) 
  Shared hhook 
  Shared CommaPoint 
  Select Hooked 
    Case 1 
      hhook = SetWindowsHookEx_(#WH_KEYBOARD, @KeyboardProc(), 0, GetCurrentThreadId_()) 
    Case 0 
      UnhookWindowsHookEx_(hhook) 
  EndSelect 
EndProcedure 

Posted: Fri Jul 24, 2009 8:56 pm
by srod
Doesn't allow the decimal point here which is coming through on my system as virtual-key code 190 - not Ascii code 46. (Vista.)

Posted: Fri Jul 24, 2009 9:03 pm
by rsts
picky, picky, picky

:D

cheers

Posted: Fri Jul 24, 2009 9:16 pm
by netmaestro
I think it uses the comma for a decimal point, I noticed that when I was testing.
picky, picky, picky
Yes, agreed. I wouldn't want to be one of his students trying for an A... :roll:

Posted: Fri Jul 24, 2009 9:36 pm
by srod
No not picky really - I just didn't really understand why the decimal point was being ignored and why the virtual key code generated is one of the OEM specific ones? I see it now that I take a closer look at the code! :)

Guess I am wondering why a hook is being used rather than simply subclassing the edit control? Oh well!