使用 KeyPress 事件的计数器

Counter using KeyPress events

第一次在这里发帖,虽然我是找答案的常客。我是 VB 和编程的新手。 我的问题是这样的。我在 VBA 中找到了这个,但我想将我的 "program" 转换为带有 VB 的独立可执行文件(使用 Visual Studio 2015) 我想跟踪在文本框上完成的某些按键操作,到目前为止我想出了一些可行的方法,但它看起来很乱。 谁能想出更好的方法

    Public Class Form1

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress
        Select Case UCase(e.KeyChar) 'For capturing keypresses and adding them
            Case "W" : Label3.Text = Val(Label3.Text) + 1
            Case "R" : Label4.Text = Val(Label4.Text) + 1
        End Select
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        TextBox1.Text = "" 'Clears the text box after each keypress
    End Sub

    Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim WcountA As Integer
        Dim RcountA As Integer
        Dim WcountB As Integer
        Dim RcountB As Integer
        Dim Wavg As Single
        Dim Ravg As Single
        Select Case Button1.Text
            Case "Finish A" 'Finishes first count and stores results in labels
                WcountA = Convert.ToInt32(Label3.Text)
                RcountA = Convert.ToInt32(Label4.Text)
                Button1.Text = "Finish B"
                Label5.Text = WcountA
                Label7.Text = RcountA
                TextBox1.Focus()
                Label3.Text = ""
                Label4.Text = ""
            Case "Finish B" 'Finishes second count and stores in labels
                WcountB = Convert.ToInt32(Label3.Text)
                RcountB = Convert.ToInt32(Label4.Text)
                With Button1
                    .Text = "Finished"
                    .Enabled = False
                End With
                Label6.Text = WcountB
                Label8.Text = RcountB
                Label3.Text = ""
                Label4.Text = ""
                WcountA = Label5.Text
                RcountA = Label7.Text
                WcountB = Label6.Text
                RcountB = Label8.Text
                Wavg = (WcountA + WcountB) / 2
                Ravg = (RcountA + RcountB) / 2

                MsgBox("W average = " & Wavg & vbNewLine & "R average = " & Ravg)
        End Select

    End Sub
End Class

在表单上,​​我有一个记录按键事件的文本框,每个特定按键("W" 和 "R")增加 1 的标签,一个每次点击都会改变其功能的按钮(完成第一次计数,完成第二次计数)和一些我不得不用来存储最终计算的第一次和第二次计数的标签。 任何建议将不胜感激。 提前致谢!

首先,您需要像这样取出按键处理程序的计数器;

因为每次 Button1_Click 子结束时它们都消失了。

 Public Class Form1

    Dim WcountA As Integer
    Dim RcountA As Integer
    Dim WcountB As Integer
    Dim RcountB As Integer
    Dim Wavg As Single
    Dim Ravg As Single

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress
    Select Case UCase(e.KeyChar) 'For capturing keypresses and adding them
        Case "W" : Label3.Text = Val(Label3.Text) + 1
        Case "R" : Label4.Text = Val(Label4.Text) + 1
    End Select
End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    TextBox1.Text = "" 'Clears the text box after each keypress
End Sub

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Select Case Button1.Text
        Case "Finish A" 'Finishes first count and stores results in labels
            WcountA = Convert.ToInt32(Label3.Text)
            RcountA = Convert.ToInt32(Label4.Text)
            Button1.Text = "Finish B"
            Label5.Text = WcountA
            Label7.Text = RcountA
            TextBox1.Focus()
            Label3.Text = ""
            Label4.Text = ""
        Case "Finish B" 'Finishes second count and stores in labels
            WcountB = Convert.ToInt32(Label3.Text)
            RcountB = Convert.ToInt32(Label4.Text)
            With Button1
                .Text = "Finished"
                .Enabled = False
            End With
            Label6.Text = WcountB
            Label8.Text = RcountB
            Label3.Text = ""
            Label4.Text = ""
            WcountA = Label5.Text
            RcountA = Label7.Text
            WcountB = Label6.Text
            RcountB = Label8.Text
            Wavg = (WcountA + WcountB) / 2
            Ravg = (RcountA + RcountB) / 2

            MsgBox("W average = " & Wavg & vbNewLine & "R average = " & Ravg)
    End Select

End Sub

结束Class

这就是我最终做的,效果更好,看起来更干净。感谢 Lectere 为我指明了正确的道路!

    Public Class Form1
    Dim WcountA As Integer
    Dim RcountA As Integer
    Dim WcountB As Integer
    Dim RcountB As Integer
    Dim Wavg As Single
    Dim Ravg As Single
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress
        Select Case UCase(e.KeyChar) 'For capturing keypresses and adding them
            Case "W" : lblWcount.Text = Val(lblWcount.Text) + 1
            Case "R" : lblRcount.Text = Val(lblRcount.Text) + 1
            Case Convert.ToChar(13) : Button1_Click(sender, e)
        End Select
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        TextBox1.Text = "" 'Clears the text box after each keypress
    End Sub

    Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Select Case Button1.Text
            Case "Finish A" 'Finishes first count and stores results in variables
                If lblWcount.Text = vbNullString Then 'checks for empty values and treats them as 0
                    WcountA = 0
                Else
                    WcountA = lblWcount.Text
                End If
                If lblRcount.Text = vbNullString Then 'checks for empty values and treats them as 0
                    RcountA = 0
                Else
                    RcountA = lblRcount.Text
                End If
                Button1.Text = "Finish B"
                lbl_1.Text = WcountA
                lbl_2.Text = RcountA
                TextBox1.Focus()
                lblWcount.Text = vbNullString
                lblRcount.Text = vbNullString
                lblcount.Text = "Count B"
            Case "Finish B" 'Finishes second count and stores results in variables
                lblcount.Text = vbNullString
                If lblWcount.Text = vbNullString Then
                    WcountB = 0
                Else
                    WcountB = lblWcount.Text
                End If
                If lblRcount.Text = vbNullString Then
                    RcountB = 0
                Else
                    RcountB = lblRcount.Text
                End If
                With Button1
                    .Text = "Reset"
                End With
                lbl_3.Text = WcountB
                lbl_4.Text = RcountB
                lblWcount.Text = vbNullString
                lblRcount.Text = vbNullString
                Wavg = (WcountA + WcountB) / 2
                Ravg = (RcountA + RcountB) / 2
                MsgBox("W average = " & Wavg & vbNewLine & "R average = " & Ravg)
                Button1.Focus()
            Case "Reset" 'Resets values
                Dim i As Integer
                For i = 1 To 4 'clears labels that start with lbl_ (1 through 4)
                    Dim myLabel As Label = CType(Controls("lbl_" & i), Label)
                    myLabel.Text = vbNullString
                Next
                Initial() ' calls initial form state
        End Select
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Initial() 'calls initial form state at load up
    End Sub
    Private Sub Initial() 'initial state sub
        lblcount.Text = "Count A"
        TextBox1.Focus()
        Button1.Text = "Finish A"

    End Sub
End Class

我设法使用循环来清除标签,并且还使在 KeyPress 事件期间按 Enter 被视为按钮单击。 喜欢这种当我终于想出新事物时的成就感!喜欢这样的页面,可以学到很多东西!