Board index » delphi » TStringList, How does it really work???

TStringList, How does it really work???

Hello programmers,

I'm having trouble on implementing a TStringList component in my custom
component.

I've got the following:

-----
type
  TMyComponent = class(TComponent)
  private
    fMyStringList: TStringList;
  protected
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  public
    MyStringList: TStringList read fMyStringList write fMyStringList;

implementation

constructor TMyComponent.Create(AOwner: TComponent);
begin
  inherited;
  fMyStringList := TStringList.Create;
end;

destructor TMyComponent.Destroy;
begin
  fMyStringList.Free;
  inherited;
end;
-----

Is there something I should initialize, like the parent.
I've checked in the help for a property or method that initializes it's
parent, but I couldn't find anything.

The error I get is (after clicking on the MyStringList property in the
Object Inspector) 'control '' has no parent window'.

Any help is welcome.

--
+++

Use this address to Mail, no Spam_Mail.
Sanne Hoekstra
Sant...@dds.dds.nl

 

Re:TStringList, How does it really work???


On 4 Jun 1997 12:02:53 GMT, "Sanne Hoekstra"

Quote
<NO_MORE_SPAM_Sant...@dds.dds.nl> wrote:
>Hello programmers,

>I'm having trouble on implementing a TStringList component in my custom
>component.

>I've got the following:

>-----
>type
>  TMyComponent = class(TComponent)
>  private
>    fMyStringList: TStringList;
>  protected
>    constructor Create(AOwner: TComponent); override;
>    destructor Destroy; override;
>  public
>    MyStringList: TStringList read fMyStringList write fMyStringList;

>implementation

>constructor TMyComponent.Create(AOwner: TComponent);
>begin
>  inherited ;
>  fMyStringList := TStringList.Create;
>end;

>destructor TMyComponent.Destroy;
>begin
>  fMyStringList.Free;
>  inherited;
>end;
>-----

>Is there something I should initialize, like the parent.
>I've checked in the help for a property or method that initializes it's
>parent, but I couldn't find anything.

>The error I get is (after clicking on the MyStringList property in the
>Object Inspector) 'control '' has no parent window'.

>Any help is welcome.

>--
>+++

>Use this address to Mail, no Spam_Mail.
>Sanne Hoekstra
>Sant...@dds.dds.nl

This is obviously not ALL the code, what you have cannot compile; at
very least you need to add the "property" keyword before MyStringList
in the class declaration, and "end;" the class.

Beyond that there are a couple of other problems:
1) In general if you want the property to be visible in the object
inspector at design time, it must appear in the "published" section of
the component, from your discussion though, I gather that this is an
omission in your posted code, not your actual component.

2) It is recommended that you type the property and the internal field
as being of type TStrings; even though you initialize it as
TStringList; this is so that the underlying VCL code can copy strings
to and from your property from and to other descendants of TStrings
automatically; if you use TStringList explicitly, you can only
directly copy to and from other TStringLists or descendants.  It also,
as you've discovered, prevents the standard object inspector string
editor from working as it expects a TStrings property.

I'd recommend the following changes:

type
  TMyComponent = class(TComponent)
  private
    fMyStringList: TStrings;
  protected
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  public
  published
    property MyStringList: TStrings
      read fMyStringList
      write fMyStringList;
  end ;

implementation

constructor TMyComponent.Create(AOwner: TComponent);
begin
  inherited Create(AOwner: TComponent) ;
  fMyStringList := TStringList.Create;
end;

destructor TMyComponent.Destroy;
begin
  fMyStringList.Free;
  inherited Destroy ;
end;

HTH

Stephen Posey
slpo...@concentric.net

Re:TStringList, How does it really work???


Quote
Sanne Hoekstra wrote:

> Hello programmers,

> I'm having trouble on implementing a TStringList component in my custom
> component.

> I've got the following:

> -----
> type
>   TMyComponent = class(TComponent)
>   private
>     fMyStringList: TStringList;
>   protected
>     constructor Create(AOwner: TComponent); override;
>     destructor Destroy; override;
>   public
>     MyStringList: TStringList read fMyStringList write fMyStringList;

> implementation

> constructor TMyComponent.Create(AOwner: TComponent);
> begin
>   inherited;
>   fMyStringList := TStringList.Create;
> end;

