Solved

Automatically move to next textbox

Posted on 2011-09-07
15
353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

695 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