Solved

Using ListBoxes dramatically slows my program

Posted on 1997-11-29
6
138 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

17 Experts available now in Live!

Get 1:1 Help Now