> destructor TMyComponent.Destroy;
> begin
>   fMyStringList.Free;
>   inherited;
> end;

Leave everything the same but change your property to->

MyStringList: TStrings read fMyStringList write fMyStringList;

This is because a property editor exists for TStrings

Mark

Re:TStringList, How does it really work???


Quote
Mark Bracey <mbra...@interaccess.com> wrote:
>Sanne Hoekstra wrote:
[snip]
>> -----
>> type
>>   TMyComponent = class(TComponent)
>>   private
>>     fMyStringList: TStringList;
>>   protected
>>     constructor Create(AOwner: TComponent); override;
>>     destructor Destroy; override;
>>   public
>>     MyStringList: TStringList read fMyStringList write fMyStringList;

[snip]

>Leave everything the same but change your property to->

>MyStringList: TStrings read fMyStringList write fMyStringList;

>This is because a property editor exists for TStrings

I agree with the above advice as given but I think the property needs
to be declared as published instead of public to take advantage of the
property editor referred to.  Also, another reason to use TStrings (in
the fMyStringList field definition as well as the property definition)
is that then any object decended from TStrings can be assigned to the
property.

---------------------
Al Testani

a...@emi.net
Boca Raton, FL
---------------------

Re:TStringList, How does it really work???


Hi Stephen,

See comment below.

Stephen Posey <slpo...@concentric.net> schreef in artikel
<3395a9bb.12717...@news.concentric.net>...

Quote
> On 4 Jun 1997 12:02:53 GMT, "Sanne Hoekstra"
> <NO_MORE_SPAM_Sant...@dds.dds.nl> wrote:

> >Hello programmers,

> >I'm having trouble on implementing a TStringList component in my custom
> >component.

> >I've got the following:

> >-----
> >type
> >  TMyComponent = class(TComponent)
> >  private
> >    fMyStringList: TStringList;
> >  protected
> >    constructor Create(AOwner: TComponent); override;
> >    destructor Destroy; override;
> >  public
> >    MyStringList: TStringList read fMyStringList write fMyStringList;

> >implementation

> >constructor TMyComponent.Create(AOwner: TComponent);
> >begin
> >  inherited ;
> >  fMyStringList := TStringList.Create;
> >end;

> >destructor TMyComponent.Destroy;
> >begin
> >  fMyStringList.Free;
> >  inherited;
> >end;
> >-----

> >Is there something I should initialize, like the parent.
> >I've checked in the help for a property or method that initializes it's
> >parent, but I couldn't find anything.

> >The error I get is (after clicking on the MyStringList property in the
> >Object Inspector) 'control '' has no parent window'.

> >Any help is welcome.

> >--
> >+++

> >Use this address to Mail, no Spam_Mail.
> >Sanne Hoekstra
> >Sant...@dds.dds.nl

> This is obviously not ALL the code, what you have cannot compile; at
> very least you need to add the "property" keyword before MyStringList
> in the class declaration, and "end;" the class.

Sorry, Indeed I forgot to set it in the message,
It surely is in my code.
It is becouse my code has some classified code (don't all program's have??)
that I had to type it over with other names.

Quote
> Beyond that there are a couple of other problems:
> 1) In general if you want the property to be visible in the object
> inspector at design time, it must appear in the "published" section of
> the component, from your discussion though, I gather that this is an
> omission in your posted code, not your actual component.

In my code I use "published", that's a mistake too in the message.
You saw that right.

Quote
> 2) It is recommended that you type the property and the internal field
> as being of type TStrings; even though you initialize it as
> TStringList; this is so that the underlying VCL code can copy strings
> to and from your property from and to other descendants of TStrings
> automatically; if you use TStringList explicitly, you can only
> directly copy to and from other TStringLists or descendants.  It also,
> as you've discovered, prevents the standard object inspector string
> editor from working as it expects a TStrings property.

I changed it to your recommendations, everything works fine,
until I click on the MyStringList property in the code inspector.
Then I get an acess violation in VCL30.DPL.

I really can't find what I am doing wrong.

Mabey I should only use TStringList as a variable, and use some functions
to make any chages????

- Show quoted text -

Quote
> I'd recommend the following changes:

