使用KeyPress事件的计数器

时间:2016-05-15 23:37:53

标签: vb.net counter keypress visual-studio-2015

第一次在这里发帖,虽然我在寻找答案时经常访问。我是VB和编程的新手。 我的问题是这个。我在VBA中想到了这一点,但我希望将我的“程序”转换为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

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

2 个答案:

答案 0 :(得分:0)

首先,您需要取出关键预处理程序的计数器,如此;

因为每次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

结束班

答案 1 :(得分:0)

这就是我最终做的,效果更好,看起来更干净。感谢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键被视为按钮单击。 当我终于想出一些关于我新事物的东西时,我很喜欢这种成就感!喜欢这样的页面,人们可以学到很多东西!