Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: TStrings.SetStrings throwing 'List index out of bounds error'


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Feb 20, 2017 9:22 AM Last Post By: Lajos Juhasz Threads: [ Previous | Next ]
Toby Dobbs

Posts: 77
Registered: 9/24/16
TStrings.SetStrings throwing 'List index out of bounds error'  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 20, 2017 8:45 AM
I have a facility in my program which assignt the contents of a list box to the title of a TeeChart using the SetStrings procedure as in:

a) Chart.Title.Text.SetStrings(Memo.Lines);
b) Memo.Lines.SetStrings(Chart.Title.Text);

a) is triggered when the user types in the TMemo and is therefore executed inside the associated OnChange event handler.
b) is triggered when the user selects a title from a list box.

I have created a nested procedure to carry out b) so that a) is not triggered at the same time which looks like this:

procedure WriteLinesToMemo(linesToWrite: TStrings);
  var
    tmpEvent: TNotifyEvent;
  begin
     tmpEvent := TitlesMemo.OnChange;
     TitlesMemo.Lines.SetStrings(linesToWrite);
     *TitlesMemo.OnChange := tmpEvent;*
  end;


However, I keep seeing en error thrown which reads:

EStringListError with message 'List index out of bounds (1)' and I can't see why. This is thrown at the line highlighted.
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: TStrings.SetStrings throwing 'List index out of bounds error'  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 20, 2017 9:22 AM   in response to: Toby Dobbs in response to: Toby Dobbs
Toby Dobbs wrote:

I have a facility in my program which assignt the contents of a list
box to the title of a TeeChart using the SetStrings procedure as in:

a) Chart.Title.Text.SetStrings(Memo.Lines);
b) Memo.Lines.SetStrings(Chart.Title.Text);

a) is triggered when the user types in the TMemo and is therefore
executed inside the associated OnChange event handler. b) is
triggered when the user selects a title from a list box.

I have created a nested procedure to carry out b) so that a) is not
triggered at the same time which looks like this:

procedure WriteLinesToMemo(linesToWrite: TStrings);
  var
    tmpEvent: TNotifyEvent;
  begin
     tmpEvent := TitlesMemo.OnChange;
     TitlesMemo.Lines.SetStrings(linesToWrite);
     *TitlesMemo.OnChange := tmpEvent;*
  end;


You doesn't set TitlesMemo.Onchange to nil to prevent the event to be
fired. Of course, you should also guard this with try finally block:

 procedure WriteLinesToMemo(linesToWrite: TStrings);
   var
     tmpEvent: TNotifyEvent;
   begin
      tmpEvent := TitlesMemo.OnChange;
      try
        TitlesMemo.OnChange:=nil;
        TitlesMemo.Lines.SetStrings(linesToWrite);
      finally 
        TitlesMemo.OnChange := tmpEvent;
      end;
   end;
 
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02