그림 참조 모든 가격 열을 추가하면 완벽하게 작동합니다. 그러나 두 번째 항목을 선택하면 첫 번째 행의 값만 표시되지만 첫 번째 항목을 선택하면 올바른 값이 표시됩니다. 어쨌든 내 코드는 다음과 같습니다.
Private Sub btnttotal_Click(sender As Object, e As EventArgs) Handles btnttotal.Click
Dim totalPrice As Integer = 0
Dim i As Integer = 0
Do While (i < ListView1.SelectedItems.Count)
totalPrice = (totalPrice + Convert.ToInt32(ListView1.Items(i).SubItems(2).Text))
i = (i + 1)
txttotal.Text = totalPrice
Loop
End Sub
이 시도 :
Private Sub btnttotal_Click(sender As Object, e As EventArgs) Handles btnttotal.Click
Dim totalPrice As Integer = 0
Dim i As Integer = 0
Do While (i < ListView1.SelectedItems.Count)
totalPrice = (totalPrice + Convert.ToInt32(ListView1.SelectedItems(i).SubItems(2).Text))
i = (i + 1)
txttotal.Text = totalPrice
Loop
End Sub
위의 솔루션을 살펴보면 합계를 계산하려면 선택한 값만 고려해야합니다. 그러나 당신은이 줄로리스트 박스의 모든 행을 계산하고있었습니다 totalPrice = (totalPrice + Convert.ToInt32(ListView1.Items(i).SubItems(2).Text))
. 따라서 두 번째 행을 선택 DO WHILE
하면 선택한 행이 하나이므로 계산이 100
처음 부터 값을 선택 하고 첫 번째 값이고 그로 중지 되므로 한 번만 반복됩니다 . 나는 당신이 실수를 이해하기를 바랍니다.
계산을 효율적이고 간단하게 수행하려면 다음을 제안합니다.
Dim totalPrice As Integer = 0
For Each item As ListViewItem In ListView1.SelectedItems.Cast(Of ListViewItem)()
totalPrice += Convert.ToInt32(item.SubItems(2).Text)
Next
txttotal.Text = totalPrice
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다