New topic: 

numeric only textfield.

<http://forums.realsoftware.com/viewtopic.php?t=38384>

         Page 1 of 1
   [ 2 posts ]                 Previous topic | Next topic          Author  
Message        BrianOBrien          Post subject: numeric only 
textfield.Posted: Tue Mar 29, 2011 2:50 pm                                 
Joined: Wed Jul 07, 2010 2:55 pm
Posts: 253
Location: University Of Calgary, Faculty Of Medicine                I have a 
textfield that can contain only numbers...
I am trying to handle this by intercepting the keys that are passed in the 
keydown event...
However things get complicated when the user is trying to edit the field with 
backspace, delete, cursor left and right...
The editing characters don't seem to be working the way I thought they might..
Code:Function KeyDown(Key As String) As Boolean
  dim srcstr as string = "0123456789" + chr(8) + chr(127) + chr(28) + chr(29) 
'backspace, delete, left, right
  if instr(srcstr, key) = 0 then
  'msgbox("key = " + str(asc(key)))
  beep
  return true
  end if
  return false
End FunctionThe editing keys no longer do anything.      
_________________
If at first you don't succeed... Try REALBasic.  
                             Top                 markwalsh          Post 
subject: Re: numeric only textfield.Posted: Tue Mar 29, 2011 2:55 pm            
             
Joined: Wed May 20, 2009 4:43 pm
Posts: 415                This is what I have used in the past in the keydown 
event:
Code:  Dim keyCode as Integer
  Dim x as boolean
  
  keyCode = Asc(Key)
  if keyCode = 13 or keyCode = 3 or keyCode = 9 or keyCode = 7 or keyCode = 8 
or keyCode = 27 then
  return False
  end if
  
  ' Delete keys
  if keyCode = 127 or keyCode = 8  then
  return False
  end if
  
  ' Arrow keys
  if keyCode = 28 or keyCode = 29 or keyCode = 30 or keyCode = 31 then
  return False
  end if
  
  ' Allow only digits entered
  Dim oRegExp as RegEx
  Dim oRegExMatch as RegExMatch
  oRegExp = new RegEx
  oRegExp.SearchPattern = "^[0-9]*$"
  oRegExMatch = oRegExp.Search (Key )
  if oRegExMatch <> Nil then
  return False
  else
  return True
  end if      
_________________
RB 2009r4 Windows XP  
                             Top            Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 2 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to