2012-01-11 16 views
0

嘿,我很難爲問題表單提出正確的流程。可以說我有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 

大衛

回答

0

數組從0開始起。所以,你的第一個問題是在索引0 &第六個問題是在索引5列陣。如果事先爲最後一個問題增加索引,那麼索引變爲6,這沒有任何價值。

+0

仍然給出錯誤**索引超出了數組的範圍**該數字需要提前1次,因爲QNum的開啓(比如0)需要在用戶按下NEXT後加載下一個問題(1) 。 – StealthRT 2012-01-11 08:04:04

+0

你爲什麼要增加currentQ的值。 currentQ的值將變爲6。數組從0開始,這意味着第六個問題是問題(5) – Harsh 2012-01-11 08:07:12

+0

因爲那是按鈕被按下時所填充的內容,所以NEXT按鈕將被感興趣的每一個都轉過來,直到它獲得了COMPLETE所在的最後一個問題在按鈕上。 – StealthRT 2012-01-11 08:19:53

1

這些行:

If theQNum <> totalQs Then 
      currentQ = currentQ + 1 
      lblQ.Text = theQuestions(currentQ) 

哪些是裏面如果您ElseIf theQNum = 5 Then是什麼原因導致你得到出界。但是,如果你的問題早得多。你開始

If firstStart = True And theQNum = 0 Then 
     ' Code here 
ElseIf theQNum = 0 Then 'ANSWERING THE FIRST QUESTION 
     'More code here that doesn't get called 

這elseif的theQNum = 0將不會在第一次運行時被調用,因爲如果是真正的第一部分。在firstStart爲true的情況下,在第一部分代碼中,不會遞增currentQ,因此下次調用firststart時該值爲false,但currentQ仍然爲0.問題的根源。

簡單的修復方法是在您的if的第一位內增加currentQ並始終設置標籤文本之前您遞增currentQ。當你最後一個問題時不要增加它。這樣currentQ永遠不會超出範圍。