Solved

TStringList and an Add String Event

Posted on 2008-06-25
6
272 Views
Last Modified: 2010-04-16
I#m using a stringlist for collecting all error and others information in my software project.
The stringlist is defined in my Mainform and share as a property with many other forms.
I use another form to display this stinglist.
So far this works fine.

Now I want an software improvment, once a new string is added in some unit to the stringlist,
the add function on this stringlist is overloaded and calls the display stringlist function in my display unit,
can i Overload just the add function for this stringlist and assign a pointer/ reference to any other
function to be excuted at this add event ?
0
Comment
Question by:BdLm
[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
  • 3
  • 3
6 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 21864016
just do something like this:
type
  TStringAddedEvent=procedure(Sender:TStringList; s:string) of object;
 
  tmys=class(tstringlist)
  private
    FOnStringAdded: TStringAddedEvent;
  public
    function Add(const S: string): Integer; override;
    property OnStringAdded:TStringAddedEvent read FOnStringAdded write FOnStringAdded;
  end;
 
 
{ tmys }
 
function tmys.Add(const S: string): Integer;
begin
  result:=inherited Add(s);
  if assigned(OnStringAdded) then
    OnStringAdded(self, s);
end;

Open in new window

0
 
LVL 8

Author Comment

by:BdLm
ID: 21864335
you code matches with my ideas, there is only a small limitation within my project.

with all the units I exchanged a Stringlist data type, using this ideas I have to exchange the tmys data type. I#m not allowed to exchange the definition of the units now.
0
 
LVL 28

Expert Comment

by:2266180
ID: 21864382
you don't have to. it's basic OOP. tmys is a descendent of tstringlist so you can pass an s:tmys to a procedure that requires a tstringlist or a tstrings. it's the other way around that can cause problems, when the procedure requires a tmys but you only have a tstringlist. in that case you must check if you can trypecast before doing so:
procedure whatever(s:ststringlist);
....
  if s is tmys then
    dosomethingelse(s as tmys)
  else
    trouble
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!

 
LVL 8

Author Comment

by:BdLm
ID: 21864827
can I get a short help how to assign a function name ?
MyStrList.OnStringAdded  := Button3Click(Sender);
ist not the correct syntax.
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Unit_TStringList2, StdCtrls;
 
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
 
    MyStrList  :  TStringList2;  //  rename tmys  to this name
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var  i  :  Integer;
begin
      //  test function to add 100 test Strings
      for i:= 1 to 100 do
           MyStrList.Add('test String =  ' + IntToStr(i)  );
 
end;
 
 
procedure TForm1.Button2Click(Sender: TObject);
begin
      //  What function to execute on the onadd event ...?
      MyStrList.OnStringAdded  := Button3Click(Sender);
end;
 
 
 
 
procedure TForm1.Button3Click(Sender: TObject);
var  i  :  Integer;
begin
 
      Memo1.Lines.Clear;
      // Move a strlist to the memo
      for i:= 1 to MyStrList.Count-1 do
                    Memo1.Lines.Add(IntToStr(i) +  ' : ' + MyStrList[i]);
 
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
      // just create the new Stringlist
      MyStrList  :=TStringList2.Create;
end;
 
end.

Open in new window

0
 
LVL 28

Accepted Solution

by:
2266180 earned 500 total points
ID: 21864878
it's not the correct sintax and it's not the correct function either.
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Unit_TStringList2, StdCtrls;
 
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
    procedure stringadded(sender:tstringlist; s:string);
  public
    { Public-Deklarationen }
 
    MyStrList  :  TStringList2;  //  rename tmys  to this name
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.stringadded(sender:tstringlist; s:string);
begin
  showmessage('the string "'+s+'" was added');
  button3.click;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var  i  :  Integer;
begin
      //  test function to add 100 test Strings
      for i:= 1 to 100 do
           MyStrList.Add('test String =  ' + IntToStr(i)  );
 
end;
 
 
procedure TForm1.Button2Click(Sender: TObject);
begin
      //  What function to execute on the onadd event ...?
      MyStrList.OnStringAdded  := Button3Click(Sender);
end;
 
 
 
 
procedure TForm1.Button3Click(Sender: TObject);
var  i  :  Integer;
begin
 
      Memo1.Lines.Clear;
      // Move a strlist to the memo
      for i:= 1 to MyStrList.Count-1 do
                    Memo1.Lines.Add(IntToStr(i) +  ' : ' + MyStrList[i]);
 
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
      // just create the new Stringlist
      MyStrList  :=TStringList2.Create;
  MyStrList.OnStringAdded:=StringAdded;
end;
 
end.

Open in new window

0
 
LVL 8

Author Comment

by:BdLm
ID: 21865022
fine, my problem is solved  .-)   tkx,
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

759 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