> type
>   TMyComponent = class(TComponent)
>   private
>     fMyStringList: TStrings;
>   protected
>     constructor Create(AOwner: TComponent); override;
>     destructor Destroy; override;
>   public
>   published
>     property MyStringList: TStrings
>       read fMyStringList
>       write fMyStringList;
>   end ;

> implementation

> constructor TMyComponent.Create(AOwner: TComponent);
> begin
>   inherited Create(AOwner: TComponent) ;

This gives an error on the TComponent. It expects an ")" instead of
TComponent.

Quote
>   fMyStringList := TStringList.Create;

Do you really mean TStringList.Create, and not TStrings.Create?
I used TStringList.Create, but I still get an error when I change anything
in this property in the object inspector.

Quote
> end;

> destructor TMyComponent.Destroy;
> begin
>   fMyStringList.Free;
>   inherited Destroy ;
> end;

> HTH

> Stephen Posey
> slpo...@concentric.net

Thank you for your help.

--
+++

Use this address to Mail, no Spam_Mail.
Sanne Hoekstra
Sant...@dds.dds.nl

Re:TStringList, How does it really work???


Hi Mark,

Se comment below.

Mark Bracey <mbra...@interaccess.com> schreef in artikel
<3395BF5B.6...@interaccess.com>...

Quote
> Sanne Hoekstra wrote:

> > Hello programmers,

> > I'm having trouble on implementing a TStringList component in my custom
> > component.

> > I've got the following:

> > -----
> > type
> >   TMyComponent = class(TComponent)
> >   private
> >     fMyStringList: TStringList;
> >   protected
> >     constructor Create(AOwner: TComponent); override;
> >     destructor Destroy; override;
> >   public
> >     MyStringList: TStringList read fMyStringList write fMyStringList;

> > implementation

> > constructor TMyComponent.Create(AOwner: TComponent);
> > begin
> >   inherited;
> >   fMyStringList := TStringList.Create;
> > end;

> > destructor TMyComponent.Destroy;
> > begin
> >   fMyStringList.Free;
> >   inherited;
> > end;

> Leave everything the same but change your property to->

> MyStringList: TStrings read fMyStringList write fMyStringList;

When I change it this way, Delphi replies with: Incompatible types
TStringList and to Strings.
Delphi replies this when I try to compile the component, It refers to
fMyStringList.

Quote
> This is because a property editor exists for TStrings

TStringList is a descendant of TStrings, so it should have all property's
TStrins has.

Quote
> Mark

--
+++

Use this address to Mail, no Spam_Mail.
Sanne Hoekstra
Sant...@dds.dds.nl

Re:TStringList, How does it really work???


On 5 Jun 1997 07:49:24 GMT, "Sanne Hoekstra"

Quote
<NO_MORE_SPAM_Sant...@dds.dds.nl> wrote:
>Hi Stephen,

>See comment below.

[deletia]

Sorry about the errors in MY code, that'll teach me to compile it
myself before posting it; the following works fine on my system in D1
and D2.  At design time, in the object inspector when I click on the
MyStringList property of the control I get the default string list
editor.

------------------------>8 cut here 8<--------------------------------
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs;

type
  TMyComponent = class(TComponent)
  private
    fMyStringList: TStrings;
  protected
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  public
  published
    property MyStringList: TStrings
      read fMyStringList
      write fMyStringList;
  end ;
procedure Register;

implementation

constructor TMyComponent.Create(AOwner: TComponent);
begin
  inherited Create( AOwner ) ;
  fMyStringList := TStringList.Create;
end;

destructor TMyComponent.Destroy;
begin
  fMyStringList.Free;
  inherited Destroy ;
end;

procedure Register;
begin
  RegisterComponents('Custom', [TMyComponent]);
end;

end.
------------------------>8 cut here 8<--------------------------------

Quote
>I changed it to your recommendations, everything works fine,
>until I click on the MyStringList property in the code inspector.
>Then I get an acess violation in VCL30.DPL.

I don't have D3 yet, but, unless they've significantly changed the
TString/TStringList behavior the above ought to work as well, let me
know if that's NOT the case and I'll investigate further.

Quote
>> constructor TMyComponent.Create(AOwner: TComponent);
>> begin
>>   inherited Create(AOwner: TComponent) ;
>This gives an error on the TComponent. It expects an ")" instead of
>TComponent.

