Limit users input to textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • michael0120
    New Member
    • Nov 2014
    • 6

    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.
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    The property for setting the MaxLength is called MaxLength
    (http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx)

    something like the code below might get you from TextBox1 to TextBox2
    Code:
        Private Sub TextBox1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
            If TextBox1.Text.Length  = 4 Then
                TextBox2.Focus()
            End If
        End Sub
    But there ARE better ways ;)
    Last edited by Luuk; Nov 27 '14, 10:34 AM. Reason: changed 'smarter' to 'better' in last sentence....

    Comment

    • michael0120
      New Member
      • Nov 2014
      • 6

      #3
      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.

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        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.

        Comment

        • twinnyfo
          Recognized Expert Moderator Specialist
          • Nov 2011
          • 3653

          #5
          All,

          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.....

          Comment

          • Luuk
            Recognized Expert Top Contributor
            • Mar 2012
            • 1043

            #6
            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.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Using the TextBox1.Text.L ength 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.

              Comment

              • Luuk
                Recognized Expert Top Contributor
                • Mar 2012
                • 1043

                #8
                How does that relate to this article:
                How to: Position the Cursor at the End of Text in a TextBox Control

                ?

                Comment

                • twinnyfo
                  Recognized Expert Moderator Specialist
                  • Nov 2011
                  • 3653

                  #9
                  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.

                  Comment

                  Working...