Urgent: Listbox searching

sfern
sfern used Ask the Experts™
on
Hi experts :-)

I've got a problem here.

I've got a listbox which collects items from a log file.

Log file example:

1;description;whatever
1;description;whatever
2;description;whatever
3;description;whatever
2;description;whatever
4;description;whatever

The ';' is to separate the items into defferent columns on the listbox.

The log file is loaded into the listbox and once loaded the log file is cleared.

My problem.

Once it is loaded to the listbox it will execute the following:

Check line by line and do the following:

0. Check for the string on the first column of the listbox
1. if line contains a '1' then do something...
2. once done clear the line from the listbox
3. go to the next line
4. etc...
until the listbox is empty.

so i will search line but line...depending if 1 or 2 or 3 or 4 is found then it will do something and clear it from the listbox.

Note: The listbox will contain many '1''s '2''s '3''s etc...

could someone please help me with an example please.

I will upgrade me points for a good example.

Thanks

sfern

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I hope the code below helps ... it is self-explanatory ... If you have any doubt, ask us, and we will help you : )

procedure TForm1.Button1Click(Sender: TObject);
var
  StringList: TStringList;
  i, code: integer;
  description, whatever: string;
begin
  StringList := TStringList.create;
  while ListBox1.Items.Count > 0 do
  begin
    // Parse the i-th line of the ListBox1 into a StringList
    StringList.CommaText := StringReplace(Listbox1.Items[0],';',',',[rfReplaceAll]);
    code := StrToInt(StringList[0]);
    description := StringList[1];
    whatever := StringList[2];
    // Now you do what you want depending on the code
    showmessage(Format('code: %d description: %s whatever: %s',[code, description, whatever]));
    // Then, delete the item, as you want
    ListBox1.Items.Delete(0);
  end;
  StringList.free;
end;
Ok, try this :

On the form, there is Listbox1 and button1.

Here is the only code you need (on the button click to process)

It assumes the first character is always the number you are looking for, which from your example, it is.

procedure TForm1.Button1Click(Sender: TObject);
var
    nNumber : integer;
begin

    while listbox1.Count > 0 do
    begin
        nNumber := StrToInt(copy(listbox1.Items[0], 0, 1));
        case nNumber of
        1:; // do something
        2:; // do something
        3:; // do something
        4:; // do something
        end; // case
        Listbox1.Items.Delete(0);
    end; // while

end;

Grendel.
Whoops, sorry knightmad, by the time I'd copied and pasted my code, you'd answered it as well, wasn't trying to steal your thunder!  ;)

Grendel.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

No problem ... and, anyway, we used different techniques : )

Author

Commented:
Hi all :-)

Ok, where do i start?

knightmad, i'm sorry if i didn't explain myself properly but you have put more functionality than what i need.

What i need is more oriented to the code posted by Grendel666.

Grendel666, i tried your's and it works but at the end i get an error (index out of bounds)

He's what i've tried:

procedure TForm1.Timer3Timer(Sender: TObject);
var i : integer;

begin
while listbox1.Count > 0 do
   begin
       i := StrToInt(copy(listbox1.Items[0], 0, 1));
       case i of
       1:SMTPBody.Lines.Add('IPMonitor Mail Alert'+#13+'First Warning Alert'+#13+ListBox1.Items[i]);
       2:SMTPBody.Lines.Add('IPMonitor Recovery Alert'+#13+'Auto Recover'+#13+ListBox1.Items[i]);
       3:SMTPBody.Lines.Add('IPMonitor Call out Alert'+#13+'Call out Alert'+#13+ListBox1.Items[i]);
       4:SMTPBody.Lines.Add('IPMonitor Recovered Alert'+#13+'Alert Recovered Alert'+#13+ListBox1.Items[i]);
       end; // case
       Listbox1.Items.Delete(0);
   end; // while

thanks

sfern

Author

Commented:
I nearly made it work with a few modification but a couple of problems:

1. It scans all the listbox items but on the last one i get error message (item index out of bounds(1).

2. When moving the individual lines to a memo, i get the ';' ...how do i get rid of them?

I will upgrade my points for the right answer.

Thanks

SFern.
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  i : integer;
  Str: String;
begin
  while listbox1.Count > 0 do
  begin
      i := StrToInt(copy(listbox1.Items[0], 0, 1));
      Str := Copy( ListBox1.Items[ 0 ], 3, Length( ListBox1.Items[ 0 ] ) );
      case i of
       1:SMTPBody.Lines.Add('IPMonitor Mail Alert'+#13+'First Warning Alert'+#13+#10+Str);
       2:SMTPBody.Lines.Add('IPMonitor Recovery Alert'+#13+#10+'Auto Recover'+#13+#10+Str);
       3:SMTPBody.Lines.Add('IPMonitor Call out Alert'+#13+#10+'Call out Alert'+#13+#10+Str);
       4:SMTPBody.Lines.Add('IPMonitor Recovered Alert'+#13+#10+'Alert Recovered Alert'+#13+#10+Str);
       end; // case
      Listbox1.Items.Delete(0);
  end; // while
end;
Hi sfern,

The problem you had with the modified version of my code was that you were using i as the memo line to work on.  The variable i is just to get the first number of the first line in the memo, so this will be 1, 1, 2, 3, 2, 4, from your example.

The example from sybernite should work.

If not, let us know and I'll have a play around with it.

Author

Commented:
Thanks :-)

It's working now.

You have both helped me althhough sybernite's code is the final solution.

Please what so i do now?

Does the points go to the final answer of what do i do here.

Please advice me.

BTW: As i promised i have upgraded my points.

Thanks

SFern.
I believe the points should go to Grendel666. It looked like his original answer that helped you work it out. All I did was refine what you already had. :)

Author

Commented:
Thanks guys for all your help :-)

Have a great day!!!

SFern.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial