0

我试图从一个独立的子过程中引用 KeyDown 事件,并让它循环回到原来的独立子过程中。

  1. 在独立子过程中开始(DisplayAction - 然后调用 KeyDown 事件)
  2. 进入 KeyDown 事件(KeyDown 然后调用 DisplayAction)
  3. 返回原始子过程(DisplayAction)并继续循环。

*步骤 1 和 2 在我的代码中运行良好。第3步是问题。我的程序不会循环回到独立的子程序。

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
4

1 回答 1

5

是自动的!当您调用另一个方法,并且该方法完成执行时,控制权会自动返回给调用者。另外,停止乱扔sendere转身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
于 2012-04-14T22:01:17.220 回答