By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

How to Auto jump to the next TextBox after the restriction of MaxLength?

P: 4
I am using 5 text boxes and each box enter 2 characters.I enter 2 char and automatically cursor jump to next text box How?
please help me..
thank u..
Jul 15 '15 #1
Share this Question
Share on Google+
6 Replies


IronRazer
P: 82
Use each TextBoxes TextChanged event to check if its TextLength is equal to it`s MaxLength which is 2 in your case. If it is then set focus to the next TextBox using the Select or Focus method.

Here is an example of just 3 TextBoxes. You should be able to get the idea and implement this for all 5 of your TextBoxes.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         TextBox1.MaxLength = 2
  5.         TextBox2.MaxLength = 2
  6.         TextBox3.MaxLength = 2
  7.     End Sub
  8.  
  9.     Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  10.         If TextBox1.TextLength = TextBox1.MaxLength Then
  11.             TextBox2.Select()
  12.         End If
  13.     End Sub
  14.  
  15.     Private Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
  16.         If TextBox2.TextLength = TextBox2.MaxLength Then
  17.             TextBox3.Select()
  18.         End If
  19.     End Sub
  20.  
  21.     Private Sub TextBox3_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
  22.         If TextBox3.TextLength = TextBox3.MaxLength Then
  23.             ' select the next textbox
  24.         End If
  25.     End Sub
  26. End Class
  27.  
Jul 17 '15 #2

P: 4
Ok now i got
Thanks for your replay..
Jul 18 '15 #3

P: 4
I saw another one method. There some doubt please help me

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1  
  2.  
  3.     Dim MaxLength As Integer = 2  
  4.  
  5.     Private Sub TextBoxes_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress  
  6.  
  7.         Dim aTextBox As TextBox = CType(sender, TextBox)  
  8.         If aTextBox.Text.Length >= MaxLength Then 
  9.             Select Case aTextBox.Name  
  10.                 Case "TextBox1" 
  11.                     TextBox2.Select()  
  12.                 Case "TextBox2" 
  13.                     TextBox3.Select()  
  14.                 Case "TextBox3" 
  15.                     If TextBox3.Text.Length > MaxLength Then e.Handled = True 
  16.             End Select 
  17.         End If 
  18.  
  19.     End Sub 
  20. End Class 
Here how to create that name function TextBoxes
Jul 18 '15 #4

IronRazer
P: 82
Yes you can use the KeyPress event but, i would use the TextChanged event because, a user could right click and paste 100 characters of text into any of the the textboxes and that would not raise the KeyPress event. The TextChanged event is raised if they type or paste anything in the textboxes.

Also, you don`t need to use a variable for the MaxLength. TextBoxes have a MaxLength property that you can set to 2 which will stop the user from typing or pasting more than 2 characters into them. You can set that in the TextBoxes properties on the Form`s [Design] tab Or you can do it in code like i showed in my first example.

I set the MaxLength property of the 3 textboxes to 2 in the properties on the Design tab for this example.

Expand|Select|Wrap|Line Numbers
  1.     Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
  2.         Dim tb As TextBox = DirectCast(sender, TextBox)
  3.         If tb.TextLength = tb.MaxLength Then
  4.             Select Case tb.Name
  5.                 Case "TextBox1"
  6.                     TextBox2.Select()
  7.                 Case "TextBox2"
  8.                     TextBox3.Select()
  9.                 Case "TextBox3"
  10.                     'data full, you can do whatever now. Maybe you need to set focus to a button or something...
  11.             End Select
  12.         End If
  13.     End Sub
  14.  
Jul 18 '15 #5

P: 4
Thank u for your replay..
Two Doubt this program, please solve this doubt

1)How to create TextBox_TextChanged.In form side came only single text box name (ex: TextBox1_TextChanged or TextBox2_TextChanged).

2)why use DirectCast. what showing DirectCast
Jul 27 '15 #6

IronRazer
P: 82
"1)How to create TextBox_TextChanged.In form side came only single text box name (ex: TextBox1_TextChanged or TextBox2_TextChanged)."

I am not sure what you are asking there.


"2)why use DirectCast. what showing DirectCast"

If you notice, the "sender" parameter of the TextChanged event sub is of type Object. The TextBox that raises the event is assigned to the "sender" Object.

An Object is the base class for all .Net classes, meaning it can be assigned any class. However, an Object type does not let you directly access the specific properties and methods of the class that was assigned to it.

That is why you need to cast the Object to a TextBox type, so you can access the specific properties of the TextBox class that was assigned to the Object.

Using DirectCast is faster and more preferable for instances like this than using the CType operator. You can read the second post down in the link below to get an idea of the difference between DirectCast and CType.

http://stackoverflow.com/questions/3...type-in-vb-net
Jul 28 '15 #7

Post your reply

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