Solved

listBox outbounds

Posted on 1998-10-12
8
142 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 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