NumberFields in Maxgui? Need some help
BlitzMax Forums/BlitzMax Beginners Area/NumberFields in Maxgui? Need some help
| ||
| Hi, I'm trying to get started on a simple MAxGUI app with the demo. (but I own BlitzMax however). When my app launches the user is prompted to enter names, I did that with the CreateTextField() function. Now I'd like the user to enter some numeric values in other fields, and retrieve them as integers. Obviously I can't do that with CreateTextField(). Is there a equivalent to it which would only accept numbers? (like a CreateNumtField() thingy ?). I can't find info about this in the doc. Thanks a lot ! Nino |
| ||
| Simple, the textfield accepts *everything*.. and at each textfield event (you typed something) you get the contents of the textfield, get rid of non-numbers yourself, and put the filtered text back. Another solution is to make a special new gadget yourself which looks like an editfield, but only accepts numbers.. |
| ||
| Thanks CS_TBL, But I guess I'm too much of a beginner to do that. Hints would be welcome. Thanks a lot :) |
| ||
SuperStrict
Local window:TGadget=CreateWindow("o_O",0,0,640,480)
Local tf:TGadget=CreateTextField(32,32,64,20,window,2)
Local a:String
Local a2:String
Local a3:String
Local t:Int
Repeat
WaitEvent()
If EventID()=EVENT_WINDOWCLOSE End
If EventID()=EVENT_GADGETACTION
If EventSource()=tf
a=TextFieldText(tf)
a2=""
For t=0 Until Len(a)
a3=Mid(a,t+1,1)
If Instr("0123456789",a3) ' number found?
a2:+a3
EndIf
Next
SetGadgetText tf,a2
EndIf
EndIf
Forever
I leave it up to you to make a decent function out of it. :P |
| ||
| Thanks, I'll dig that in the morning ! |
| ||
oh heck, what the f*ck .. here ya go. A hookie for a rookie :P
SuperStrict
' ===========================================================================
' ===========================================================================
' ===========================================================================
Type TNumfield
Field tf:TGadget
Field NewEvent:TEvent=New TEvent
Function eventhook:Object(id:Int,data:Object,context:Object)
If TNumfield(context) TNumfield(context).ev TEvent(data);Return data
EndFunction
Method New()
AddHook EmitEventHook,eventhook,Self
End Method
Method Free()
RemoveHook EmitEventHook,eventhook
FreeGadget tf
GCCollect()
End Method
Method ev(event:TEvent)
Local t:Int
Local a:String
Local a2:String
Local a3:String
If Event.id=EVENT_GADGETACTION
If Event.source=tf
a=TextFieldText(tf)
a2=""
For t=0 Until Len(a)
a3=Mid(a,t+1,1)
If Instr("0123456789",a3) ' number found?
a2:+a3
EndIf
Next
SetGadgetText tf,a2
NewEvent.ID=EVENT_GADGETACTION
NewEvent.source=Self
EmitEvent NewEvent
EndIf
EndIf
End Method
Method Value$()
Return TextFieldText(tf)
End Method
End Type
Function CreateNumfield:TNumfield(x:Int,y:Int,w:Int,h:Int,parent:TGadget,style:Int=0)
Local a:TNumfield=New TNumfield
a.tf=CreateTextField(x,y,w,h,parent,style)
Return a
End Function
' ===========================================================================
' ===========================================================================
' ===========================================================================
Local window:TGadget=CreateWindow("o_O",0,0,640,480)
Local tf:TNumfield=CreateNumField(32,32,64,20,window,2)
Repeat
WaitEvent()
If EventID()=EVENT_WINDOWCLOSE And EventSource()=window
tf.Free
End
EndIf
If EventSource()=tf
DebugLog tf.Value()
EndIf
Forever
|
| ||
| Hi, Have a look here also, Eikon and Pert came up with some good ways to filter out non numeric data in a text field. http://www.blitzbasic.com/Community/posts.php?topic=55626 |