eNarc
asked on
Search Listview
How do I search a listview for a name? and if not found add a new
so if New is found check New (1) if found check New (2) if not found add New (2)
so if New is found check New (1) if found check New (2) if not found add New (2)
ASKER
its just the caption.
within 1 colomn.
New
New (1)
New (2)
now when I add its gonna be New By Default, I'm needing to search through the list and then put the next number which would be
New (4)
how could I achieve this?
within 1 colomn.
New
New (1)
New (2)
now when I add its gonna be New By Default, I'm needing to search through the list and then put the next number which would be
New (4)
how could I achieve this?
I think you meant:
If you have:
New
New (1)
New (2)
The next will be:
New (3)
Is that correct?
Also what is the possibility of having different order?
Is it always going to be in this order:
1
2
3
...
If you have:
New
New (1)
New (2)
The next will be:
New (3)
Is that correct?
Also what is the possibility of having different order?
Is it always going to be in this order:
1
2
3
...
For example is it possible to have:
New
New (1)
New (2)
New (4)
So when you add new item it will be:
New (3)
Or you always have them in order (i.e. the last item will be the greater)?
New
New (1)
New (2)
New (4)
So when you add new item it will be:
New (3)
Or you always have them in order (i.e. the last item will be the greater)?
ASKER
its just like finding a name that isn't used and using that name. if the name is used then try next number. its like in chrome, you add a photo, if there is a name the same it will use (1) and so on, even like creating folders, using the same way.
any ideas?
any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The code I provided in post #35497251 answers the question and does exactly what the Author has requested.
@jimmyX
You are continually modifying the search string for no reason. And your code assumes there is a proper order of the items.
Str := 'New (' + IntToStr(j) +')';
That is not the right way to do it at least not if the answer is to used for later reference.
You only need to find the missing index once and construct the entry regardless of the item orders
Actually the code is correct.
It starts looking for "New" in the list. If found you now have to see if "New(1)" is in the list and so on until you find either "New: is not used or the first n such that "New(n)" is not used.
mlmcc
It starts looking for "New" in the list. If found you now have to see if "New(1)" is in the list and so on until you find either "New: is not used or the first n such that "New(n)" is not used.
mlmcc
@ewangoya
Your code is long and its need to involve TStringList!!
The Code i provided short, and does not need to create StringList! & Solved the question,
Anyway, i dont think deleting the question is good, because i think the question is already answered
from all the comments above, so if there is no Points Spliting, so i suggest to give the points to jimyX because he was the faster.
Your code is long and its need to involve TStringList!!
The Code i provided short, and does not need to create StringList! & Solved the question,
Anyway, i dont think deleting the question is good, because i think the question is already answered
from all the comments above, so if there is no Points Spliting, so i suggest to give the points to jimyX because he was the faster.
@Snip3rX
1. Its not how fast yo answer a question and it does not matter whether the code is long or not. Sometimes when you optimize stuff, you may end up with long code (I'm not saying my code is optimized)
2. Your code does not work at all, If you run it, you get an integer conversion error
@jimmyX
Sorry, your actually does work. Here is the problem with your code which made me think it did not work to begin with
while i <= ListView1.Items.Count-1 do
begin
if ListView1.Items.Item[].Cap tion = Str then
begin
Str := 'New (' + IntToStr(j) +')';
inc(j);
i := 0; //you are actually changing the value of the loop control variable within the loop
continue;
end;
...............
Personally, I don't condone such code but I must say that it works. If its good for the asker, then that's fine, I have no problem with that at all
cheers
1. Its not how fast yo answer a question and it does not matter whether the code is long or not. Sometimes when you optimize stuff, you may end up with long code (I'm not saying my code is optimized)
2. Your code does not work at all, If you run it, you get an integer conversion error
@jimmyX
Sorry, your actually does work. Here is the problem with your code which made me think it did not work to begin with
while i <= ListView1.Items.Count-1 do
begin
if ListView1.Items.Item[].Cap
begin
Str := 'New (' + IntToStr(j) +')';
inc(j);
i := 0; //you are actually changing the value of the loop control variable within the loop
continue;
end;
...............
Personally, I don't condone such code but I must say that it works. If its good for the asker, then that's fine, I have no problem with that at all
cheers
there are many ways to cook, be sure the ingredients are not rotten and the food is not raw. ;)
In a WHILE loop you have to change the value of the loop control variable otherwise you will be in an infinite loop.
He is changing it so the loop essentially restarts but that is because when j is incremented you have to search from the beginning of the list again since there is no guarantee the list is any specific order.
mlmcc
He is changing it so the loop essentially restarts but that is because when j is incremented you have to search from the beginning of the list again since there is no guarantee the list is any specific order.
mlmcc
I was not very clear with what I said. The variable definitely has to be incremented.
I'm pointing to the fact that the variable is reset to the start value within the loop its controlling.
True and to restart the search through the list you have to reset it when you find the one you want
Consider this list
New(1)
New
New(2)
New(4)
New(3)
If after finding New you just contiinue the list looking for New(1) it won't be found but it does exist as the first element in the list. Thus the list index must get reset so the search can start back at the beginning of the list.
If the list were to be ordered in ascending value there would be no need to reset the index because it wouldn't be in the earlier part of the list. Since there is no guarantee the list is sorted in any order the loop control must be reset.
mlmcc
Consider this list
New(1)
New
New(2)
New(4)
New(3)
If after finding New you just contiinue the list looking for New(1) it won't be found but it does exist as the first element in the list. Thus the list index must get reset so the search can start back at the beginning of the list.
If the list were to be ordered in ascending value there would be no need to reset the index because it wouldn't be in the earlier part of the list. Since there is no guarantee the list is sorted in any order the loop control must be reset.
mlmcc
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
If you need to search the first column:
Open in new window