Solved

Automatically move to next textbox

Posted on 2011-09-07
15
341 Views
Last Modified: 2012-05-12
Hi Experts,

I have 7 textboxes in asp.net (VB).  The purpose is to allow one letter be placed in each box and then move to the next box automatically.  I have tried to use the textbox_TextChanged event but cannot get it to work.  

My code is shown below and I can not get it to fire.

Any Ideas?

Protected Sub Let1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Let1.TextChanged
        If Len(Let1.Text) = 1 Then
            Let2.Focus()
        End If
    End Sub

Open in new window

0
Comment
Question by:us1975mc
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 36495944
Switch to javascript. Add an "onchange" event to each textbox and have it call the focus of the next textbox:

function(tbToFocus)
{
     document.getElementById(tbToFocus).focus();
}
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36495978
Code-Behind
Let1.Attributes.Add("onchange", "FocusNextTextBox(" & Let2.ClientID & ")")
Let2.Attributes.Add("onchange", "FocusNextTextBox(" & Let3.ClientID & ")")
'etc

Code-InFront
<script type="text/javascript">
function FocusNextTextBox(tbToFocus)
{
     document.getElementById(tbToFocus).focus();
}
</script>
0
 
LVL 5

Expert Comment

by:eridanix
ID: 36495988
Hi,

I think, you have to set focus to another textbox control with javascript.

Some useful example you can see here http://ryanfarley.com/blog/archive/2004/12/21/1325.aspx

0
 

Author Comment

by:us1975mc
ID: 36496532
strickdd,

I am not for sure where you are saying to place hte code behind.  My textbox has DataBinding, Disposed, Init, Load, PreRender, TextChanged and Unload.  The only one that I figured to place it on was the TextChange and that don't work.
Protected Sub Let1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Let1.TextChanged
        Let1.Attributes.Add("onchange", "FocusNextTextBox(" & Let2.ClientID & ")")
    End Sub

Open in new window

0
 

Author Comment

by:us1975mc
ID: 36496556
eridanix,

The web site that you sent me to was all in C#.net.  I need VB.net.
0
 
LVL 4

Expert Comment

by:guramrit
ID: 36496766
TextBox.TextChanged is server side event, it will not occur when you changed the text, but it will occur when you changed the text and press any button to PostBack changes.
You've do similar code in client side (javascript) as told by others.

You've to add following lines in Page_Load event instead of Let1_TextChanged
 Let1.Attributes.Add("onchange", "FocusNextTextBox(" & Let2.ClientID & ")")
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36496993
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Let1.TextChanged
        Let1.Attributes.Add("onchange", "FocusNextTextBox(" & Let2.ClientID & ")")
End Sub
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 28

Expert Comment

by:strickdd
ID: 36497002
Sorry, remove the "Handles Let1.TextChanged", basically this goes in the Page_Load method.
0
 

Author Comment

by:us1975mc
ID: 36497160
I have listed the code that I have in the code behind and a image of the error that I am receiving.  Any ideas? This is the error that I am getting
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
        Let1.Attributes.Add("onchange", "FocusNextTextBox(" & Let2.ClientID & ")")
        Let2.Attributes.Add("onchange", "FocusNextTextBox(" & Let3.ClientID & ")")

    End Sub

Open in new window

0
 
LVL 28

Expert Comment

by:strickdd
ID: 36497192
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
        Let1.Attributes.Add("onchange", "FocusNextTextBox('" & Let2.ClientID & "')")
        Let2.Attributes.Add("onchange", "FocusNextTextBox('" & Let3.ClientID & "')")

    End Sub

-- Added single quotes around the parameter in the function call.
0
 
LVL 4

Expert Comment

by:guramrit
ID: 36497199
replace
Let1.Attributes.Add("onchange", "FocusNextTextBox(" & Let2.ClientID & ")")
 Let2.Attributes.Add("onchange", "FocusNextTextBox(" & Let3.ClientID & ")")
with
Let1.Attributes.Add("onchange", "FocusNextTextBox('" & Let2.ClientID & "')")
Let2.Attributes.Add("onchange", "FocusNextTextBox('" & Let3.ClientID & "')")

The difference is single qoute before and after " & Let2.ClientID & "
0
 

Author Comment

by:us1975mc
ID: 36497683
Still a no go. Typing a letter in the text box does not move to the next textbox.  When I place a break on Let1.Attributes.Add("onchange", "FocusNextTextBox('" & Let2.ClientID & "')")  and hover over ClientID I get "ctl00_ContentPlaceHolder1_Let2".  Is that what I should be getting there?
0
 
LVL 28

Accepted Solution

by:
strickdd earned 500 total points
ID: 36497897
Yes, that is the ClientID of Let2. That is fine. Alert the document.getElementById line without the .focus()
0
 

Author Comment

by:us1975mc
ID: 36498091
I changed the onchange to onkeyup and it works.

Let1.Attributes.Add("onkeyup", "FocusNextTextBox('" & Let2.ClientID & "')")
Let2.Attributes.Add("onkeyup", "FocusNextTextBox('" & Let3.ClientID & "')")

Thanks for the help!
0
 

Author Closing Comment

by:us1975mc
ID: 36498099
Thanks again!

us1975mc
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now