|
| Author |
Message |
wb
Joined: 04 Oct 2007 Posts: 10
|
Posted: Sat Feb 23, 2008 2:48 pm Post subject: vb6 listview control existing listitems |
|
|
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 |
|
 |
Ivar
Joined: 04 Oct 2007 Posts: 28
|
Posted: Sat Feb 23, 2008 11:04 pm Post subject: Re: vb6 listview control existing listitems |
|
|
"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 |
|
 |
wb
Joined: 04 Oct 2007 Posts: 10
|
Posted: Sun Feb 24, 2008 3:46 pm Post subject: Re: vb6 listview control existing listitems |
|
|
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 |
|
 |
MikeD
Joined: 04 Oct 2007 Posts: 3348
|
Posted: Sun Feb 24, 2008 6:56 pm Post subject: Re: vb6 listview control existing listitems |
|
|
"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 |
|
 |
"Jan Hyde
Joined: 04 Oct 2007 Posts: 466
|
Posted: Mon Feb 25, 2008 1:13 pm Post subject: Re: vb6 listview control existing listitems |
|
|
"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 |
|
 |
Desi
Joined: 04 Oct 2007 Posts: 49
|
Posted: Mon Feb 25, 2008 2:12 pm Post subject: Re: vb6 listview control existing listitems |
|
|
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 |
|
 |
Jeff Johnson
Joined: 04 Oct 2007 Posts: 1327
|
Posted: Mon Feb 25, 2008 4:46 pm Post subject: Re: vb6 listview control existing listitems |
|
|
"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 |
|
 |
wb
Joined: 04 Oct 2007 Posts: 10
|
Posted: Mon Feb 25, 2008 5:35 pm Post subject: Re: vb6 listview control existing listitems |
|
|
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 |
|
 |
wb
Joined: 04 Oct 2007 Posts: 10
|
Posted: Mon Feb 25, 2008 5:39 pm Post subject: Re: vb6 listview control existing listitems |
|
|
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 |
|
 |
Jeff Johnson
Joined: 04 Oct 2007 Posts: 1327
|
Posted: Mon Feb 25, 2008 9:14 pm Post subject: Re: vb6 listview control existing listitems |
|
|
"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 |
|
 |
Desi
Joined: 04 Oct 2007 Posts: 49
|
Posted: Mon Feb 25, 2008 8:46 pm Post subject: Re: vb6 listview control existing listitems |
|
|
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 |
|
 |
MikeD
Joined: 04 Oct 2007 Posts: 3348
|
Posted: Tue Feb 26, 2008 12:16 am Post subject: Re: vb6 listview control existing listitems |
|
|
"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 |
|
 |
Steve Gerrard
Joined: 04 Oct 2007 Posts: 1164
|
Posted: Mon Feb 25, 2008 11:42 pm Post subject: Re: vb6 listview control existing listitems |
|
|
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 |
|
 |
|