Max Chars in a textbox

Arg81

Well-known member
Joined
Mar 11, 2005
Messages
949
Location
Midlands, UK
Programming Experience
1-3
Just a quickie...I've seen a post in the forums but it's for an ASP.net form, and I need to do something similar on a windows form (vb.net)

I have a MULTILINE textbox, with a database maximum for that field of 1000. I want to make sure that once 1000 characters have been entered into that textbox, no more can be added.
And if possible add a label so that it counts the charaters in the textbox (and also reclaim them when deleting / backspacing).

I did see something but it was using Javascripts with ASP

Is this possible for a win form?

Thanks in advance
Luke
 
Use the TextChanged event in combination with the Textlength property. TextChanged event is available for the web control too, but in web apps 'server' roundtrips (postbacks) are expensive and tried avoided by using client executing Javascripts, use all the events in win apps regardless! :)
 
OK me again :D

Maybe not the best way but it does the job...

VB.NET:
Private Sub textbox1_keypress......
 
me.lblTextCount.text = me.Textbox1.maxlength - me.textbox1.length
 
End Sub

However, it doesn't update on Paste (Ctrl+v) or when you delete all the text and start again (it ignores the first key press)
 
Back
Top