Solved

listBox outbounds

Posted on 1998-10-12
8
138 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reading complex type element repeats first one found several times 3 41
drawing animated level bar based on numbers 3 88
Multiple image collision 13 69
CheckListBox usage 3 51
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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

15 Experts available now in Live!

Get 1:1 Help Now