2012-01-11 1 views
0

Salut à tous, j'ai du mal à trouver le bon flux pour un formulaire de question. Disons que j'ai 6 questions. Je les ai définis comme ceci:Poser des questions dans un tableau

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"} 

suivant/code du bouton complet ressemble à ceci:

Private Sub cmdNextFinish_Click(ByVal sender As System.Object, ByVal e As    System.EventArgs) Handles cmdNextFinish.Click 
    Call theQs(currentQ) 
End Sub 

Le form_load ressemble à ceci:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Call theQs(0) 
End Sub 

Et maintenant la configuration de question ressemble ce:

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 

Je semble être confus parce que quand il arrive à la question 5, il me donne une erreur de Index était en dehors des limites de la matrice. sur la ligne de

lblQ.Text = theQuestions(currentQ) 

pour theQNum = 5

Je sais que le 5 question, mais le tableau ne va pas jusqu'à 6.

ce que je fais mal ici (ou overthinking quelque chose de simple)

Merci,

David

MISE A JOUR 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 

David

Répondre

0

tableaux commence à 0 partir. SO, votre première question est à l'index 0 & sixième question est à l'index 5 dans le tableau. Si vous augmentez l'indice au préalable pour la dernière question, l'index devient 6, ce qui n'a aucune valeur.

+0

Toujours l'erreur de ** L'index était en dehors des limites du tableau ** Ce nombre doit être 1 en avance puisque le QNum son sur (disons 0) doit charger la question suivante (1) après que l'utilisateur appuie sur NEXT . – StealthRT

+0

pourquoi augmentez-vous la valeur de currentQ. La valeur de currentQ deviendra 6.Le tableau commence à partir de 0, ce qui signifie que la sixième question est theQuestions (5) – Harsh

+0

Parce que c'est ce qui est rempli lorsque le bouton est appuyé, le bouton NEXT sera incrémenté de 1 chaque tour jusqu'à ce qu'il obtienne la dernière question où COMPLETE serait sur le bouton. – StealthRT

1

Ces lignes:

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

qui sont à l'intérieur du Si pour votre ElseIf theQNum = 5 Then sont ce qui est en vous faisant sortir des limites. Mais le problème est beaucoup plus tôt dans votre si. Vous commencez avec

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 

Ce ElseIf theQNum = 0 ne sera pas appelé à la première course, parce que la première partie du cas est vrai. Et dans ce premier bit de code quand firstStart est vrai, vous n'incrémentez pas currentQ, donc la prochaine fois que ce sous-élément est appelé firststart est faux, mais currentQ est toujours 0. La racine de votre problème.

La solution simple est d'incrémenter votre currentQ dans le premier bit de votre if et de toujours définir le texte de l'étiquette AVANT vous incrémentez currentQ. Et ne pas incrémenter quand vous êtes à votre dernière question. De cette façon, currentQ ne sera jamais hors de portée.

Questions connexes