嘿,我很難爲問題表單提出正確的流程。可以說我有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"}
下一個/結束按鈕的代碼如下所示:
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
我似乎感到困惑,因爲當它到達第五個問題時,它給了我一個錯誤索引超出了數組的範圍。上的
lblQ.Text = theQuestions(currentQ)
爲行theQNum = 5
我知道它的5號問題,但陣列未上升到6
我在做什麼錯在這裏(得太多或者簡單的東西)
感謝,
大衛
UPDATE GOT IT
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
大衛
仍然給出錯誤**索引超出了數組的範圍**該數字需要提前1次,因爲QNum的開啓(比如0)需要在用戶按下NEXT後加載下一個問題(1) 。 – StealthRT 2012-01-11 08:04:04
你爲什麼要增加currentQ的值。 currentQ的值將變爲6。數組從0開始,這意味着第六個問題是問題(5) – Harsh 2012-01-11 08:07:12
因爲那是按鈕被按下時所填充的內容,所以NEXT按鈕將被感興趣的每一個都轉過來,直到它獲得了COMPLETE所在的最後一個問題在按鈕上。 – StealthRT 2012-01-11 08:19:53