Extended StringGadget Decimal-Value

Share your advanced PureBasic knowledge/code with the community.
TeddyLM
Enthusiast
Enthusiast
Posts: 133
Joined: Wed Apr 30, 2003 2:04 pm
Location: Germany (French expat)

Extended StringGadget Decimal-Value

Post 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 !
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Extended StringGadget Decimal-Value

Post 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.
TeddyLM
Enthusiast
Enthusiast
Posts: 133
Joined: Wed Apr 30, 2003 2:04 pm
Location: Germany (French expat)

Post by TeddyLM »

Hi PB

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


thanks 2 U
TeddyLM
Enthusiast
Enthusiast
Posts: 133
Joined: Wed Apr 30, 2003 2:04 pm
Location: Germany (French expat)

Post 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.
TeddyLM
Enthusiast
Enthusiast
Posts: 133
Joined: Wed Apr 30, 2003 2:04 pm
Location: Germany (French expat)

Post 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
;============================
Donald
User
User
Posts: 15
Joined: Fri Jan 07, 2005 1:29 am
Location: Germany

Post by Donald »

Hi,

Its works under WIN XP

No Works under WIN VISTA
Who can Help ?

Donald

..
DONALD :D http://www.PureBasic-Donald.de
PureBasic - jaPBe - PureFORM - PureVisonXP - TailBite
GFABasic 16Bit 4.38 - GB32 2.30 B1160 - GFABasic DOS 4.51
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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 
;============================
BERESHEIT
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Post 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
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

#PB_String_Numeric lacks flexibility. Using the method above you can tune the input any way you want it.
BERESHEIT
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Post 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.
Donald
User
User
Posts: 15
Joined: Fri Jan 07, 2005 1:29 am
Location: Germany

Post 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 
DONALD :D http://www.PureBasic-Donald.de
PureBasic - jaPBe - PureFORM - PureVisonXP - TailBite
GFABasic 16Bit 4.38 - GB32 2.30 B1160 - GFABasic DOS 4.51
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.)
I may look like a mule, but I'm not a complete ass.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

picky, picky, picky

:D

cheers
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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:
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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!
I may look like a mule, but I'm not a complete ass.
Post Reply