I have 4 listboxes named: courseNameListBox, gradeListBox, creditsListBox, and qualityPointsListBox. Elsewhere in my program, a total course average is calculated and then courseName, grade, credits, and qualitypoints for that course are transferred into their respective ListBoxes. I'm trying to make it so that if the user edits a particular course, like adding more grades for example, when they transfer that new information to the afore mentioned Listboxes, if the courseName already exists, it should replace the same line in all 4 ListBoxes with the new data from that particular course instead of adding a new line for the same course. I hope that makes sense.

I can find and replace the courseNameListBox.item just fine, but I'm having a real difficult time figuring out how to connect the other 3 ListBox lines to courseNameListBox.item line.

  ElseIf courseNameListBox.Items.Contains(courseName) Then

     dim courseName as string = nameOfCourseTextBox.Text
     dim credits as integer = 0
     dim average as decimal = 0
     credits = Val(creditsTextBoxt.Text)
     average = Val(averageResultLabel.text)

     For i = 0 To courseNameListBox.Items.Count - 1
       If courseNameListBox.Items(i).ToString.Contains(courseName) Then
         courseNameListBox.Items(i) = courseNameListBox.Items(i).ToString.Replace(courseName, courseName)
       End If
     Next

  Else

        ' add course Name to course Name ListBox
        courseNameListBox.Items.Add(nameOfCourseTextBox.Text)

        ' add credits to credits ListBox
        creditsListBox.Items.Add(creditsTextBox.Text)

        Select Case average
           Case Is >= A
              gradeListBox.Items.Add("A")
           Case Is >= B
              gradeListBox.Items.Add("B")
           Case Is >= C
              gradeListBox.Items.Add("C")
           Case Is >= D
              gradeListBox.Items.Add("D")
           Case Is <= F
              gradeListBox.Items.Add("F")
        End Select

        Select Case average
           Case Is >= A
              qualityPointsListBox.Items.Add(credits * Val(aLetterGradeQualityPointsTextBox.Text))
           Case Is >= B
              qualityPointsListBox.Items.Add(credits * Val(bLetterGradeQualityPointsTextBox.Text))
           Case Is >= C
              qualityPointsListBox.Items.Add(credits * Val(cLetterGradeQualityPointsTextBox.Text))
           Case Is >= D
              qualityPointsListBox.Items.Add(credits * Val(dLetterGradeQualityPointsTextBox.Text))
           Case Is <= F
              qualityPointsListBox.Items.Add(credits * Val(fLetterGradeQualityPointsTextBox.Text))
        End Select
  End If

I have tried, among many other things:

     dim courseName as string = nameOfCourseTextBox.Text
     dim credits as integer = 0
     dim average as decimal = 0
     credits = Val(creditsTextBoxt.Text)
     average = Val(averageResultLabel.text)

     For i = 0 To courseNameListBox.Items.Count - 1
       If courseNameListBox.Items(i).ToString.Contains(courseName) Then
         courseNameListBox.Items(i) = courseNameListBox.Items(i).ToString.Replace(courseName, courseName)
       End If
     Next
     For C = 0 To creditsListBox.Items.Count - 1
        creditsListBox.Items(C) = creditsListBox.Items(C).ToString.Replace(credits, credits)
     Next

But that just replaces every line in the creditsListBox with the new credits value instead of just the line with the same courseName. So, how can I connect the line with the specified courseName in the courseNameListBox with the same line in the other 3 ListBoxes?

有帮助吗?

解决方案

I got it! Here's the code:

  ' check if courseName is already in courseNameListBox 
  ElseIf courseNameListBox.Items.Contains(courseName) Then

  ' declare variables
  Dim average As Decimal = 0
  Dim courseName As String
  Dim credits As Integer = 0
  Dim A As Integer
  Dim B As Integer
  Dim C As Integer
  Dim D As Integer
  Dim F As Integer

  ' assign user input to variables
  average = Val(averageResultLabel.Text)
  courseName = nameOfCourseTextBox.Text
  credits = Val(creditsTextBox.Text)
  A = Val(aTextBox.Text)
  B = Val(bTextBox.Text)
  C = Val(cTextBox.Text)
  D = Val(dTextBox.Text)
  F = Val(fTextBox.Text)

     For i = 0 To courseNameListBox.Items.Count - 1
        If courseNameListBox.Items(i).ToString.Contains(courseName) Then

           ' replace courseNameListBox line item with nameOfCourseTextBoxt.Text
           courseNameListBox.Items(i) = courseNameListBox.Items(i).ToString.Replace(courseNameListBox.Items(i), courseName)

           ' replace creditsListBox line item with creditsTextBox.Text
           creditsListBox.Items(i) = creditsListBox.Items(i).ToString.Replace(creditsListBox.Items(i), credits)

           ' replace gradeListBox line item with grade
           Select Case average
              Case Is >= A
                 gradeListBox.Items(i) = gradeListBox.Items(i).ToString.Replace(gradeListBox.Items(i), "A")
              Case Is >= B
                 gradeListBox.Items(i) = gradeListBox.Items(i).ToString.Replace(gradeListBox.Items(i), "B")
              Case Is >= C
                 gradeListBox.Items(i) = gradeListBox.Items(i).ToString.Replace(gradeListBox.Items(i), "C")
              Case Is >= D
                 gradeListBox.Items(i) = gradeListBox.Items(i).ToString.Replace(gradeListBox.Items(i), "D")
              Case Is <= F
                 gradeListBox.Items(i) = gradeListBox.Items(i).ToString.Replace(gradeListBox.Items(i), "F")
           End Select

           ' replace qualityPointsListBox line item with calculated qualityPoints
           Select Case average
              Case Is >= A
                 qualityPointsListBox.Items(i) = qualityPointsListBox.Items(i).ToString.Replace(qualityPointsListBox.Items(i), credits * Val(aLetterGradeQualityPointsTextBox.Text))
              Case Is >= B
                 qualityPointsListBox.Items(i) = qualityPointsListBox.Items(i).ToString.Replace(qualityPointsListBox.Items(i), credits * Val(bLetterGradeQualityPointsTextBox.Text))
              Case Is >= C
                 qualityPointsListBox.Items(i) = qualityPointsListBox.Items(i).ToString.Replace(qualityPointsListBox.Items(i), credits * Val(cLetterGradeQualityPointsTextBox.Text))
              Case Is >= D
                 qualityPointsListBox.Items(i) = qualityPointsListBox.Items(i).ToString.Replace(qualityPointsListBox.Items(i), credits * Val(dLetterGradeQualityPointsTextBox.Text))
              Case Is <= F
                 qualityPointsListBox.Items(i) = qualityPointsListBox.Items(i).ToString.Replace(qualityPointsListBox.Items(i), credits * Val(fLetterGradeQualityPointsTextBox.Text))
           End Select
        End If
     Next
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top