Solved

cycling thru a listbox

Posted on 2004-04-01
20
228 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

930 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

10 Experts available now in Live!

Get 1:1 Help Now