📄 formtt.vb
字号:
'lbStart
'
Me.lbStart.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.lbStart.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.lbStart.ForeColor = System.Drawing.SystemColors.Desktop
Me.lbStart.Location = New System.Drawing.Point(192, 88)
Me.lbStart.Name = "lbStart"
Me.lbStart.Size = New System.Drawing.Size(160, 32)
Me.lbStart.TabIndex = 2
Me.lbStart.Text = "按回车键开始计时"
Me.lbStart.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Label5
'
Me.Label5.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Label5.Location = New System.Drawing.Point(8, 128)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(552, 16)
Me.Label5.TabIndex = 0
Me.Label5.Text = "Label5"
'
'Label8
'
Me.Label8.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Label8.Location = New System.Drawing.Point(8, 200)
Me.Label8.Name = "Label8"
Me.Label8.Size = New System.Drawing.Size(552, 16)
Me.Label8.TabIndex = 0
Me.Label8.Text = "Label8"
'
'formMain
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(582, 311)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel1, Me.ToolBar1})
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Menu = Me.MainMenu1
Me.MinimizeBox = False
Me.Name = "formMain"
Me.Text = "英打练习"
Me.Panel1.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Dim lbSource(5) As Label, lbInput(5) As Label
'分别定义用户输入和原始文本的Label控件数组
Dim sSource(331) As Char, sInput(331) As Char
'分别定义用于存储用户输入和原始文本的字符数组
Dim iNum As Integer, iCorrect As Integer, iSecond As Integer
'iNum用于记录用户已经输入的字符数,iCorrect用于记录用户输入正确的字符数
'iSecond用于记录已经用去的时间
Private Sub formTT_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lbSource(1) = Label1
lbSource(2) = Label3
lbSource(3) = Label5
lbSource(4) = Label7
lbSource(5) = Label9
lbInput(1) = Label2
lbInput(2) = Label4
lbInput(3) = Label6
lbInput(4) = Label8
lbInput(5) = Label10
'将用于显示原始文本和用户输入文本的Label赋给数组
'以便我们在后面的程序中使用这些控件
lbStart.Text = "按回车键开始计时"
lbStart.Visible = False
Timer1.Enabled = False
Timer1.Interval = 1000
ToolBarButton3.Enabled = False
Dim i As Integer, j As Integer
For i = 1 To 5
lbSource(i).Text = ""
lbInput(i).Text = ""
Next
'对控件的属性值初始化
iNum = 0
iCorrect = 0
iSecond = 0
'对变量初始化
End Sub
Private Sub subRandom()
Dim i As Integer, j As Integer, s As String
For i = 1 To 5
lbSource(i).Text = ""
Next
'清空原始文本
Randomize()
'随机函数种子
For i = 1 To 330
If i Mod 6 = 0 Then
sSource(i) = " "
lbSource(Int((i - 1) / 66 + 1)).Text = lbSource(Int((i - 1) / 66 + 1)).Text & " "
'每隔五个字母就自动产生一个空格
Else
sSource(i) = Chr(Int(Rnd() * 25 + 97))
lbSource(Int((i - 1) / 66 + 1)).Text = lbSource(Int((i - 1) / 66 + 1)).Text & sSource(i)
'在小写字母“a”-“z”中随机产生一个字母
'并将其加入sSource字符数组和lbSource中。
End If
Next
iNum = 0
iCorrect = 0
iSecond = 0
For i = 1 To 5
lbInput(i).Text = ""
Next
For i = 1 To 330
sInput(i) = ""
Next
lbPerformance.Text = ""
Timer1.Enabled = False
lbStart.Text = "按回车键开始计时"
lbStart.Visible = True
Panel1.Focus()
ToolBarButton3.Enabled = False
'重置各控件和变量的值
End Sub
Private Sub subOpenTxt()
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
'用户用“打开按钮”选择了文本文件,并单击了确定按钮
Dim i As Integer, j As Integer, s As String
For i = 1 To 5
lbSource(i).Text = ""
Next
'清空原始文本
FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
'以“Input”方式打开文本文件
Do While Not EOF(1)
s = s & LineInput(1)
If s.Length > 330 Then
Exit Do
End If
Loop
'将文本文件内容读入s中
'当读至文件尾,或字符串s的长度超过330的时候,就结束循环。
Do While s.Length < 330
s = s & s
Loop
'若s的长度小于330,则对s进行自身复制,以保证s的长度
FileClose(1)
'关闭文本文件
sSource = s.Substring(1, 330).ToCharArray()
'将字符串s的前330个字符复制到字符数组sSource中
For i = 1 To 330
lbSource(Int(i / 66 + 1)).Text = lbSource(Int(i / 66 + 1)).Text & sSource(i)
Next
'将字符数组sSource中的字符分行放入用于显示原始文本的5个Label中
iNum = 0
iCorrect = 0
iSecond = 0
For i = 1 To 5
lbInput(i).Text = ""
Next
For i = 1 To 330
sInput(i) = ""
Next
lbPerformance.Text = ""
Timer1.Enabled = False
lbStart.Text = "按回车键开始计时"
lbStart.Visible = True
Panel1.Focus()
ToolBarButton3.Enabled = False
'重置各控件和变量的值
End If
End Sub
Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick
Select Case ToolBar1.Buttons.IndexOf(e.Button)
Case 0
subRandom()
'随机产生原始文本
Case 1
subOpenTxt()
'从文本文件中读入文本
Case 2
If ToolBarButton3.Text = "暂停(&P)" Then
Timer1.Enabled = False
ToolBarButton3.Text = "计时(&S)"
Else
Timer1.Enabled = True
ToolBarButton3.Text = "暂停(&P)"
End If
'根据按钮显示的文本内容来控制计时的暂停和开始
Case 3
Close()
'退出
End Select
'根据点击的ToolBar中的Button的索引值来判断用户点击的按钮
End Sub
Private Sub mRandom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mRandom.Click
subRandom()
'随机产生原始文本
End Sub
Private Sub mOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mOpen.Click
subOpenTxt()
'从文本文件中读入文本
End Sub
Private Sub mExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mExit.Click
Close()
'退出
End Sub
Private Overloads Sub Panel1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Panel1.KeyPress
'本函数用于对用户的每次输入进行判断,并对部分变量进行相应的计数
If iNum = 329 Then
Timer1.Enabled = False
lbStart.Text = "测试结束"
lbStart.Visible = True
'当用户已经输入了329个字符,则测试完成
'此时终止时钟,并给出提示信息
ElseIf lbStart.Visible = True Then
If e.KeyChar = Chr(13) Then
lbStart.Visible = False
Timer1.Enabled = True
ToolBarButton3.Enabled = True
End If
'如果尚未开始测试,当用户输入回车键时,开始测试,并计时
ElseIf Timer1.Enabled = True Then
Select Case e.KeyChar
Case Chr(13)
lbInput(1).Text = lbInput(1).Text + " "
iNum = iNum + 1
sInput(iNum) = " "
'用户开始测试后,用户输入的回车键被当作空格键处理
Case Else
lbInput(Int(iNum / 66 + 1)).Text = lbInput(Int(iNum / 66 + 1)).Text & e.KeyChar
iNum = iNum + 1
sInput(iNum) = e.KeyChar
'变量iNum进行累加,对用户输入的字符进行分行显示,并存储到sInput中
End Select
If sInput(iNum) = sSource(iNum) Then
iCorrect = iCorrect + 1
'如果输入的字符和原始文本中相对应的字符相等,则对变量iCorrect累加
End If
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
iSecond = iSecond + 1
'在计时器中对时间进行累加
lbPerformance.Text = "共用了" & iSecond & "秒,速度" & Int(60 * iNum / iSecond) & "字/分,正确率:" & iCorrect & "/" & iNum & "=" & Int(100 * iCorrect / iNum) & "%"
'每次计时都对当前的时间、速度、正确率等信息进行统计,并显示出来
End Sub
Private Sub mAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mAbout.Click
Dim fAbout As New formAbout()
fAbout.Show()
'显示“关于”窗口
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -