[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 868
  • Last Modified:

How to focus to next could focus control?

HI,

I need to focus to next could focus control when user press "Enter" key.

Below are the code, but it does not focus to next could focus control.

Please help to solve this problem.

Thank you.
If strKeyCode.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            frmForm.GetNextControl(frmForm.ActiveControl, True).Focus()
            MsgBox(frmForm.ActiveControl.Name)
        End If

Open in new window

0
emi_sastra
Asked:
emi_sastra
  • 12
  • 12
1 Solution
 
Wayne Taylor (webtubbs)AstronautCommented:
I'm not sure where you are running that code, but this works fine....
    Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
                Handles TextBox1.KeyPress, _
                        TextBox2.KeyPress, _
                        TextBox3.KeyPress, _
                        TextBox4.KeyPress, _
                        TextBox5.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            Me.GetNextControl(sender, True).Focus()
            MsgBox(Me.ActiveControl.Name)
        End If
    End Sub

Open in new window

0
 
emi_sastraAuthor Commented:
Hi,

The function also focus to label, groupbox and etc.

I need it to focus at texbox, checkbox or combobox.

I need it called at form keypress.

Thank you.
Private Sub Form_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        ERV_Global.Focus_To_Next_Control(Me, sender, e)
    End Sub
 
 
If strKeyCode.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            frmForm.GetNextControl(frmForm.ActiveControl, True).Focus()
            MsgBox(frmForm.ActiveControl.Name)
        End If

Open in new window

0
 
Wayne Taylor (webtubbs)AstronautCommented:
Use the forms KeyPress event, and ensure the KeyPreview property of the form is set to True.
    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            Me.GetNextControl(Me.ActiveControl, True).Focus()
            MsgBox(Me.ActiveControl.Name)
        End If
    End Sub

Open in new window

0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
emi_sastraAuthor Commented:
HI,

Your code have the similar result with mine.

For example, there are controls:

textbox1, texbox2, label1, label2, groupbox1, texbox3.

After leaving focus at texbox2, the function will focus to label1.
I want it to focus to texbox3 directly.

Thank you.
0
 
Wayne Taylor (webtubbs)AstronautCommented:
Sounds like it is working correctly. But if the order at which the controls are focussed is not what you require, you need to ensure the TabIndex of each control is set correctly.
0
 
emi_sastraAuthor Commented:
Hi,

If I use tab then it works fine, so nothing wrong with the tab index.

Thank you.
0
 
Wayne Taylor (webtubbs)AstronautCommented:
You cannot Tab into a label control. Your Tab index is incorrect.
0
 
emi_sastraAuthor Commented:
No, I mean it tab to textbox3  if I press Tab Key at  textbox2. It works fine.

textbox1, texbox2, label1, label2, groupbox1, texbox3.

Thank you.
0
 
Wayne Taylor (webtubbs)AstronautCommented:
>>No, I mean it tab to textbox3  if I press Tab Key at  textbox2. It works fine.

Yes, I realise that. The reason for that is you CANNOT tab into a Label control. The GetNextControl method does not work the same as pressing the tab key. Please check the TabIndex property for each of the controls. For example, if you wish to move from Textbox2 to Textbox3, if the TabIndex for Textbox2 = 2, then the TabIndex for Textbox3 MUST equal 3.
0
 
emi_sastraAuthor Commented:
As I mentioned before. Nothing wrong with the tab index.

I don't want to tab to label control, I want to tab to texbox3 control and it works fine if I using Tab Key not Enter Key.

If I use Enter Key than the msgbox will show :
label1, label2, groupbox1, texbox3.

Thank you.
0
 
Wayne Taylor (webtubbs)AstronautCommented:
>>Nothing wrong with the tab index.
I don't believe you. Your TabIndex must be incorrect for it to select the Label instead of the Textbox. AGAIN, you cannot tab into a Label control. And, AGAIN, pressing the Tab key IS NOT the same as the GetNextControl method.

>>I don't want to tab to label control
Yes, I know that. Please read my comments. Then read them again.

OK, lets forget the TabIndex altogether. If you maintain a set control name, you can use something like this, which will completely ignore the tab index, and the other controls that have the wrong tabindex.....
    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            Dim NextControlName As String = "TextBox" & (Me.ActiveControl.Name.ToLower.Replace("textbox", "") + 1)
            Me.Controls.Find(NextControlName, False)(0).Focus()
            MsgBox(Me.ActiveControl.Name)
        End If
    End Sub

Open in new window

0
 
emi_sastraAuthor Commented:
To make you believe of TabIndex.

I use below code.

Have you tested by your self?

Thank you.

        For Each ctrl As Control In grbFirst.Controls
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).Enabled = Inc_Number(intTabIndex)
            End If
        Next
 
        For Each ctrl As Control In grbSecond.Controls
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).Enabled = Inc_Number(intTabIndex)
            End If
        Next
 
  Public Shared Function Inc_Number(ByRef intNumber As Integer) As Integer
        intNumber += 1
        Inc_Number = intNumber
    End Function

