Solved

listBox outbounds

Posted on 1998-10-12
8
140 Views
Last Modified: 2010-04-06
Hi !  I am using some code to delete an entry out of a list box and delete the item from a string list but it produces an list out of bounds error, i cant see why this is, have a look and see what you think, its Delphi 1 code.The dates in the string list are stored in british date format so i have to convert the date in the list box because it might be some other format.Here is a typical entry:
12/10/98 21:05:09 00:37:10 0.279      first is date, then time, then duration(hrs,mns,secs), then cost.  List box entry could be..  10/12/98 21:05:09 00:37:10 0.279

procedure TForm1.DeleteEntryClick(Sender: TObject);
var L : TStringList; i : integer; info : string;
begin
     try
     L := TStringList.Create;
     if (fileExists('History.log'))  then
        L.LoadFromFile('history.log')
     else
         exit;
     if ListBox1.itemIndex = -1  then exit;
     label5.caption := IntToStr(ListBox1.itemIndex);
     info := copy(ListBox1.items[listBox1.itemIndex],9,length(ListBox1.items[listBox1.itemIndex]) );
     ListBox1.items[listBox1.itemIndex] := (BritishDateOrder(StrToDate(copy(ListBox1.items[listBox1.itemIndex],0,9))))
     + (info);
            for i := 0 to L.count-1  do  begin
                if (L[i]) = (ListBox1.items[listBox1.itemIndex] )then begin
                   ListBox1.items.delete(listBox1.itemIndex);
                   L.delete(i); L.SaveTofile('History.log'); break;  end;
            end;


     finally
            L.Free; end;

end;

function Tform1.BritishDateOrder(date : TDateTime) : string;
begin
        result := FormatDateTime('dd/mm/yy', date);
end;

Mitsy anyhelp appreciated
0
Comment
Question by:Mitsy
8 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 1342630
Mitsy,

change "for i:=0 to L.count-1 do begin" to "for i:=L.count-1 downto 0 do begin".

Regards, Madshi.
0
 
LVL 7

Accepted Solution

by:
RBertora earned 50 total points
ID: 1342631
Dude as soon as you delete an entry in
the listbox your COUNT value is decreased.

so L.count may be 20 , but when you delete it
it becomes 19 then 18 and so on.... but your initial
loop variable still remains 20 so when you try and
access number 19, and the listbox only has 18 the program
returns list out of bounds...

you have to use a fancy while or repeat or something to
get around this..


Have fun
Cheers dude.

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1342632
RBertora,

don't you read the comments!? My suggestion is solving exactly the problem that you describe...

Regards, Madshi.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Mitsy
ID: 1342633
It still produces a list outofbounds error i think the problem is the itemindex if i change to a value say 1 it works without a problem, but change it back and the error occurs again also it does not delete the item out the list box but deletes from string list if i change itemindex to some value, i can not see any problem with the code after i made the changes you suggested
any idea? Mit
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1342634
hi, mit!

i've looked at this and thought: man, once you had a problem which was quite similar. problem is: i can't remember clearly what the problem was...
now.
i have not so much time to create an appropriate file and project to test this one. whaddaya think of sending me your source and the file your operating on?
i'll c what i can do then.

regards,

Black Death.

0
 

Author Comment

by:Mitsy
ID: 1342635
hey blackdeath
i have solved it, i chnaged line

 ListBox1.items[listBox1.itemIndex] := (BritishDateOrder(StrToDate(copy(ListBox1.items[listBox1.itemIndex],0,9))))
to

 info := (BritishDateOrder(StrToDate(copy(ListBox1.items[listBox1.itemIndex],0,9))))
then i changed

if (L[i]) = (ListBox1.items[listBox1.itemIndex] )then begin
to

if (L[i]) =  (info) then begin
this is very strange why does this change stop the program from causing
an error, i discovered if i took that line out it allowed me to delete an item from the listBox, i think its because i changed the listbox item it didnt like it.


0
 

Author Comment

by:Mitsy
ID: 1342636
if anyone knows what caused this error please tell me
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1342637
Perhaps the "listBox1.itemIndex" value is changed when you change "listBox1.items[listBox1.itemIndex]" ?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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