msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

vb6 listview control existing listitems

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB General Discussions
Author Message
wb



Joined: 04 Oct 2007
Posts: 10

PostPosted: Sat Feb 23, 2008 2:48 pm    Post subject: vb6 listview control existing listitems Reply with quote

Users can "add" items to the listview with the click of a button, but I want
to prevent duplicate items from being added.

I understand I can loop through each item when a new item is being added but
that doesn't seem very efficient (O^n right?). Is there a way to search on
the key value since that is what should be unique within each item?

WB

Archived from group: microsoft>public>vb>general>discussion
Back to top
View user's profile Send private message
Ivar



Joined: 04 Oct 2007
Posts: 28

PostPosted: Sat Feb 23, 2008 11:04 pm    Post subject: Re: vb6 listview control existing listitems Reply with quote

"wb" wrote in message news:%233tEFRkdIHA.4844@TK2MSFTNGP04.phx.gbl...
> Users can "add" items to the listview with the click of a button, but I
> want to prevent duplicate items from being added.
>
> I understand I can loop through each item when a new item is being added
> but that doesn't seem very efficient (O^n right?). Is there a way to
> search on the key value since that is what should be unique within each
> item?
>
> WB

Not 'Should be unique', it's has to be unique. So if you tried to add a new
listitem with a key that is already in the list you would get an error
stating 'Key is not unique in collection'.
Are you asking about the listitem Text property?

Ivar
Back to top
View user's profile Send private message
wb



Joined: 04 Oct 2007
Posts: 10

PostPosted: Sun Feb 24, 2008 3:46 pm    Post subject: Re: vb6 listview control existing listitems Reply with quote

I do recognize the key MUST be unique. That is what I want to search on. I
am asking if there is a more efficient method than a loop.

WB

"Ivar" wrote in message $Z_2.13@newsfe4-win.ntli.net...
>
> "wb" wrote in message
> news:%233tEFRkdIHA.4844@TK2MSFTNGP04.phx.gbl...
>> Users can "add" items to the listview with the click of a button, but I
>> want to prevent duplicate items from being added.
>>
>> I understand I can loop through each item when a new item is being added
>> but that doesn't seem very efficient (O^n right?). Is there a way to
>> search on the key value since that is what should be unique within each
>> item?
>>
>> WB
>
> Not 'Should be unique', it's has to be unique. So if you tried to add a
> new listitem with a key that is already in the list you would get an error
> stating 'Key is not unique in collection'.
> Are you asking about the listitem Text property?
>
> Ivar
>
Back to top
View user's profile Send private message
MikeD



Joined: 04 Oct 2007
Posts: 3348

PostPosted: Sun Feb 24, 2008 6:56 pm    Post subject: Re: vb6 listview control existing listitems Reply with quote

"wb" wrote in message @TK2MSFTNGP04.phx.gbl...
>I do recognize the key MUST be unique. That is what I want to search on.
>I am asking if there is a more efficient method than a loop.
>

You're missing the point. If there's already a ListItem object with that
key, you'll get a trappable error. No need to loop through anything. Of
course, this depends on what you're using for keys. If adding a duplicate
ListItem would use a different key than the existing ListItem has, this
won't do you any good.

--
Mike
Microsoft MVP Visual Basic
Back to top
View user's profile Send private message
"Jan Hyde



Joined: 04 Oct 2007
Posts: 466

PostPosted: Mon Feb 25, 2008 1:13 pm    Post subject: Re: vb6 listview control existing listitems Reply with quote

"wb" 's wild thoughts were released on Sun, 24 Feb
2008 10:46:56 -0800 bearing the following fruit:

>I do recognize the key MUST be unique. That is what I want to search on. I
>am asking if there is a more efficient method than a loop.

And if you read his post again then you will realise he has
answered exactly that question.

J

>WB
>
>"Ivar" wrote in message
>$Z_2.13@newsfe4-win.ntli.net...
>>
>> "wb" wrote in message
>> news:%233tEFRkdIHA.4844@TK2MSFTNGP04.phx.gbl...
>>> Users can "add" items to the listview with the click of a button, but I
>>> want to prevent duplicate items from being added.
>>>
>>> I understand I can loop through each item when a new item is being added
>>> but that doesn't seem very efficient (O^n right?). Is there a way to
>>> search on the key value since that is what should be unique within each
>>> item?
>>>
>>> WB
>>
>> Not 'Should be unique', it's has to be unique. So if you tried to add a
>> new listitem with a key that is already in the list you would get an error
>> stating 'Key is not unique in collection'.
>> Are you asking about the listitem Text property?
>>
>> Ivar
>>
>

