Solved

cycling thru a listbox

Posted on 2004-04-01
20
229 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now