仅使按键代码运行一次,请在按开始时提供帮助

问题描述 投票:-1回答:1

我正在编写一段代码,当按下一个按钮时,它会创建一个图片框,但是当您按住相同的键时,该图片框需要增大。最初,我的代码重复了该框的创建,但是现在我尝试了下面的代码,而是只创建了一次,但是在代码末尾。没有人知道在第一次按下键实例的情况下如何执行某些代码来执行一部分键按下代码吗?

Private Class Form1
      Dim KeyHolding As Boolean = False


Private Sub Btn_1_KeyDown(sender As Object, e As KeyEventArgs) Handles Btn_1.KeyDown
        If Not KeyHolding Then          'the events which are activated once only when the key is pressed
            KeyHolding = True

            Dim PB As New PictureBox With {.Width = Btn_1.Width - 2, .Height = 10, .Top = Btn_1.Top + 20, .Left = Btn_1.Left + 1, .BackColor = Color.Cyan}
            PB.Name = "PB_1"
            TestPanel.Controls.Add(PB)
        Else                            'the events which are constantly done when the key is held

        End If
    End Sub

Private Sub Btn_1_KeyUp(sender As Object, e As KeyEventArgs) Handles Btn_1.KeyUp
        Btn_1.BackColor = SystemColors.Control
        KeyHolding = False
    End Sub
vb.net vb.net-2010
1个回答
0
投票

尝试此

Dim KeyHolding As Boolean = False
Dim PB As PictureBox

Private Sub Btn_1_KeyDown(sender As Object, e As KeyEventArgs) Handles Btn_1.KeyDown
    If Not KeyHolding Then          'the events which are activated once only when the key is pressed
        KeyHolding = True
        PB = New PictureBox With {.Width = Btn_1.Width - 2, .Height = 10, .Top = Btn_1.Top + 20, .Left = Btn_1.Left + 1, .BackColor = Color.Cyan}
        PB.Name = "PB_1"
        TestPanel.Controls.Add(PB)
    Else                            'the events which are constantly done when the key is held
        PB.Width += 10
        PB.Height += 10
    End If
End Sub

Private Sub Btn_1_KeyUp(sender As Object, e As KeyEventArgs) Handles Btn_1.KeyUp
    Btn_1.BackColor = SystemColors.Control
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.