--
Jan Hyde

https://mvp.support.microsoft.com/profile/Jan.Hyde
Back to top
View user's profile Send private message
Desi



Joined: 04 Oct 2007
Posts: 49

PostPosted: Mon Feb 25, 2008 2:12 pm    Post subject: Re: vb6 listview control existing listitems Reply with quote

What you might not be aware of WB is that _someone_ is looping, wether it's
you or VB, someone has to loop through the collection of ListItem objects to
find a match. Now then, you can let VB do the search by giving it either an
Index value (LVControl.ListItems(IndexValueHere), or by Key value
(LVControl.ListItems(YourKeyValueHere). In either instance VB kindly returns
a corresponding ListItem Object if there is one, or an error.

Set LstItmObj = LVControl.ListItems(IndexValueHere)
Set LstItmObj = LVControl.ListItems(YourKeyValueHere)

On the other hand, you can perform the search your self should you care to.
In doing that you can search based upon any number of criteria Index, Key,
Tag, Checked, Selected, etc. Either method is common prctice. I have found a
For Each loop to be more user friendly as it does not return an error when
there is no match or zero ListItem objects in the control's current
collection. For each will also allow you to build a list of more than one
ListItem object, for example, if the user has "Checked" multiple ListItem
objects.

Dim LstItmObj as MSComctlLib.ListItem
Dim LstItmCntrl as MSComctlLib.ListView

For Each LstItmObj In LstItmCntrl .ListItems
If LstItmObj.Checked Then 'Take action here...
Next

Desi
_________________________


"wb" wrote in message @TK2MSFTNGP04.phx.gbl...
>I do recognize the key MUST be unique. That is what I want to search on.
>I am asking if there is a more efficient method than a loop.
>
> WB
>
> "Ivar" wrote in message
> $Z_2.13@newsfe4-win.ntli.net...
>>
>> "wb" wrote in message
>> news:%233tEFRkdIHA.4844@TK2MSFTNGP04.phx.gbl...
>>> Users can "add" items to the listview with the click of a button, but I
>>> want to prevent duplicate items from being added.
>>>
>>> I understand I can loop through each item when a new item is being added
>>> but that doesn't seem very efficient (O^n right?). Is there a way to
>>> search on the key value since that is what should be unique within each
>>> item?
>>>
>>> WB
>>
>> Not 'Should be unique', it's has to be unique. So if you tried to add a
>> new listitem with a key that is already in the list you would get an
>> error stating 'Key is not unique in collection'.
>> Are you asking about the listitem Text property?
>>
>> Ivar
>>
>
>
Back to top
View user's profile Send private message
Jeff Johnson



Joined: 04 Oct 2007
Posts: 1327

PostPosted: Mon Feb 25, 2008 4:46 pm    Post subject: Re: vb6 listview control existing listitems Reply with quote

"Desi" wrote in message @TK2MSFTNGP05.phx.gbl...

> What you might not be aware of WB is that _someone_ is looping, wether
> it's you or VB, someone has to loop through the collection of ListItem
> objects to find a match.

Not necessarily. The keys may be implemented as a hash table, which means
you simply need to compute the hash value of the key and then use that to
index into the table. Much faster than looping and, in fact, it's the entire
reason hash tables exist.
Back to top
View user's profile Send private message
wb



Joined: 04 Oct 2007
Posts: 10

PostPosted: Mon Feb 25, 2008 5:35 pm    Post subject: Re: vb6 listview control existing listitems Reply with quote

Yes, this is what I was thinking (and unsuccessfully trying to ask) but
wasn't sure how vb would do this. Do you have any references I can review?
WB

"Jeff Johnson" wrote in message @corp.supernews.com...
> "Desi" wrote in message
> @TK2MSFTNGP05.phx.gbl...
>
>> What you might not be aware of WB is that _someone_ is looping, wether
>> it's you or VB, someone has to loop through the collection of ListItem
>> objects to find a match.
>
> Not necessarily. The keys may be implemented as a hash table, which means
> you simply need to compute the hash value of the key and then use that to
> index into the table. Much faster than looping and, in fact, it's the
> entire reason hash tables exist.
>
Back to top
View user's profile Send private message
wb



Joined: 04 Oct 2007
Posts: 10

PostPosted: Mon Feb 25, 2008 5:39 pm    Post subject: Re: vb6 listview control existing listitems Reply with quote

I was missing the point, you're right. It never occurred to me to trap an
error to accomplish what I want. I was trying to prevent the error from
occurring in the first place.

WB

"MikeD" wrote in message @TK2MSFTNGP05.phx.gbl...
>
> "wb" wrote in message @TK2MSFTNGP04.phx.gbl...
>>I do recognize the key MUST be unique. That is what I want to search on.
>>I am asking if there is a more efficient method than a loop.
>>
>
> You're missing the point. If there's already a ListItem object with that
> key, you'll get a trappable error. No need to loop through anything. Of
> course, this depends on what you're using for keys. If adding a duplicate
> ListItem would use a different key than the existing ListItem has, this
> won't do you any good.
>
> --
> Mike
> Microsoft MVP Visual Basic
>
>
Back to top
View user's profile Send private message
Jeff Johnson



Joined: 04 Oct 2007
Posts: 1327

PostPosted: Mon Feb 25, 2008 9:14 pm    Post subject: Re: vb6 listview control existing listitems Reply with quote

"wb" wrote in message %23dIHA.4880@TK2MSFTNGP02.phx.gbl...

> Yes, this is what I was thinking (and unsuccessfully trying to ask) but
> wasn't sure how vb would do this. Do you have any references I can
> review?

No. Error trapping is the best way.
Back to top
View user's profile Send private message
Desi



Joined: 04 Oct 2007
Posts: 49

PostPosted: Mon Feb 25, 2008 8:46 pm    Post subject: Re: vb6 listview control existing listitems Reply with quote

Guess WB and I both learned something. I'll go read up on hash tables and
learn what the man behind the curtain is actually up to. Thanks Jeff.

Desi
___________

"Jeff Johnson" wrote in message @corp.supernews.com...
> "Desi" wrote in message
> @TK2MSFTNGP05.phx.gbl...
>
> Not necessarily. The keys may be implemented as a hash table, which means
> you simply need to compute the hash value of the key and then use that to
> index into the table. Much faster than looping and, in fact, it's the
> entire reason hash tables exist.
>
Back to top
View user's profile Send private message
MikeD



Joined: 04 Oct 2007
Posts: 3348

PostPosted: Tue Feb 26, 2008 12:16 am    Post subject: Re: vb6 listview control existing listitems Reply with quote

"wb" wrote in message %23dIHA.1824@TK2MSFTNGP02.phx.gbl...
>I was missing the point, you're right. It never occurred to me to trap an
>error to accomplish what I want. I was trying to prevent the error from
>occurring in the first place.
>


Don't think of *trappable* errors as bad things. They're just information.
Use that information to your advantage. Errors that you *can't* trap are bad
things.

--
Mike
Microsoft MVP Visual Basic
Back to top
View user's profile Send private message
Steve Gerrard



Joined: 04 Oct 2007
Posts: 1164

PostPosted: Mon Feb 25, 2008 11:42 pm    Post subject: Re: vb6 listview control existing listitems Reply with quote

wb wrote:
> Yes, this is what I was thinking (and unsuccessfully trying to ask)
> but wasn't sure how vb would do this. Do you have any references I
> can review? WB
>

The ListView.ListItems is basically a typical VB collection. Like the VB
collection, you can retrieve items by key, which is generally fast because it
uses the hash table technique Jeff mentioned. However, also like the VB
collection, you don't have a method for checking the existence of an item with a
given key, other than trapping the error. It's too bad, it would be cleaner to
have an Exists(Key) function that just returned True or False, instead of having
to try and retrieve a key, or add a duplicate key, and trapping the error. Oh
well.

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
ListView Control Hello. I've got a ListView control that I'm trying to use to display a list of strings. Only one column vertically is needed. Need it wide enough to display at least 50 characters per row. I'm using this control so that I can determine when someone clicks

Data report & listview control Hey, I was curious to know if there is a way to use the listview control as the source when creating a data report. I tried searching for examples and haven't really comp up with anything worth while. Any leads would be helpful but if there are none then

listview - right mouse button down to make listview selectio Hi Experts How can I use right mouse button down to make a listview selection? Thanks.

Existing VB6 project with SQL2005 I have VB6 project working with SQL2000 database. I use ADO. Is it going to work without any change with the same database attached to SQL2005? If NO, then what kind of changes are required to make it work? Thank you Esha

Can I add columns to an existing recordset? The title explains it. I want to do a query and then add some columns to the resulting ado recordset, iterate through the recordset and put values in the columns. My working alternative is to create a second recordset with the columns I need and populate
Post new topic   Reply to topic    msvisual.com Forum Index -> VB General Discussions All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group