Solved

listBox outbounds

Posted on 1998-10-12
8
137 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

13 Experts available now in Live!

Get 1:1 Help Now