Open in new window

0
 
Wayne Taylor (webtubbs)AstronautCommented:
>>Have you tested by your self?
Yes. And I have verified, numerous times, that I am correct.

Set the tab index manually, like this....

Textbox1 = 0
Textbox2 = 1
Label1 = 3
Textbox3 = 2

To make it easier to set the correct TabIndex, go to View > Tab Order. Click the numbers to change.
0
 
emi_sastraAuthor Commented:
I think I should send you the file.

How can I attach to you, I have try, but failed.

The extension of the uploaded file is not in the list of accepted extensions.

Thank you.
0
 
Wayne Taylor (webtubbs)AstronautCommented:
Change the extension.
0
 
emi_sastraAuthor Commented:
Can not set the type of .net managed resource file, even change the extension.

Thank you.


frmTest.vb.TXT
frmTest.Designer.VB.TXT
0
 
Wayne Taylor (webtubbs)AstronautCommented:
Well, the Tab order seems fine, because it goes from textbox to textbox. To ignore controls other than textboxes, comboboxes, or checkboxes then use this....
    Private Sub frmTes_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            Dim ctrl As Control = Me.GetNextControl(Me.ActiveControl, True)
            Do While Not (TypeOf ctrl Is TextBox Or TypeOf ctrl Is CheckBox Or TypeOf ctrl Is ComboBox)
                ctrl = Me.GetNextControl(ctrl, True)
            Loop
            ctrl.Focus()
            MsgBox(Me.ActiveControl.Name)
        End If
    End Sub

Open in new window

0
 
emi_sastraAuthor Commented:
Finally you've got it.

There also problem, at the last control of grbPenerimaanBarang, when I press Tab It goes to cmdSave button, not to next textbox.

But If I press Enter, it goes to nex texbox.

Why is it?

Try it.

Thank you.
0
 
Wayne Taylor (webtubbs)AstronautCommented:
>>at the last control of grbPenerimaanBarang, when I press Tab It goes to cmdSave button

Because that is the next control according to the TabIndex. The code I gave above will ignore the Button.

Like I said before, pressing the TAB key IS NOT the same as using the GetNextControl method. Do not confuse the two.
0
 
emi_sastraAuthor Commented:
Not like it, if you see the Set_Tab_Index, the btnSave button is at the last, before btnCancel.

In vb6 I this problem easily solved using Sendkeys "{TAB}".
How about it in vb 2005?

Thank you.

0
 
Wayne Taylor (webtubbs)AstronautCommented:
Do not use Sendkeys for this. It's not recommended or required.

Also, don't use the code to set the TabIndex. Do it properly in the IDE by going to View > Tab Order.

>>when I press Tab It goes to cmdSave button, not to next textbox.

Where do you want it to go???
0
 
emi_sastraAuthor Commented:
I want it to go as the order of set tab index in Set_Tab_Index.

If I see through view tab order, then the first groupbox index is 0, second is 1, third is 2, the fourth is 61.
When I try to change the value of the 1st textbox of 2nd groupbox it always start at 1 point something, I can not change to other than 1. something.

Thank you.
0
 
Wayne Taylor (webtubbs)AstronautCommented:
>>When I try to change the value of the 1st textbox of 2nd groupbox it always start at 1 point something, I can not change to other than 1. something.

That's how it's supposed to work. The number before the decimal is the container's TabIndex (that is the GroupBoxes), the number after is the controls TabIndex within it's container. The TabIndex of controls in different containers have no relation to one another. They must be set by container by container.

Alternatively, you can take to controls out of the GroupBoxes, then, using the arrow keys, position back in the GroupBox, but not IN the Groupbox, but OVER (you may need to bring the controls to the front). Then you can set the TabIndex properly, and your original code will work.

The attached project shows how.
WindowsApplication3.zip.txt
0
 
emi_sastraAuthor Commented:
Ok.

I think I know what to do then.

Thank you very much for your help.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 12
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now