Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Questions & Help Needed
Home Register FAQ Members List Search Today's Posts Mark Forums Read

Questions & Help Needed Post any question regarding Basic4ppc.


TextBox Input Filter?


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-09-2007, 07:35 PM
Junior Member
 
Join Date: Apr 2007
Location: Olympia, Washington USA
Posts: 24
Default TextBox Input Filter?

Has anyone figured out a way to filter textbox input? For example, I wish to have the textbox ignore any key press that is not numeric. I tried the IsNumber function in the KeyPress event but that doesn't work.

The first approach I tried was:

Sub TextBox1_KeyPress (key)
if IsNumeric(key) = False then key = ""
End Sub

But changing the variable "key" within that subroutine does not do anything to what is displayed in the textbox. To illustrate this, replace the "if" statement above with "key = R". The textbox will still display what you type, not the value "R".

My second approach was to chop the right-hand character off of TextBox1.Text but alas, TextBox1.Text is not updated until AFTER the TextBox1.KeyPress(key) subroutine has been completed.

The only other events for a textbox are GotFocus and LostFocus, neither of which helps for this problem. The KeyPress event allows me to detect which key was pressed, but does not let me do anything about it.

Any ideas?

-dlfallen
Reply With Quote
  #2 (permalink)  
Old 05-09-2007, 07:51 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,134
Default

You should use IgnoreKey method:
If IsNumeric(key) = false then TextBox1.IgnoreKey
Reply With Quote
  #3 (permalink)  
Old 05-10-2007, 12:39 AM
Junior Member
 
Join Date: Apr 2007
Location: Olympia, Washington USA
Posts: 24
Default

Thanks Erel. Your response was quick and to the point, as usual!

-dlfallen
Reply With Quote
  #4 (permalink)  
Old 10-09-2007, 04:09 AM
Senior Member
 
Join Date: May 2007
Posts: 130
Awards Showcase
Beta Tester 
Total Awards: 1
Default Code help anyone...

B4PPC community, Hi!

I'm coding a dynamic textbox input and need help (the last item).
Any simple or correct solution for these filters?

Code:
Sub HHStart_KeyPress (key) 

  'input only number
If IsNumber(key) = false AND Asc(key) <> 8  Then HHstart.IgnoreKey

  'mouse selected character and replace
If StrLength(HHStart.Text) <=2 AND HHstart.SelectionLength<>0 Then
s = HHStart.SelectionStart
HHstart.Text=StrRemove(HHstart.Text,s,HHstart.SelectionLength)
HHStart.SelectionStart=s
End If

  'limiting input length
If StrLength(HHstart.text)>=2 AND Asc(key) > 31 Then HHstart.IgnoreKey

  'limiting input number range
????????? from 0 to 24 only

End Sub
Thanks in advance
__________________
Rioven

Sony Ericsson XPERIA X1i WM6.1
480x800 Display Resolution
with QWERTY keyboard
Reply With Quote
  #5 (permalink)  
Old 10-09-2007, 10:34 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,690
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

Could be a bit more elegant, especially if you removed the need to replace a selected character, but it works.
Code:
Sub HHStart_KeyPress (key)

	'process only numbers, Asc("0")=48, AsSc("1")= 50 etc. and backspace = 8
	If (IsNumber(key) = false AND Asc(key) <> 8) Then
		HHstart.IgnoreKey
		Return
	End If

	If HHstart.SelectionLength <> 0 Then
		s = HHStart.SelectionStart
		If s = 0 AND Asc(key) > 50 AND  StrLength(HHStart.Text) >=2 Then 
			' only allow first digit to be in range
			HHstart.IgnoreKey
			Return
		End If
		HHstart.Text=StrRemove(HHstart.Text,s,HHstart.SelectionLength)
		HHStart.SelectionStart=s
	End If

             'limit input number range 
  	If StrLength(HHStart.Text) > 0 AND IsNumber(key) Then
		first = Asc(StrAt(HHStart.Text,0))
 		If first > 50 Then
			HHstart.IgnoreKey ' first digit more than "2"
		Else If	first = 50 AND Asc(Key) > 52 Then
			HHstart.IgnoreKey ' final number would be > 24
		Else If StrLength(HHStart.Text) >= 2
			HHstart.IgnoreKey ' final number would be more then two digits
		End If		
	End If
