Private Function FindSubItem(ByVal lv As ListView, ByVal SearchString As String) As Boolean
Dim idx = (From c In ListView1.Columns Where c.Text = "รหัสสี" Select c = c.Index).First()
For Each itm As ListViewItem In lv.Items
If itm.SubItems(idx).Text = SearchString Then Return True
Next
'Return False
End Function
Code (VB.NET)
Try
Dim articlecheck As String = txtMixID.Text
If FindSubItem(ListView1, articlecheck) = True Then
For Each itms As ListViewItem In ListView1.Items
If txtMixID.Text = articlecheck Then
Dim tmpAmount As Integer = 0
Dim tmpce As Integer
tmpAmount = itms.SubItems(3).Text
tmpce = CInt(tmpAmount + Val(txtAmountPay.Text))
itms.SubItems(3).Text = CStr(tmpce)
Exit Sub
End If
Next
For i = 0 To ListView1.Items.Count - 1
If txtMixID.Text = articlecheck Then
Dim tmpAmount As Integer = 0
'Dim Math As Double
Dim tmpce As Integer
'Dim TotalMath As Double
tmpAmount = CInt(CDbl(ListView1.Items(i).SubItems(3).Text))
tmpce = CInt(tmpAmount + Val(txtAmount.Text))
ListView1.Items(i).SubItems(3).Text = CStr(tmpce)
Exit Sub
End If
Next
Else
Dim arr As String() = New String(5) {}
Dim itm As ListViewItem
arr(0) = txtMixID.Text
arr(1) = txtProductName.Text
arr(2) = txtPrice.Text
arr(3) = txtAmountPay.Text
arr(4) = CInt(txtBalance.Text) - CInt(arr(3)) 'Balance
arr(5) = CDbl(txtPriceSpec.Text) * CInt(txtAmountPay.Text)
itm = New ListViewItem(arr)
ListView1.Items.Add(itm)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try