使用vb.net替换多个ListBox中的同一行

时间:2014-04-27 16:55:38

标签: vb.net listbox

我有4个名为courseNameListBoxgradeListBoxcreditsListBoxqualityPointsListBox的列表框。在我的课程的其他地方,计算总课程平均值,然后将该课程的课程名称,成绩,学分和质量要点转移到各自的列表框中。我尝试制作它,以便如果用户编辑特定课程,例如添加更多成绩,当他们将新信息传输到上述列表框时,如果courseName已经存在,则应该替换相同的行在所有4个ListBox中包含来自该特定课程的新数据,而不是为同一课程添加新行。我希望这是有道理的。

我可以很好地找到并替换courseNameListBox.item,但是我很难确定如何将其他3个ListBox行连接到courseNameListBox.item行。

  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

我已经尝试了许多其他事情:

     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

但是,这只会将creditsListBox中的每一行替换为新的credits值,而不仅仅是具有相同courseName的行。那么,如何将courseName中指定courseNameListBox的行与其他3个ListBox中的相同行连接起来?

1 个答案:

答案 0 :(得分:0)

我明白了!这是代码:

  ' 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