End Sub
Reply With Quote
  #6 (permalink)  
Old 10-09-2007, 11:51 AM
Senior Member
 
Join Date: May 2007
Posts: 130
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Hi agraham, thanks for your help, I could apply you techniques but still need to fix something...

when mouse is placed in between the 2 digits and use backspace, I could enter number that results more than 24, sometimes cannot enter any valid number.

or

When there is an existing 1 digit, and input number before this digit, I could enter number that results more than 24, sometimes cannot enter any valid number.

My problem looks simple, but seems no shortcut solution?

but if you can easily solve the above...will be much appreciated.

thanks again.
__________________
Rioven

Sony Ericsson XPERIA X1i WM6.1
480x800 Display Resolution
with QWERTY keyboard
Reply With Quote
  #7 (permalink)  
Old 10-09-2007, 11:59 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 3,134
Default

Maybe you can use a NumUpDown control instead of a TextBox?
Reply With Quote
  #8 (permalink)  
Old 10-09-2007, 01:40 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,690
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

Quote:
Originally Posted by Erel View Post
Maybe you can use a NumUpDown control instead of a TextBox?
Or maybe a TrackBar?

The logic is more complicated than it seems. So brute force then?

Code:
Sub HHStart_KeyPress (key)
	'process only numbers and backspace
	If (IsNumber(key) = false AND Asc(key) <> 8) Then
		HHstart.IgnoreKey
		Return
	End If
End Sub

Sub HHStart_LostFocus (key)
	'process only numbers and backspace
	If HHStart.Text <0 OR HHStart.Text > 24 Then
		Msgbox("Must be between 0 and 24")
	HHStart.Focus
	End If
End Sub
Reply With Quote
  #9 (permalink)  
Old 10-09-2007, 05:38 PM
Senior Member
 
Join Date: May 2007
Posts: 130
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Quote:
Originally Posted by Erel View Post
Maybe you can use a NumUpDown control instead of a TextBox?
Yes Erel, I have thought about that and more often applied on hours, but if I have solution for these kind of filters on textbox, I could think of some other positive applications.


Quote:
Originally Posted by agraham
The logic is more complicated than it seems
difficult for years of not writing code like me, only started again when found Basic4PPC and still refreshing and learning. I could try to do some logic flowcharting then...as it seems I could not do it on the spot anymore.


thank you!
__________________
Rioven

Sony Ericsson XPERIA X1i WM6.1
480x800 Display Resolution
with QWERTY keyboard
Reply With Quote
  #10 (permalink)  
Old 10-09-2007, 07:07 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,690
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

Quote:
Originally Posted by Rioven View Post
if I have solution for these kind of filters on textbox, I could think of some other positive applications.
I just love a challenge! Try to break this one


Code:
Sub HHStart_KeyPress (key)
	' I know this takes time but this is slow user input processing and avoids global clutter
	MAXNUM = 3456
	HHshadow = HHstart.text
	
	If (IsNumber(key) = false AND Asc(key) <> 8) Then
		'process only numbers, Asc("0")=48, AsSc("1")= 50 etc. and backspace = 8		
		HHstart.IgnoreKey
		Return
	Else If	 HHstart.SelectionLength <> 0 Then
		' part of string selected
		ss = HHStart.SelectionStart
		sl = HHStart.SelectionLength
		hl = StrLength(HHstart.text)
		first = SubString(HHshadow,0,ss)
		last = SubString(HHshadow,ss+sl,hl-sl)
		HHshadow = first & key & last		
	Else
		' no selection so process normally and check limit
		HHshadow = HHshadow & key
	End If
	
	If IsNumber(key) AND  HHshadow > MAXNUM   Then
		HHstart.IgnoreKey
		Return	
	End If
End Sub
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
SIP Input leestevens Questions & Help Needed 2 08-01-2008 04:00 PM
SOund Input? PKinz Basic4ppc Wishlist 1 12-12-2007 03:51 PM
How to limit a textbox input strlength taximania Questions & Help Needed 5 08-27-2007 06:08 PM
Input Panel Erel Code Samples & Tips 17 07-04-2007 04:29 AM
Sound/Mic input Bill Todd Questions & Help Needed 2 06-22-2007 12:29 PM


All times are GMT. The time now is 03:22 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0