Solved

listBox outbounds

Posted on 1998-10-12
8
139 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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