473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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
6 2222
IronRazer
83 64KB
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
Ok now i got
Thanks for your replay..
Jul 18 '15 #3
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
83 64KB
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
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
83 64KB
"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

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

Similar topics

1
by: Gil | last post by:
I am trying to advance to the next textbox when input for the current textbox has reached its maximum length. In a different newsgroup I found this code snipit, but I have not been able to use it....
2
by: VB Programmer | last post by:
I created a page which I am using to prevent the user from hitting the BACK button. I'll call it my "Auto Jump" page. When it is called it basically auto-redirects to a page specified in the...
4
by: Xero | last post by:
Hello. I am using vb.net. I have a series of textboxes and each of them can contain one letter only. How can I get the cursor jump to the next textbox after one has been filled with a letter?...
1
by: AA Arens | last post by:
I have a button containing the VB command Me.Notes = Me.Notes & Format(Date, "dd-mm-yyyy") to have a date filled in a field. I want it automatically jump to the next field, after the date is...
5
cbucks
by: cbucks | last post by:
I would really appreciate help with this one. The string of code below fires with the afterupdate function of a 3rd combo box slelction. The idea is to provide a series of dynamic text boxes. The...
4
by: Surek | last post by:
I have created a form using the wizard, to display the contents of the table. I need to update the table by entering the data through the form. When i click the update button of my form, it ignores...
0
by: kimiraikkonen | last post by:
Hi I used Windows Media Player control to programme a simple player, The problem is when the song finishes playing it must jump to next item automatically. (listbox's items are used as media's...
3
by: spoonybard | last post by:
Hi Everyone, I have an ASP.Net 2.0 C# web application that requires a user to enter in two sets of numbers. The first number is broken into 4 textboxes and the second number is broken into 2...
1
by: Omg83191 | last post by:
I'm doing a web form for registration, log in and forgot password. I have the registration and the log in. I used Vb.net and Mysql as my database. I have this columns in my database userEmail,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.