Vb.net 连串提问
嘿,所有我有一个困难的时间来正确的流程为一个问题的形式。假设我有6个问题。我这样定义它们:Vb.net 连串提问,vb.net,arrays,arraylist,Vb.net,Arrays,Arraylist,嘿,所有我有一个困难的时间来正确的流程为一个问题的形式。假设我有6个问题。我这样定义它们: Dim firstStart As Boolean = True Dim totalQs As Integer = 0 Dim currentQ As Integer = 0 Dim theAnswers() As String Dim theQuestions() As String = {"Please scan barcode 1 then press NEXT", "" & _
Dim firstStart As Boolean = True
Dim totalQs As Integer = 0
Dim currentQ As Integer = 0
Dim theAnswers() As String
Dim theQuestions() As String = {"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press COMPLETE"}
Private Sub cmdNextFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNextFinish.Click
Call theQs(currentQ)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call theQs(0)
End Sub
Private Sub theQs(ByRef theQNum As Integer)
If firstStart = True And theQNum = 0 Then
firstStart = False
totalQs = (theQuestions.Length)
ReDim theAnswers(totalQs)
lblQ.Text = theQuestions(0)
cmdNextFinish.Enabled = True
cmdNextFinish.Text = "NEXT"
Call buttons(theQNum)
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
ElseIf theQNum = 0 Then 'ANSWERING THE FIRST QUESTION
theAnswers(currentQ) = "-" & txtNumber.Text
If currentQ <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 1 Then 'ANSWERING THE SECOND QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 2 Then 'ANSWERING THE THIRD QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 3 Then 'ANSWERING THE FORTH QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 4 Then 'ANSWERING THE FIFTH QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 5 Then 'ANSWERING THE SIXTH QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
End If
End Sub
下一个/完成按钮代码如下所示:
Dim firstStart As Boolean = True
Dim totalQs As Integer = 0
Dim currentQ As Integer = 0
Dim theAnswers() As String
Dim theQuestions() As String = {"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press COMPLETE"}
Private Sub cmdNextFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNextFinish.Click
Call theQs(currentQ)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call theQs(0)
End Sub
Private Sub theQs(ByRef theQNum As Integer)
If firstStart = True And theQNum = 0 Then
firstStart = False
totalQs = (theQuestions.Length)
ReDim theAnswers(totalQs)
lblQ.Text = theQuestions(0)
cmdNextFinish.Enabled = True
cmdNextFinish.Text = "NEXT"
Call buttons(theQNum)
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
ElseIf theQNum = 0 Then 'ANSWERING THE FIRST QUESTION
theAnswers(currentQ) = "-" & txtNumber.Text
If currentQ <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 1 Then 'ANSWERING THE SECOND QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 2 Then 'ANSWERING THE THIRD QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 3 Then 'ANSWERING THE FORTH QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 4 Then 'ANSWERING THE FIFTH QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 5 Then 'ANSWERING THE SIXTH QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
End If
End Sub
表单加载如下所示:
Dim firstStart As Boolean = True
Dim totalQs As Integer = 0
Dim currentQ As Integer = 0
Dim theAnswers() As String
Dim theQuestions() As String = {"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press COMPLETE"}
Private Sub cmdNextFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNextFinish.Click
Call theQs(currentQ)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call theQs(0)
End Sub
Private Sub theQs(ByRef theQNum As Integer)
If firstStart = True And theQNum = 0 Then
firstStart = False
totalQs = (theQuestions.Length)
ReDim theAnswers(totalQs)
lblQ.Text = theQuestions(0)
cmdNextFinish.Enabled = True
cmdNextFinish.Text = "NEXT"
Call buttons(theQNum)
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
ElseIf theQNum = 0 Then 'ANSWERING THE FIRST QUESTION
theAnswers(currentQ) = "-" & txtNumber.Text
If currentQ <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 1 Then 'ANSWERING THE SECOND QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 2 Then 'ANSWERING THE THIRD QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 3 Then 'ANSWERING THE FORTH QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 4 Then 'ANSWERING THE FIFTH QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 5 Then 'ANSWERING THE SIXTH QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
End If
End Sub
现在问题设置如下所示:
Dim firstStart As Boolean = True
Dim totalQs As Integer = 0
Dim currentQ As Integer = 0
Dim theAnswers() As String
Dim theQuestions() As String = {"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press NEXT", "" & _
"Please scan barcode 1 then press COMPLETE"}
Private Sub cmdNextFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNextFinish.Click
Call theQs(currentQ)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call theQs(0)
End Sub
Private Sub theQs(ByRef theQNum As Integer)
If firstStart = True And theQNum = 0 Then
firstStart = False
totalQs = (theQuestions.Length)
ReDim theAnswers(totalQs)
lblQ.Text = theQuestions(0)
cmdNextFinish.Enabled = True
cmdNextFinish.Text = "NEXT"
Call buttons(theQNum)
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
ElseIf theQNum = 0 Then 'ANSWERING THE FIRST QUESTION
theAnswers(currentQ) = "-" & txtNumber.Text
If currentQ <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 1 Then 'ANSWERING THE SECOND QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 2 Then 'ANSWERING THE THIRD QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 3 Then 'ANSWERING THE FORTH QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 4 Then 'ANSWERING THE FIFTH QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
ElseIf theQNum = 5 Then 'ANSWERING THE SIXTH QUESTION
theAnswers(currentQ) = txtNumber.Text
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
If (totalQs - currentQ) = 0 Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
txtNumber.Text = ""
txtNumber.MaxLength = theQuestionTextboxLimits(theQNum)
txtNumber.Focus()
Call buttons(currentQ)
Else
'Call writeXMLFile()
MsgBox("exited")
End If
End If
End Sub
对于qnum=5
我知道是在第五个问题上,但数组没有上升到6
我在这里做错了什么(或者过度思考了一些简单的事情)
谢谢
大卫
更新得到了它
Private theQNum As Integer
Sub Start
theQNum =0
SetupNextQuestion
End Sub
Sub SetupNextQuestion
txtNumber.Text = ""
lblQuestion.Text = theQuestions(theQNum)
If theQNum = (theQuestions.Length - 1) Then
cmdNextFinish.Text = "Complete"
Else
cmdNextFinish.Text = "NEXT"
End If
End Sub
Sub cmdNextFinish_Click
theAnswers(theQNum) = txtNumber.Text
'Check if this is a finish
theQNum += 1
If theQNum >= theQuestions.Length Then
'Call writeXMLFile()
MsgBox("exited")
Else
SetupNextQuestion
End If
End Sub
David数组从0开始。第一个问题在索引0处,第六个问题在数组中的索引5处。如果您提前增加最后一个问题的索引,则索引将变为6,没有任何值 这些行:
If theQNum <> totalQs Then
currentQ = currentQ + 1
lblQ.Text = theQuestions(currentQ)
在第一次运行时不会调用该ElseIf theQNum=0,因为if的第一部分为true。当firstStart为true时,在代码的第一位中,不增加currentQ,所以下次调用firstStart时,currentQ为false,但currentQ仍然为0。你问题的根源
简单的解决方法是在if的第一位内增加currentQ,并在增加currentQ之前始终设置标签文本。当你回答最后一个问题时,不要增加它。这样,currentQ将永远不会超出范围。仍然给出索引的错误超出了数组的界限,该数字需要提前1,因为QNUM its on(假设为0)需要在用户按下next后加载下一个问题(1)。为什么要增加currentQ的值。currentQ的值将变为6。数组从0开始,这意味着第六个问题是问题(5),因为这是按下按钮时填充的内容,因此下一个按钮将以1递增,每次循环,直到它得到最后一个问题,其中完整的问题将出现在按钮上。