Привет всем, мне трудно найти правильный вопрос для формы вопроса. Допустим, у меня есть 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", "" & _
"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"}
Код кнопки Next / Complete выглядит следующим образом:
Private Sub cmdNextFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNextFinish.Click
Call theQs(currentQ)
End Sub
Form_load выглядит так:
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
Я, кажется, запутался, потому что, когда он дойдет до 5-го вопроса, он выдаст мне ошибку Индекс находится за пределами массива.
lblQ.Text = theQuestions(currentQ)
для theQNum = 5
Я знаю это по 5-му вопросу, но массив не поднимается до 6.
Что я здесь не так делаю (или думаю что-то простое)
Спасибо
David
ОБНОВЛЕНИЕ ПОЛУЧИЛО ЭТО
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