正如问题所说,我想制作一个IF语句来检查我创建的六个标签中是否存在按下的键。在六个标签中,随机生成一个字母并放入每个标签中。
我有这行代码,但它没有正常工作,因为正确的按键操作仍会执行下面的代码。
If (e.KeyChar <> lblletter1.Text And e.KeyChar <> lblletter2.Text And e.KeyChar <> lblletter3.Text And e.KeyChar <> lblletter4.Text And e.KeyChar <> lblletter5.Text And e.KeyChar <> lblletter6.Text) Then
score -= 1
lblscore.Text = score
incorrect += 1
End If
以下是本节上面的代码:
If (e.KeyChar = lblletter2.Text) Then
'MsgBox("works")
score += 1
lblscore.Text = score
lblletter2.Top = 35
lblletter2.Text = Convert.ToChar(num).ToString
num = random.Next(97, 122)
speed += 1
End If
出于某种原因,在我禁用该行之后它按预期工作,我已经为我拥有的六个标签中的每一个重复了这些。
lblletter2.Text = Convert.ToChar(num).ToString
希望它不会太混乱
Private Sub typingtutor_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
'CHECKS IF THE KEY PRESSED IS THE SAME AS THE LETTER IN THE LABELS, IF IT IS THEN THE LABEL MAKES A NEW LETTER AND ADDS 1 TO THE SCORE
'PROBLEM WITH CODE - IF THERE ARE TWO LABELS WITH SAME LETTER I CANNOT FIND A WAY TO CHECK TO MAKE ONE KEYPRESS CHANGE BOTH LABELS WITHOUT AFFECTING THE SCORING (TRIED SEPARATE IF STATEMENTS BUT THEN THAT MEANS THE ELSE STATEMENT CANNOT BE USED, THE CODE "e.keychar <> lblletter.text" DOESN:T WORK)
If line1.Visible = True Then ' Disables key presses from working until the start button is pressed
'nested if statements that work only when the above condition is met (line becomes visible when start button is pressed)
If (e.KeyChar = lblletter1.Text) Then ' check if the pressed key (e.keychar) is the same as the letter in the label
score += 1
lblscore.Text = score
lblletter1.Top = 35
'lblletter1.Text = Convert.ToChar(num).ToString 'uses the randomly produced number that was declared globally and converts it to a letter (ascii) - sourced from MSDN
num = random.Next(97, 122) 'chooses a random number between 97 and 122 which represents the letters of alphabet, for next letter
speed += 1 ' keeps track of how many key presses there are have been
End If
If (e.KeyChar = lblletter2.Text) Then
'MsgBox("works")
score += 1
lblscore.Text = score
lblletter2.Top = 35
'lblletter2.Text = Convert.ToChar(num).ToString
num = random.Next(97, 122)
speed += 1
End If
If (e.KeyChar = lblletter3.Text) Then
score += 1
lblscore.Text = score
lblletter3.Top = 35
'lblletter3.Text = Convert.ToChar(num).ToString
num = random.Next(97, 122)
speed += 1
End If
If (e.KeyChar = lblletter4.Text) Then
score += 1
lblscore.Text = score
lblletter4.Top = 35
'lblletter4.Text = Convert.ToChar(num).ToString
num = random.Next(97, 122)
speed += 1
End If
If (e.KeyChar = lblletter5.Text) Then
score += 1
lblscore.Text = score
lblletter5.Top = 35
'lblletter5.Text = Convert.ToChar(num).ToString
num = random.Next(97, 122)
speed += 1
End If
If (e.KeyChar = lblletter6.Text) Then
score += 1
lblscore.Text = score
lblletter6.Top = 35
'lblletter6.Text = Convert.ToChar(num).ToString
num = random.Next(97, 122)
speed += 1
End If
'Else
'If (e.KeyChar <> lblletter1.Text And e.KeyChar <> lblletter2.Text And e.KeyChar <> lblletter3.Text And e.KeyChar <> lblletter4.Text And e.KeyChar <> lblletter5.Text And e.KeyChar <> lblletter6.Text) Then
If (e.KeyChar <> lblletter1.Text(0) And e.KeyChar <> lblletter2.Text(0) And e.KeyChar <> lblletter3.Text(0) And e.KeyChar <> lblletter4.Text(0) And e.KeyChar <> lblletter5.Text(0) And e.KeyChar <> lblletter6.Text(0)) Then
'If (e.KeyChar <> lblletter1.Text(0) And e.KeyChar <> lblletter2.Text(0) And _
'e.KeyChar <> lblletter3.Text(0) And e.KeyChar <> lblletter4.Text(0) And _
'e.KeyChar <> lblletter5.Text(0) And e.KeyChar <> lblletter6.Text(0)) Then
score -= 1
lblscore.Text = score
incorrect += 1 ' variable used to check for accuracy
speed += 1
Label2.Text = incorrect
End If
End If
If score < 0 Then
counter -= 1
End If
End Sub
答案 0 :(得分:0)
[编辑] 此答案已更新,以回应问题的变化。
不是复制6个标签中的每个标签的代码,而是更容易创建标签数组并循环遍历数组。为了将所有内容保持在一起,我创建了数组KeyPress事件。最好将数组作为表单中的Private字段,并在表单的Load事件中初始化它。代码使用名为good
的布尔值来记住按下的键是否与任何标签匹配。
Sub MainForm_KeyPress(sender As Object, e As KeyPressEventArgs)
Dim labels() As Label = {lblletter1, lblletter2, lblletter3, lblletter4, lblletter5, lblletter6}
If Not line1.Visible Then Exit Sub
speed += 1
Dim good As Boolean
For Each lab As Label In labels
If e.KeyChar = lab.Text(0) Then
score += 1
lblscore.Text = score.ToString
lab.Top = 35
Dim num As Integer = random.Next(97, 123) 'choose >= "a" And < "{"
lab.Text = Chr(num)
good = True
End If
Next
If Not good Then
score -= 1
lblscore.Text = score.ToString
incorrect += 1
Label2.Text = incorrect.ToString
If score < 0 Then counter -= 1
End If
End Sub