Link to home
Start Free TrialLog in
Avatar of aztec
aztec

asked on

Using ListBoxes dramatically slows my program

Hi ..
  I have an app that reads a file looking for strings specified by the user. I used to have it do they could specify the up to 10 strings via edit boxes, then I'd take the contents of the edit boxes and move it to a array of string 10 big. I'd then loop around checking the input file record for any of these 10 strings. This worked OK, but I wanted to allow the user to input an unlimited number of strings, so I switched over to using a list box for input.
  This slowed the execution of my program by almost 300%! Here's the code I'm using:

for y:=0 to ListBox3.items.count - 1 do
    begin
      x:=0;
      x := Pos(ListBox3.items[y], inrec);
      if x <> 0 then
      begin
        writeln(outfile, inrec);
        goto doagain;
      end;
    end;

.. The code I had before looked almost identical except I used string arrays instead of the listbox:

for y:=1 to 10 do
    begin
      x:=0;
      x := Pos(input_str[y], inrec);
      if x <> 0 then
      begin
        writeln(outfile, inrec);
        goto doagain;
      end;
    end;

.. How can I get around this? I really have to use listBoxes
so I can accept a lot of strings easily, but it really seems to be slowing me down!!

Regards,
  Shawn
Avatar of julio011597
julio011597

I guess the only way is keeping with the array of strings, and use the ListBox just to show the strings to the user.
Avatar of aztec

ASKER

I don't want to use just arrays of strings because I had a lot of string arrays in my program before and it blew it out of the water. The user could potentially add a lot of strings and to make a string array hundreds big would crash my program like before. Can't I use pointers to strings? I've heard this may help me, but I don't know how to do it??

Shawn
ASKER CERTIFIED SOLUTION
Avatar of anilms
anilms

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, the above was not an answer - it was more of a guideline. I did not purposefully put it as an answer. I can give an answer based on more input.
Agreed with you both; i said 'array of strings' just for the sake of simplicity.
how do you ascertain that using listbox is 300 times slower?
I tested you code and found that there are not much difference in speed of both method. This is true event with the use of memo box. Is there any thing wrong in your 'doagain' code?
May be I have misunderstood your requirement. Could you give us more detail.
The following are my test program which use
a. array of string
b. listbox
c. memo
(Note that I have remove the goto statement for [b] and [c].)

procedure TForm1.Button1Click(Sender: TObject);
var x, y: integer;
label doagain;
begin
  for x:=1 to 10 do
    input_Str[x]:= listbox1.items[x-1];
  doagain:
  inrec:= InputBox('Data entry', 'Data = ', '');
  for y:=1 to 10 do begin
    x:=0;
    x := Pos(input_str[y], inrec);
    if x <> 0 then begin
      writeln(outfile, inrec);
      goto doagain;
    end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var x, y: integer;
begin
 while InputQuery('Data entry',
    'Click [OK] to confirm or [Cancel] to quit:', inrec) do begin
   for y:=0 to ListBox1.items.count - 1 do begin
     x:=0;
     x := Pos(ListBox1.items[y], inrec);
     if x <> 0 then
       writeln(outfile, inrec);
   end;
 end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var x, y: integer;
begin
  while InputQuery('Data entry',
    'Click [OK] to confirm or [Cancel] to quit:', inrec) do begin
    for y:=0 to memo1.lines.count - 1 do begin
      x:=0;
      x := Pos(memo1.lines[y], inrec);
      if x <> 0 then
        writeln(outfile, inrec);
    end;
  end;
end;