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

自動です!別のメソッドを呼び出し、そのメソッドの実行が終了すると、制御は自動的に呼び出し元に返されます。senderまた、とを振り回すのはやめてe、を回してください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 に答える