如何从按键关闭事件返回到原始子过程(VB.NET)

如何从按键关闭事件返回到原始子过程(VB.NET),vb.net,events,onkeydown,Vb.net,Events,Onkeydown,我试图从一个独立的子过程中引用KeyDown事件,并使其循环回到原始的独立子过程中 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 strAction

我试图从一个独立的子过程中引用KeyDown事件,并使其循环回到原始的独立子过程中

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
  • 在独立子过程中启动(DisplayAction-然后调用KeyDown事件)
  • 进入KeyDown事件(KeyDown然后调用DisplayAction)
  • 返回到原始子过程(DisplayAction),循环继续 *在我的代码中,步骤1和2工作得很好。第三步是问题所在。我的程序不会循环回独立的子过程

    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
    

    这是自动的!当您调用另一个方法,并且所述方法已完成执行时,控制将自动返回给调用者。此外,请停止在
    发送者
    e
    之间乱扔,并请将
    选项严格打开

    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
    

    这是自动的!当您调用另一个方法,并且所述方法已完成执行时,控制将自动返回给调用者。此外,请停止在
    发送者
    e
    之间乱扔,并请将
    选项严格打开

    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
    
    +1.此外,如果您永远不需要事件参数,您可以将其关闭以简化方法签名。私有子frmMain_Load()处理Me.Load。私有子函数frmMain_KeyDown()处理我。KeyDown+1。此外,如果您永远不需要事件参数,您可以将其关闭以简化方法签名。私有子frmMain_Load()处理Me.Load。私有子函数frmMain_KeyDown()处理我。KeyDown