Solved

cycling thru a listbox

Posted on 2004-04-01
20
227 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
That does not work either (ie. same error)
0
 
LVL 4

Expert Comment

by:BedouinDN
Comment Utility
Why are you setting the ItemIndex?
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:EE_Newguy
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yeah, hat will definitely prevent you from finding the value

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I would have helped you find a solution, had you been more patient.... so would have other experts.

Sorry!

Shane
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
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…

762 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

6 Experts available now in Live!

Get 1:1 Help Now