Link to home
Start Free TrialLog in
Avatar of eNarc
eNarcFlag for United Kingdom of Great Britain and Northern Ireland

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)
Avatar of jimyX
jimyX

Can you clarify more please? How many columns and what is New (1) and New(2)?
If you need to search the first column:
procedure TForm1.Button5Click(Sender: TObject);
var
  i:integer;
  Found:Boolean;
begin
  Found:= False;
  for i:= 0 to ListView1.Items.Count-1 do
    begin
      if ListView1.Items[i].Caption = 'New' then
        begin
          Found:=True;
          break;
        end;
    end;
  if not Found then
    begin
      ListView1.Items.Add;
      ListView1.Items.Item[ListView1.Items.Count-1].Caption := 'New';
    end;
end;

Open in new window

Avatar of eNarc

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?
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
...
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)?
Avatar of eNarc

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?
ASKER CERTIFIED SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
@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.
@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[].Caption = 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
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

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
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.