Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if item exists in LIST View before adding new item. 1

Status
Not open for further replies.

ftpdoo

Programmer
Aug 9, 2001
202
GB
Hi,

I've got a ListView called "lvwAddToGroup"

Before I add an item to this list view I wish to check if it already exists in that list view.

The item I'm check for it stored in "strFunctionCode".

Any idea's??
Thankx,
Jonathan
 
Hi,

Something like:


---------------------------------------------
Dim i as integer, AddItem as boolean

AddItem = True
For i = 0 to MyList.listcount-1
MyList(i) = strFunctionCode then AddItem = False
next i

If Additem then
MyList.Additem strFunctionCode
end if
---------------------------------------------


Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
One or two small twiks & this worked great!! :eek:)
Thnx,
Jonathan

if ur interested:
=======================================================
Dim i As Integer, AddItem As Boolean

AddItem = True
For i = 0 To lvwAddToGroup.ListItems.Count - 1
i = i + 1
If lvwAddToGroup.ListItems(i) = strFunctionCode Then
AddItem = False
i = i - 1
End If
Next i

If AddItem = True Then
lvwAddToGroup.ListItems.Add intIndex, , strFunctionCode
lvwFunctionCode.ListItems.Remove (intDeleteItem)
End If
 
Hi,

Are you sure?
I'm not really into your list structure, but adding to the counter inside a for loop is generally confusing.
It looks like your loop would jump every 2nd item in the list (because both 'i=i+1' and 'next i' adds 1 to i).

What about
i=0
do
If lvwAddToGroup.ListItems(i) = strFunctionCode Then
AddItem = False
Exit do
End If
i=i+1
loop until i=lvwAddToGroup.ListItems.Count

:cool: Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Yep - Your right.. It does jump every second item. Got that resolved now.

Thnx (Again)
Jonathan
 
My problem is the fact that you are looping around every item in the list every time you want to add one !!

Why not Have a function as follows:

Public Function ListNodeExists(LstView As listView, strKey As String) As Boolean

Dim strTmp As String

On Error GoTo ListNodeExists_Err

ListNodeExists = False

strTmp = LstView .Nodes(strKey).Text

ListNodeExists= True

ListNodeExists_Exit:
Exit Function

ListNodeExists_Err:
Resume ListNodeExists_Exit
End Function

When you add the item "strFunctionCode" add it as the key to the listitem.

Exactly the same can be done with treeViews
Hope This helps
 
Well for all of the friends who are working with loops, please note one thing in your code and that is that your code keeps on looping to the end of the list, in this case let us assume we have 4 items and the second item is the one which you are trying to insert, the found flag will be true but with the next iteration it will become false again, so you need to break out of loop if you find the item in the list and then the code in the top solution will work alright like this

dim fnd as boolean
dim strToSrch as string 'whatever
fnd = false
for lp = 0 to list1.listcount - 1
if list1.list(lp) = strToSrch then
fnd = true
exit for
end if
next

if fnd = true then
list1.additem strToSrch
end if

Hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top