Focus next text box with command button

Hi Experts,

I need some help with this. I have the following code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      If Not (lastTextBox Is Nothing) Then
            Static R As New Random
            lastTextBox.Text = ComboBox27.Items.Item(R.Next(0, Me.ComboBox27.Items.Count))
            R.Next()
        End If

Just after this action I would like the next text box to be focused (like using the tab). The tab index of the text boxes is from 0 to 17.

Thanks!
hermorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Praveen KumarArchitectCommented:
YourTextBoxName.Focus()
Mohammed NasmanSoftware DeveloperCommented:
this will work without needing to hard coded the textbox name

SelectNextControl(ActiveControl, True, True, True, True)
hermorAuthor Commented:
mnasman,

I tried your code, it is moving the focus, but it is doing it to a ComboBox next to the TextBox. For example, no matter which text box was focused, the next .focus is moving to ComboBox1 (which has a tab index of 20). The same happened with another code I tried before asking.

Thank you!
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

kisruCommented:
Try this one.

Page.RegisterStartupScript("SetFocus", _
"<script>document.getElementById('" & TextBox1.ClientID & _
"').focus();</script>")

hermorAuthor Commented:
I have tried every recommendation.

I tought this question wasn't that hard. I have increased its value.

Thanks!
hermorAuthor Commented:
Hi,

I just want the next tab index to be focused. Remember that I am using a button for this.

This textbox is focused then go to the next tab index and so on.

Thank you!
hermorAuthor Commented:

I finally found how to do it. I answered the question myself.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      If Not (lastTextBox Is Nothing) Then
            Static R As New Random
            lastTextBox.Text = ComboBox27.Items.Item(R.Next(0, Me.ComboBox27.Items.Count))
            R.Next()
'I added the following two lines
            lastTextBox.Focus()
            SendKeys.Send("{TAB}")
 
  End If

Thank you experts...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.