user1333792
user1333792

Reputation: 11

How to go back to original Sub Procedure from Key Down Event (VB.NET)

I am trying to reference the KeyDown event from a independent sub procedure and have it loop back into original independent sub procedure.

  1. Starts off in Independent sub procedure (DisplayAction - which then calls KeyDown Event)
  2. Goes into KeyDown event (KeyDown then calls DisplayAction)
  3. Goes back into original sub procedure (DisplayAction) and the loop continues.

* Steps 1 and 2 work just fine in my code. Step 3 is the problem. My program does not loop back into the independent sub procedure.

Public Class frmMain
    Dim RandomNumber As Integer
    Dim RandomNumbers(1000) As Integer
    Dim intAction As Integer
    Dim strAction1 As String = "A"
    Dim strAction2 As String = "B"
    Dim strAction3 As String = "C"
    Dim strAction4 As String = "D"
    Dim i As Integer

    Private Sub frmMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        FormLoad(sender, e)
    End Sub

    Private Sub FormLoad(sender, e)

    'Creates random numbers here                    
        DisplayAction(sender, e)
    End Sub

    Public Sub DisplayAction(sender, e)

        For i As Integer = 0 To 3
            Select Case lstRandom.Items(i)
                Case 1
                    lblDisplay.Text = strAction1
                    intAction = 1
                    frmMain_KeyDown(Sender, intAction = 1)
                Case 2
                    lblDisplay.Text = strAction2
                    intAction = 2
                    frmMain_KeyDown(sender, intAction = 2)
                Case 3
                    lblDisplay.Text = strAction3
                    intAction = 3
                    frmMain_KeyDown(sender, intAction = 3)
                Case 4
                    lblDisplay.Text = strAction4
                    intAction = 4
                    frmMain_KeyDown(sender, intAction = 4)
            End Select
        Next i

    End Sub

    Private Sub frmMain_KeyDown(sender, e) Handles Me.KeyDown


        If intAction = 1 Then
            lblDisplay.Text = "works! 1"
            Call DisplayAction(sender, e)

        ElseIf intAction = 2 Then
            lblDisplay.Text = "works! 2"
            Call DisplayAction(sender, e)

        ElseIf intAction = 3 Then
            lblDisplay.Text = "works! 3"
            DisplayAction(sender, e)

        ElseIf intAction = 4 Then
            lblDisplay.Text = "works! 4"
            DisplayAction(sender, e)

        End If

    End Sub
End Class

Upvotes: 0

Views: 506

Answers (1)

Ry-
Ry-

Reputation: 224904

It's automatic! When you call another method, and said method finished executing, control is automatically returned to the caller. Also, stop throwing around sender and e, and please turn Option Strict On.

Public Class frmMain
    Dim RandomNumber As Integer
    Dim RandomNumbers(1000) As Integer
    Dim intAction As Integer
    Dim strAction1 As String = "A"
    Dim strAction2 As String = "B"
    Dim strAction3 As String = "C"
    Dim strAction4 As String = "D"

    Private Sub frmMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        FormLoad()
    End Sub

    Private Sub FormLoad()
        'Creates random numbers here                    
        DisplayAction()
    End Sub

    Public Sub DisplayAction()
        For i As Integer = 0 To 3
            Select Case lstRandom.Items(i)
                Case 1
                    lblDisplay.Text = strAction1
                    intAction = 1
                    frmMain_KeyDown(Me, EventArgs.Empty)
                Case 2
                    lblDisplay.Text = strAction2
                    intAction = 2
                    frmMain_KeyDown(Me, EventArgs.Empty)
                Case 3
                    lblDisplay.Text = strAction3
                    intAction = 3
                    frmMain_KeyDown(Me, EventArgs.Empty)
                Case 4
                    lblDisplay.Text = strAction4
                    intAction = 4
                    frmMain_KeyDown(Me, EventArgs.Empty)
            End Select
        Next
    End Sub

    Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As EventArgs) Handles Me.KeyDown
        lblDisplay.Text = "works! " & intAction
    End Sub
End Class

Upvotes: 5

Related Questions