Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

cycling thru a listbox

Posted on 2004-04-01
20
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 5
  • 3
20 Comments
 
LVL 11

Accepted Solution

by:
shaneholmes earned 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 100 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

609 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