472,145 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Limit users input to textbox

Good Day to you all,

I have an application where i want to limit a user's input to the textbox. What i want is that they wont exceed the textbox width and when that textbox is full it will automatically go to the next textbox. Im using Visual Basic 6.0.

Thanks for the help.
Nov 27 '14 #1
8 2385
1,047 Expert 1GB
The property for setting the MaxLength is called MaxLength

something like the code below might get you from TextBox1 to TextBox2
Expand|Select|Wrap|Line Numbers
  1.     Private Sub TextBox1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
  2.         If TextBox1.Text.Length  = 4 Then
  3.             TextBox2.Focus()
  4.         End If
  5.     End Sub
But there ARE better ways ;)
Nov 27 '14 #2
Luuk thanks for the reply.
but is there other ways other than MaxLength? i dont want to limit it by characters. i want to limit the input just within the textbox's width, if its possible.
Nov 28 '14 #3
1,047 Expert 1GB
How wide is a character?
An 'i' is less wide than a 'w'....

So, how much characters do fit in a textbox's width?

MaxLength is just to make sure you cannot enter more characters than given in that property.
Nov 28 '14 #4
3,653 Expert Mod 2GB

There are ways to do this, but it is very involved, and includes creating a separate class to limit the size of the input for a text box. The code I have is 200 lines long. Not sure that is the simplicity you are looking for.....
Dec 1 '14 #5
1,047 Expert 1GB
The code i gave will work too, if you also use the MaxLength

If you do not use MaxLength, it will be possible to paste a too long text.
Dec 1 '14 #6
9,735 Expert Mod 8TB
Using the TextBox1.Text.Length to determine when to move to the next TextBox is not correct.

You need to check if the position of the cursor in the TextBox to see if it is at the end and only move to the next TextBox if the cursor is at the end of the allotted space.

Likewise, you will probably have to do the same thing for the backspace key to move back a TextBox upon having the cursor in the first position of the TextBox...

Twinnyfo's reply is correct: there is no real simple solution.
Dec 1 '14 #7
1,047 Expert 1GB
How does that relate to this article:
How to: Position the Cursor at the End of Text in a TextBox Control

Dec 1 '14 #8
3,653 Expert Mod 2GB
Luuk, et al,

We must make sure we differentiate betwixt "Text Box Width"--which is the specific question of the OP, and "Text Length"--which is always based on the number of characters.

Text Box Width is a physical measurement, which may differ based on OS Display settings. If we create a form that has a text box on it that is one inch wide (1440 twips), then that is a physical limitation of the text box. It is possible to overflow the size of a text box, at which point the text will not display properly.

Text Length is always an integer and is a count of the number of characters. I know you know this and fully understand the difference between these two, so I am not saying this to insult anyone's intelligence or competence....

It is possible to display a differing number of characters within the same text box. One could reduce the font size, and more characters could fit. One could use all skinny letters (e.g. "iiiiiiiiii" takes up less room than "wwwwwwwwww" with most font styles). Limiting the text box to the length of string to 10 characters will limit the amount of data that may be entered, but will not take into account either the size of the text box (a physical measurement) nor the size of the characters within the font set.

I do have some code which will calculate all this stuff (via another web site (sorry, Mods), but again--it is 100's of lines of code. I got mine to work for an experiment I was doing a while back, but have limited use for it right now.

If anyone is interested in this, I am glad to share--just not sure I remember all the tweaks needed to get it to work properly.
Dec 2 '14 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by SAN CAZIANO | last post: by
2 posts views Thread by Parintas Themis STE Kardias | last post: by
1 post views Thread by Parintas Themis STE Kardias | last post: by
6 posts views Thread by Maziar Aflatoun | last post: by
1 post views Thread by eSolTec, Inc. 501(c)(3) | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.