Solved

Using ListBoxes dramatically slows my program

Posted on 1997-11-29
6
141 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:aztec
6 Comments
 
LVL 5

Expert Comment

by:julio011597
ID: 1352423
I guess the only way is keeping with the array of strings, and use the ListBox just to show the strings to the user.
0
 

Author Comment

by:aztec
ID: 1352424
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
0
 
LVL 1

Accepted Solution

by:
anilms earned 50 total points
ID: 1352425
You can do this - use the ListBox for UI purposes only and then use a Linked List for storing all the strings. You have two choices here - either to create your own list handling functions or to use a TList object. The TList object is faster than the ListBox but if you define your own list processing functions, it will be faster still. You will have to write code to add,delete and traverse a list. You can avoid all this if you use TList. TList is basically a collection of pointers.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:anilms
ID: 1352426
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.
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1352427
Agreed with you both; i said 'array of strings' just for the sake of simplicity.
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1352428
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;


0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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