Silly error on my part, obviously you don't include the type like that
in the method CALL.

Quote
>>   fMyStringList := TStringList.Create;
>Do you really mean TStringList.Create, and not TStrings.Create?
>I used TStringList.Create, but I still get an error when I change anything
>in this property in the object inspector.

This is what's puzzling me, you really WANT a StringList if you aren't
going to handle the storage and internal manipulation of the strings
yourself or they aren't being handled internally by some other control
(a la Windows' Edit or Listbox controls).

Quote
>Thank you for your help.

>--
>+++

>Use this address to Mail, no Spam_Mail.
>Sanne Hoekstra
>Sant...@dds.dds.nl

HTH

Stephen Posey
slpo...@concentric.net

Re:TStringList, How does it really work???


Quote
Al Testani wrote:
> Mark Bracey <mbra...@interaccess.com> wrote:
> >Sanne Hoekstra wrote:
> >> type
> >>   TMyComponent = class(TComponent)
> >>   private
> >>     fMyStringList: TStringList;
> >>   protected
> >>     constructor Create(AOwner: TComponent); override;
> >>     destructor Destroy; override;
> >>   public
> >>     MyStringList: TStringList read fMyStringList write
> fMyStringList;
> >Leave everything the same but change your property to->
> >MyStringList: TStrings read fMyStringList write fMyStringList;
> >This is because a property editor exists for TStrings

> I agree with the above advice as given but I think the property needs
> to be declared as published instead of public to take advantage of the
> property editor referred to.  Also, another reason to use TStrings (in
> the fMyStringList field definition as well as the property definition)
> is that then any object decended from TStrings can be assigned to the
> property.

Please correct me if I am wrong, but I think this code leads to an
exception after using the property editor. I would use the following
code:

 type
   TMyComponent = class(TComponent)
   private
     fMyStringList: TStringList;
   protected
     constructor Create(AOwner: TComponent); override;
     destructor Destroy; override;
     procedure SetMyStringList(Value: TStringList);
   published
     MyStringList: TStringList read fMyStringList write SetMyStringList;

 procedure SetMyStringList(Value: TStringList);
 begin
   fMyStringList.Assign(Value);
 end;

BTW: A property editor editor registered for some class (say TStrings)
should also be usable for all descendants of this class (TStringList).

Regards, Goeran

Re:TStringList, How does it really work???


Quote
> type
>   TMyComponent = class(TComponent)
>   private
>     fMyStringList: TStringList;
>   protected
>     constructor Create(AOwner: TComponent); override;
>     destructor Destroy; override;
>     procedure SetMyStringList(Value: TStringList);
>   published
>     MyStringList: TStringList read fMyStringList write SetMyStringList;

> procedure SetMyStringList(Value: TStringList);
> begin
>   fMyStringList.Assign(Value);
> end;

What would the constructor look like?

constructor TMyComponent.Create(AOwner : TComponent);
begin
  inherited create(AOwner);

  fMyStringList:=TStringList.Create;
end;

I have had problems with a component that uses a TString list when the
component is in design mode.  It causes GPF's with a error 210?

Thanks in Advance

Ryan

Re:TStringList, How does it really work???


Ray Lischner's book, Secrets of Delphi 2 has the best  discussion of the
TStrings, TStringList, TList components of Delphi that I have come across.
Good luck.
Gene

Ryan Evans <r...@newmedco.com> wrote in article
<5rihjh$mp...@news.gate.net>...

Quote
> > type
> >   TMyComponent = class(TComponent)
> >   private
> >     fMyStringList: TStringList;
> >   protected
> >     constructor Create(AOwner: TComponent); override;
> >     destructor Destroy; override;
> >     procedure SetMyStringList(Value: TStringList);
> >   published
> >     MyStringList: TStringList read fMyStringList write SetMyStringList;

> > procedure SetMyStringList(Value: TStringList);
> > begin
> >   fMyStringList.Assign(Value);
> > end;

> What would the constructor look like?

> constructor TMyComponent.Create(AOwner : TComponent);
> begin
>   inherited create(AOwner);

>   fMyStringList:=TStringList.Create;
> end;

> I have had problems with a component that uses a TString list when the
> component is in design mode.  It causes GPF's with a error 210?

> Thanks in Advance

> Ryan

Other Threads