Board index » delphi » StringList question

StringList question

I have dynamically created several Button's and StringList's.
named each stringlist after a button       Name :=  'SL' + Button1.caption;

  and i am trying to call it up like this
Myname := 'SL'+Button1.caption;
memo1.Lines.AddStrings(TStringList(MyName).Items);

The stringlist is created and my strings are added but i am at a loss
as how to call the stringlist back up by the string "MyName"

Any help would be appriciated.

          Gart T

 

Re:StringList question


The code provided is a little perplexing:

Myname := 'SL'+Button1.caption;
No problem with this line assuming myName is a string.
  memo1.Lines.AddStrings(TStringList(MyName).Items);
It would seem you're typecasting a string as a stringlist.  This blows up
when you run it, right?

If you want merely to associate a stringList with a button you could just
use
  button1.tag := integer(pointer(myStringList));

If you want to associate a stringlist just with some text, and be able to
retrieve it using merely that text, here's sample code to do that:

var sl1, sl2, sl3 : tStringlist;
begin
  sl1 := tStringList.Create;
  sl2 := tStringList.Create;
  sl3 := tStringList.Create;

  sl2.add('have a nice day 1');
  sl3.add('have a nice day 2');

  sl1.AddObject('whatever 1', sl2);
  sl1.addObject('whatever 2', sl3);

  showMessage(tStringList(sl1.Objects[sl1.indexOf('whatever 1')])[0]);
  showMessage(tStringList(sl1.Objects[sl1.indexOf('whatever 2')])[0]);

  sl1.free;
  sl2.free;
  sl3.free;
end;

I'm using sl1 as the master stringlist, it holds the strings that each
object (stringlist, in this case) is associated with.  Stringlists have the
ability to associate objects with each string inserted, so we're using that,
along with their ability to find the index of a given string within the
list.  In the example, sl2 is associated with the string 'whatever 1' and
when the code runs, it indeed references back to sl2 via that string and
shows it's first line of text.

(Or have I completely missed the point of your question?)

Hope this helps.

Quote
Gary T wrote in message <6qqr3d$2...@forums.borland.com>...
>I have dynamically created several Button's and StringList's.
>named each stringlist after a button       Name :=  'SL' + Button1.caption;

>  and i am trying to call it up like this
>Myname := 'SL'+Button1.caption;
>memo1.Lines.AddStrings(TStringList(MyName).Items);

>The stringlist is created and my strings are added but i am at a loss
>as how to call the stringlist back up by the string "MyName"

>Any help would be appriciated.

>          Gart T

Other Threads