Solved

cycling thru a listbox

Posted on 2004-04-01
20
230 Views
Last Modified: 2010-04-05
I am trying to match items in a listbox with the caption of a lable using this code.

procedure TForm1.Timer1Timer(Sender: TObject);
 Var
  I : integer;
begin
 for I := 0 to ListBox1.Items.Count -1 do
  if label1.Caption = ListBox1.Items[i] then
   begin
    showmessage ('match found' + IntToStr(listbox1.ItemIndex));
   end
  else
    begin
      exit;
    end;
end;

but the only matches that are found are from the last item in the listbox and not sure how to change the code so it will match ALL the items that are found instead of the last one and I am pretty sure that the index that will be returned in the showmessage function will not be correct either so any help there would be great to.
0
Comment
Question by:EE_Newguy
  • 12
  • 5
  • 3
20 Comments
 
LVL 11

Accepted Solution

by:
shaneholmes earned 100 total points
ID: 10736018
for I:= 0 to ListBox1.Items.Count - 1 do
 if ListBox1.Items[I] = Label1.Caption then
  ShowMessage('Match Found at ' + intToStr(I);


Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10736030
for I:= 0 to ListBox1.Items.Count - 1 do
 if ListBox1.Items[I] = Label1.Caption then
  ShowMessage('Match Found at ' + intToStr(I)
 else
  Exit;


Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10736044
oops, i meant to ask why the "else exit".


for I:= 0 to ListBox1.Items.Count - 1 do
 if ListBox1.Items[I] = Label1.Caption then
  ShowMessage('Match Found at ' + intToStr(I));

Shane
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 4

Expert Comment

by:BedouinDN
ID: 10736444
Shanes answer should give you what you want, but just to explain...

The ItemIndex property will give you the integer value of the selected item rather than the referenced item.
So, if you had clicked on the last item in thelistbox  and selected (highlighted) it, that is the number you will recieve through your code when the code finds a match in the list box.

Shanes code will give you the integer value of the referenced list box item.

You might also want to try AnsiSameText rather than = as this is non case sensitive. eg.

for i := 0 to ListBox1.Items.Count -1 do
begin

0
 
LVL 4

Assisted Solution

by:BedouinDN
BedouinDN earned 25 total points
ID: 10736465
damn TAB key... *blush*

for i := 0 to ListBox1.Items.Count-1 do
begin
    if AnsiSameText(ListBox1.Items.Strings[i],Lable1.Caption) then
    begin
    ShowMessage("Match found at ' + IntToStr(i));
    end;
end;
0
 

Author Comment

by:EE_Newguy
ID: 10736878
the else exit part was leftover from a previous attempt which used the item index of the listbox and it was returning a List Out of bound error when it first ran so I had to add the else statement and just forgot to remove it.

Also thx for the help but could you tell me why adding the following to your code above causes a (List out of bounds error) when there is more then 1 item in the list but works fine if the list only contains 1 item.

for I:= 0 to ListBox1.Items.Count - 1 do
 if ListBox1.Items[I] = Label1.Caption then
  Begin
   ShowMessage('Match Found at ' + intToStr(I));
   ListBox1.ItemIndex := (I);
   ListBox1.DeleteSelected;
  End;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10736909

OTTOMH, Try this

for I:= 0 to ListBox1.Items.Count - 1 do
 if ListBox1.Items[I] = Label1.Caption then
  Begin
   ShowMessage('Match Found at ' + intToStr(I));
   ListBox1.ItemIndex := (I);
   ListBox1.Selected[I]:= True;
   ListBox1.DeleteSelected;
  End;


Shane
0
 

Author Comment

by:EE_Newguy
ID: 10736941
That does not work either (ie. same error)
0
 
LVL 4

Expert Comment

by:BedouinDN
ID: 10736954
Why are you setting the ItemIndex?
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10736999
Shouldn't be selecting it anyway... just delete it

for I:= 0 to ListBox1.Items.Count - 1 do
 if ListBox1.Items[I] = Label1.Caption then
 begin
    ListBox1.Delete(I);
    Exit;
  end;
0
 

Author Comment

by:EE_Newguy
ID: 10737047
There is no such thing as Delete For listbox you have to go down to the items level and even then it's red in the code completion window (not sure what that means but can't be good) but I tried it anyway and no joy.

procedure TForm1.Timer1Timer(Sender: TObject);
Var
 I : integer;
begin
for I:= 0 to ListBox1.Items.Count - 1 do
 if ListBox1.Items[I] = Label1.Caption then
  Begin
  ShowMessage('Match Found at ' + intToStr(I));
  ListBox1.Items.delete(I);
 end;
end;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10737083
Yes, your right, delete is a method of the items object, sorry (but see, you figured it out)

anyway, it works perfect on my listbox

Should i send you a demo?

Shane
0
 

Author Comment

by:EE_Newguy
ID: 10737095
I think the problem is that the label caption is also on a timer which changes to a random value every second.

So basically I have 2 timers (the one above) and another one that changes the caption of the label every second to a random value so I am guessing this is where the problem is coming from.
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10737098
This works perfect on mine.

I substituted the label for a Edit box, so i could type in what i wanted to delete

procedure TForm1.Button1Click(Sender: TObject);
var
 I: Integer;
begin
 for I:= 0 to ListBox1.Items.Count - 1 do
 if ListBox1.Items[I] = Edit1.Text then
 begin
    ListBox1.Items.Delete(I);
    Exit;
  end;
end;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10737113
You may have to say  

if UpperCase( ListBox1.Items[I])  =  UpperCase(Edit1.Text) then

or

if AnsiSameText(ListBox1.Items[I]), Edit1.Text) then

Shane

 
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10737116
Yeah, hat will definitely prevent you from finding the value

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10737129
IN your second timer, after you change the label, disable the timer.
In your first timer, after you delete the value in the listbox, re-enable the second timer

Shane
0
 

Author Comment

by:EE_Newguy
ID: 10737159
That wont work but I have wasted enough of your time. I will find a diff way to do what I need.
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10737173
I would have helped you find a solution, had you been more patient.... so would have other experts.

Sorry!

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10737174
I would have helped you find a solution, had you been more patient.... so would have other experts.

Sorry!

Shane
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question