|
-
Feb 14th, 2009, 12:27 PM
#1
Thread Starter
Fanatic Member
Re: Loading Listview Problems.........
NP, I have added a check to verify that the listitem the user is trying to add to the list is not already in the listview, but now the item does not add at all. Maybe this is another VB6 vs. .NET thing?
Code:
Private Sub btnAddSurgeProcedure(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSurgProcedure.Click
Dim lvItem As New ListViewItem, strSurgProcedure As String, strSurgPerformed As String, strSurgName As String, strSurgPhone As String, strSurgFacility As String, strSurgPostOp As String, i As Integer
'remove leading and trailing spaces from the entries and store as a variable
strSurgProcedure = Trim(txtSurgProcedure.Text)
strSurgPerformed = Trim(dtpSurgPerformed.Text)
strSurgName = Trim(txtSurgName.Text)
strSurgPhone = Trim(txtSurgPhone.Text)
strSurgFacility = Trim(txtSurgFacility.Text)
strSurgPostOp = Trim(txtSurgPostOp.Text)
'set lvitem to the value of the SurgProcedure string
lvItem.Text = (strSurgProcedure)
'Set the value of the lvitem subitems
With lvItem
.SubItems.Add(strSurgPerformed)
.SubItems.Add(strSurgName)
.SubItems.Add(strSurgPhone)
.SubItems.Add(strSurgFacility)
.SubItems.Add(strSurgPostOp)
End With
'check to see if the procedure already exists in the listview
For i = 1 To lvSurgProcedures.Items.Count
If strSurgProcedure = lvSurgProcedures.Items(i).Text Then
'
MessageBox.Show("The Surgical Prodcedure you are trying to add is already in the list")
Else
'add the lvitem to the listview
lvSurgProcedures.Items.Add(lvItem)
End If
Next
-
Feb 14th, 2009, 01:17 PM
#2
Re: Loading Listview Problems.........
The For loop has to start at zero since the list is zero based.
Code:
For i = 0 To lvSurgProcedures.Items.Count - 1
-
Feb 14th, 2009, 01:47 PM
#3
Thread Starter
Fanatic Member
Re: Loading Listview Problems.........
Ok, I've got a validation check in place now and everything is working. Here is my updated code
Code:
Private Sub btnAddSurgeProcedure(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSurgProcedure.Click
Dim lvItem As New ListViewItem, strSurgProcedure As String, strSurgPerformed As String, strSurgName As String, strSurgPhone As String, strSurgFacility As String, strSurgPostOp As String, i As Integer
'remove leading and trailing spaces from the entries and store as a variable
strSurgProcedure = Trim(txtSurgProcedure.Text)
strSurgPerformed = Trim(dtpSurgPerformed.Text)
strSurgName = Trim(txtSurgName.Text)
strSurgPhone = Trim(mtxtSurgPhone.Text)
strSurgFacility = Trim(txtSurgFacility.Text)
strSurgPostOp = Trim(txtSurgPostOp.Text)
'set lvitem to the value of the SurgProcedure string
lvItem.Text = (strSurgProcedure)
'Set the value of the lvitem subitems
With lvItem
.SubItems.Add(strSurgPerformed)
.SubItems.Add(strSurgName)
.SubItems.Add(strSurgPhone)
.SubItems.Add(strSurgFacility)
.SubItems.Add(strSurgPostOp)
End With
'if there are no items in the listview, don't check for duplicates
If lvSurgProcedures.Items.Count = 0 Then
'add the lvitem to the listview
lvSurgProcedures.Items.Add(lvItem)
Exit Sub
Else
'check to see if the procedure already exists in the listview
For i = 0 To lvSurgProcedures.Items.Count - 1
If strSurgProcedure = lvSurgProcedures.Items(i).Text Then
'
MessageBox.Show("The Surgical Prodcedure you are trying to add is already in the list")
Else
'add the lvitem to the listview
lvSurgProcedures.Items.Add(lvItem)
End If
Next
End If
End Sub
I can add 2 items to the listview and if I enter a duplicate, it throws up the message box. However, when I attempt to add the 3rd item to the listview I get an error on the line in red in the above code. The error is
Code:
'System.ArgumentException' occurred in System.Windows.Forms.dll
-
Feb 14th, 2009, 05:13 PM
#4
Re: Loading Listview Problems.........
That's because adding a new item changes the Items.Count. Change you code as below.
Code:
Dim AlreadyPresent As Boolean = False
For i = 0 To lvSurgProcedures.Items.Count - 1
If strSurgProcedure = lvSurgProcedures.Items(i).Text Then
MessageBox.Show("The Surgical Prodcedure you are trying to add is already in the list")
AlreadyPresent = True
Exit For
End If
Next
If Not AlreadyPresent Then lvSurgProcedures.Items.Add(lvItem)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|