Question User Define Sub -- Move to next Object

t_jcruise

Member
Joined
Jul 16, 2010
Messages
10
Programming Experience
Beginner
Help and advice needed.

I've created a private sub which enables me to move to the next object when I press enter in the textbox or any input controls...

What I want to do is to create a universal sub procedure for moving to next object.

this is my code


Code 1 - but this will work only for textboxes

VB.NET:
    Private Sub MoveToNextObj(ByVal e As System.Windows.Forms.KeyEventArgs, ByVal NextOBj As Object)
        Dim textOBj As TextBox
        textOBj = CType(NextOBj, TextBox)
        If e.KeyCode = Keys.Enter Then
            textOBj.Focus()
        End If
    End Sub

any suggestions or revisions? this sub procedure will work for textbox only... help needed... thanks...

Code 2 - but receiving errors such as "Option Strict On is disallowing Late binding"

VB.NET:
    Private Sub MoveToNextObj(ByVal e As System.Windows.Forms.KeyEventArgs, ByVal NextOBj As Object)
        If e.KeyCode = Keys.Enter Then
            NextOBj.Focus()
        End If
    End Sub
 
Last edited:
The "proper" way to do this is to use the Tab order. Simply set the Tab order as appropriate in the designer and then use it:
VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ctl As Control = Me.GetNextControl(Me, True)

        Do Until ctl Is Nothing
            AddHandler ctl.KeyDown, AddressOf AllControls_KeyDown
            ctl = Me.GetNextControl(ctl, True)
        Loop
    End Sub

    Private Sub AllControls_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Me.SelectNextControl(DirectCast(sender, Control), True, True, True, True)
    End Sub

End Class
That's all you need. The rest happens automatically.
 
Back
Top