Vb.net 如果文本等于某个值,则缩短代码
我是一个初学者程序员,我正在尝试创建一个有问题的游戏(想成为百万富翁)。然而,不幸的是,我被卡住了一点,因为我想做的事情需要经常重写代码。我能让这更简单一点吗 第一个代码是带问题的 如果LbNumber.Text增加+1,则所有代码我的增加+1Vb.net 如果文本等于某个值,则缩短代码,vb.net,Vb.net,我是一个初学者程序员,我正在尝试创建一个有问题的游戏(想成为百万富翁)。然而,不幸的是,我被卡住了一点,因为我想做的事情需要经常重写代码。我能让这更简单一点吗 第一个代码是带问题的 如果LbNumber.Text增加+1,则所有代码我的增加+1 Private Sub BttStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BttStart.Click Dim Ra
Private Sub BttStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BttStart.Click
Dim Rand As New Random() 'Pentru extragerea unui ` Număr ` aleatoriu)
Dim Number = Rand.Next(1, CountQListBox.Text) 'Exemplu, Un Număr intre (1,26)
LBNumber.Text = Number 'In Label-ul nostru denumit, LBNumber, va apărea Numarul extras.
If LBNumber.Text = ("1") Then 'Dacă Numărul extras va fi 1
QuestionBox.Text = QListBoxLoad.Items(1) 'In TextBox-ul denumit, QuestionBox va apărea primul Item din ListBox 1.
QListBoxLoad.SelectedItem = QListBoxLoad.Items(1) 'Vom selecta astfel primul Item, din ListBox 1.
ElseIf LBNumber.Text = ("2") Then
QuestionBox.Text = QListBoxLoad.Items(2)
QListBoxLoad.SelectedItem = QListBoxLoad.Items(2)
ElseIf LBNumber.Text = ("3") Then
QuestionBox.Text = QListBoxLoad.Items(3)
QListBoxLoad.SelectedItem = QListBoxLoad.Items(3)
ElseIf LBNumber.Text = ("4") Then
QuestionBox.Text = QListBoxLoad.Items(4)
QListBoxLoad.SelectedItem = QListBoxLoad.Items(4)
ElseIf LBNumber.Text = ("5") Then
QuestionBox.Text = QListBoxLoad.Items(5)
QListBoxLoad.SelectedItem = QListBoxLoad.Items(5)
End If
End Sub
第二个是答案
如果QListBox.Items增加+1,则AnswerLoadFile.Lines增加+1。但是其他的没有改变价值
Private Sub QuestionBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuestionBox.TextChanged
If QuestionBox.Text = QListBoxLoad.Items(1) Then
Dim a As String : a = AnswerLoadFile.Lines(1)
Debug.Print(indexThroughString(a, 2))
Dim b As String : b = AnswerLoadFile.Lines(1)
Debug.Print(indexThroughString(b, 3))
Dim c As String : c = AnswerLoadFile.Lines(1)
Debug.Print(indexThroughString(c, 4))
Dim d As String : d = AnswerLoadFile.Lines(1)
Debug.Print(indexThroughString(d, 5))
AnswerA.Text = (indexThroughString(a, 2))
AnswerB.Text = (indexThroughString(b, 3))
AnswerC.Text = (indexThroughString(c, 4))
AnswerD.Text = (indexThroughString(d, 5))
ElseIf QuestionBox.Text = QListBoxLoad.Items(2) Then
Dim a As String : a = AnswerLoadFile.Lines(2)
Debug.Print(indexThroughString(a, 2))
Dim b As String : b = AnswerLoadFile.Lines(2)
Debug.Print(indexThroughString(b, 3))
Dim c As String : c = AnswerLoadFile.Lines(2)
Debug.Print(indexThroughString(c, 4))
Dim d As String : d = AnswerLoadFile.Lines(2)
Debug.Print(indexThroughString(d, 5))
AnswerA.Text = (indexThroughString(a, 2))
AnswerB.Text = (indexThroughString(b, 3))
AnswerC.Text = (indexThroughString(c, 4))
AnswerD.Text = (indexThroughString(d, 5))
ElseIf QuestionBox.Text = QListBoxLoad.Items(3) Then
Dim a As String : a = AnswerLoadFile.Lines(3)
Debug.Print(indexThroughString(a, 2))
Dim b As String : b = AnswerLoadFile.Lines(3)
Debug.Print(indexThroughString(b, 3))
Dim c As String : c = AnswerLoadFile.Lines(3)
Debug.Print(indexThroughString(c, 4))
Dim d As String : d = AnswerLoadFile.Lines(3)
Debug.Print(indexThroughString(d, 5))
AnswerA.Text = (indexThroughString(a, 2))
AnswerB.Text = (indexThroughString(b, 3))
AnswerC.Text = (indexThroughString(c, 4))
AnswerD.Text = (indexThroughString(d, 5))
ElseIf QuestionBox.Text = QListBoxLoad.Items(4) Then
Dim a As String : a = AnswerLoadFile.Lines(4)
Debug.Print(indexThroughString(a, 2))
Dim b As String : b = AnswerLoadFile.Lines(4)
Debug.Print(indexThroughString(b, 3))
Dim c As String : c = AnswerLoadFile.Lines(4)
Debug.Print(indexThroughString(c, 4))
Dim d As String : d = AnswerLoadFile.Lines(4)
Debug.Print(indexThroughString(d, 5))
AnswerA.Text = (indexThroughString(a, 2))
AnswerB.Text = (indexThroughString(b, 3))
AnswerC.Text = (indexThroughString(c, 4))
AnswerD.Text = (indexThroughString(d, 5))
ElseIf QuestionBox.Text = QListBoxLoad.Items(5) Then
Dim a As String : a = AnswerLoadFile.Lines(5)
Debug.Print(indexThroughString(a, 2))
Dim b As String : b = AnswerLoadFile.Lines(5)
Debug.Print(indexThroughString(b, 3))
Dim c As String : c = AnswerLoadFile.Lines(5)
Debug.Print(indexThroughString(c, 4))
Dim d As String : d = AnswerLoadFile.Lines(5)
Debug.Print(indexThroughString(d, 5))
AnswerA.Text = (indexThroughString(a, 2))
AnswerB.Text = (indexThroughString(b, 3))
AnswerC.Text = (indexThroughString(c, 4))
AnswerD.Text = (indexThroughString(d, 5))
End If
试试这个:
Private Sub BttStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BttStart.Click
Dim Rand As New Random()
Dim Number = Rand.Next(1, CountQListBox.Text)
LBNumber.Text = Number
QListBoxLoad.SelectedItem = QListBoxLoad.Items(Number)
QuestionBox.Text = QListBoxLoad.Items(Number)
End Sub
然后这个:
Private Sub QuestionBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuestionBox.TextChanged
Dim Number = QListBoxLoad.SelectedIndex
Dim a As String : a = AnswerLoadFile.Lines(Number)
Debug.Print(indexThroughString(a, 2))
Dim b As String : b = AnswerLoadFile.Lines(Number)
Debug.Print(indexThroughString(b, 3))
Dim c As String : c = AnswerLoadFile.Lines(Number)
Debug.Print(indexThroughString(c, 4))
Dim d As String : d = AnswerLoadFile.Lines(Number)
Debug.Print(indexThroughString(d, 5))
AnswerA.Text = (indexThroughString(a, 2))
AnswerB.Text = (indexThroughString(b, 3))
AnswerC.Text = (indexThroughString(c, 4))
AnswerD.Text = (indexThroughString(d, 5))
End Sub
试试这个:
Private Sub BttStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BttStart.Click
Dim Rand As New Random()
Dim Number = Rand.Next(1, CountQListBox.Text)
LBNumber.Text = Number
QListBoxLoad.SelectedItem = QListBoxLoad.Items(Number)
QuestionBox.Text = QListBoxLoad.Items(Number)
End Sub
然后这个:
Private Sub QuestionBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuestionBox.TextChanged
Dim Number = QListBoxLoad.SelectedIndex
Dim a As String : a = AnswerLoadFile.Lines(Number)
Debug.Print(indexThroughString(a, 2))
Dim b As String : b = AnswerLoadFile.Lines(Number)
Debug.Print(indexThroughString(b, 3))
Dim c As String : c = AnswerLoadFile.Lines(Number)
Debug.Print(indexThroughString(c, 4))
Dim d As String : d = AnswerLoadFile.Lines(Number)
Debug.Print(indexThroughString(d, 5))
AnswerA.Text = (indexThroughString(a, 2))
AnswerB.Text = (indexThroughString(b, 3))
AnswerC.Text = (indexThroughString(c, 4))
AnswerD.Text = (indexThroughString(d, 5))
End Sub
将
Dim Rand
更改为Static Rand
,或将其从本地移动到类/表单级别。将Dim Rand
更改为Static Rand
,或将其从本地移动到类/表单级别。