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.


How to limit a textbox input strlength


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-27-2007, 10:47 AM
taximania's Avatar
Basic4ppc Veteran
 
Join Date: May 2007
Location: Derbyshire. UK
Posts: 231
Awards Showcase
Beta Tester 
Total Awards: 1
Default How to limit a textbox input strlength

I know it should be easy but I just can't figure it out.
I want to put a limit of ten chars in a textbox field ?
__________________
Endemol
Waste of space

O2 XDA Artemis Touch Flo 4.02 Full Cube Rom WM6.1

http://www.taximania.net
Reply With Quote
  #2 (permalink)  
Old 08-27-2007, 12:04 PM
Cableguy's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: N 41º11'30.30" W 8º39'46.60"
Posts: 1,314
Default

You can use my password textbox library, wich can be used as a normal textbox with a few extras....
Check the samples and tis
__________________
Paulo Gomes
Porto, Portugal

PC: Dual-Core 1,8Ghz, 2GB RAM, 80GB HD
PPC: Qtek9000, 1GB SD

DLL Version Listing
Reply With Quote
  #3 (permalink)  
Old 08-27-2007, 12:31 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,687
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

This works but has the side effect of moving the insertion point to the beginning of the textbox on the eleventh character for reasons that I don't understand.


The length is tested at nine as the KeyPress event occurs before "key" is added to the string and there seems to be no way of preventing it being added.

Code:
Sub TextBox1_KeyPress (key)
	If StrLength(Textbox1.Text) >=9 Then
	Textbox1.Text= SubString(Textbox1.Text,0,9)
	End If
End Sub
Reply With Quote
  #4 (permalink)  
Old 08-27-2007, 01:35 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,687
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

I missed the obvious - this works nicely, the check for key >31 allows backspace editing of a full length textbox value.
Code:
Sub TextBox1_KeyPress (key)
	If StrLength( textbox1.Text) >=10 Then
		If Asc(key)>31 Then
			Textbox1.IgnoreKey
		End If
	End If
End Sub

Last edited by agraham : 08-27-2007 at 01:38 PM.
Reply With Quote
  #5 (permalink)  
Old 08-27-2007, 01:53 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,687
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
Default

An improvement on my first method. I still don't understand why the insertion point moves but this code resets it. One advantage of this method is that if the user pastes in a string longer than the required length then if he tries to edit it the string is truncated which would not happen with the "ignorekey" method.

Code:
Sub TextBox1_KeyPress (key)
    If StrLength( textbox1.Text) >=9 Then	
        If Asc(key)>31 Then
            Textbox1.Text= SubString(Textbox1.Text,0,9)
            Textbox1.SelectionStart = 9
        End If
    End If
End Sub
EDIT: The following is even better for user editing as the insertion point doesn't do unexpected things

Code:
Sub TextBox1_KeyPress (key)
    If StrLength( textbox1.Text) >=9 Then	
        If Asc(key)>31 Then
            ss = Textbox1.SelectionStart
            Textbox1.Text= SubString(Textbox1.Text,0,9)
            Textbox1.SelectionStart = ss
        End If
    End If
End Sub
EDIT AGAIN!: I forgot that B4PPC 5.50 can now evaluate more complex logical conditions so the following is a little more elegant!

Code:
Sub TextBox1_KeyPress (key) 
	If StrLength(textbox1.Text) >=9 AND Asc(key) > 31 Then	
		s = textbox1.SelectionStart
		Textbox1.Text= SubString(Textbox1.Text,0,9)
		textbox1.SelectionStart = s
	End If
End Sub

Last edited by agraham : 08-27-2007 at 06:20 PM.
Reply With Quote
  #6 (permalink)  
Old 08-27-2007, 06:08 PM
taximania's Avatar
Basic4ppc Veteran
 
Join Date: May 2007
Location: Derbyshire. UK
Posts: 231
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Many thanks, it works a treat
__________________
Endemol
Waste of space

O2 XDA Artemis Touch Flo 4.02 Full Cube Rom WM6.1

http://www.taximania.net
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
scrolling image with limit europe Questions & Help Needed 1 11-07-2008 05:54 PM
TextBox limit number of digit to enter mike2ppc Questions & Help Needed 3 08-12-2008 09:34 PM
Running programs - is there a depth limit. Also window waking LineCutter Questions & Help Needed 0 01-28-2008 12:41 AM
TextBox Input Filter? dlfallen Questions & Help Needed 18 10-10-2007 07:50 PM
table Dezimalseparator saveCSV 64k limit BerndB Questions & Help Needed 0 05-02-2007 04:58 PM


All times are GMT. The time now is 02:02 PM.


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