Recent

Author Topic: What am I doing wrong?  (Read 8864 times)

Guest

  • Guest
What am I doing wrong?
« on: December 20, 2005, 09:03:41 am »
I am learning how to build a Lazarus package.Here is my unit file:

Code: [Select]
unit MySlidePan;

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls,Windows;

type TSlidePanel = class(TPanel)
  private
    FSlideTextFile : string;
    FTextToSlide : Ansistring;
    FSlideRect : Rect ;
    function GetTextFileName : string;
    procedure SetTextFileName(ATextFileName:string);
    procedure SetSlideRect;
  public
    constructor Create;
    Destructor Destroy;
    procedure SetUp;
    procedure ScrollText(ScrollingStep:integer;Orientation:boolean);
  published
    property SlideTextFile : string read GetTextFileName write SetTextFileName;
  end;
  procedure Register;

implementation

constructor TSlidePanel.Create;
var
ii          : integer;
OpenFileDlg : TOpenDialog;
Strs        : TStringList;
begin
  inherited Create(self);
  if SlideTextFile='' then begin
    OpenFileDlg := TOpenDialog.Create(self);
    OpenFileDlg.Execute;
    SlideTextFile := OpenFileDlg.FileName;
  end;
  SetSlideRect;
  Strs := TStringList.Create;
  Strs.LoadFromFile(SlideTextFile);
  for ii :=0 to Strs.Count-1 do FTextToSlide := FTextToSlide+Strs.Strings[ii]+#13#10;
  Canvas.Font.Color := clBlack;    DrawText(Canvas.Handle,PChar(FTextToSlide),Length(FTextToSlide),@FSlideRect,DT_CENTER);
end;

Destructor TSlidePanel.Destroy;
begin
  inherited;
end;

procedure TSlidePanel.SetUp;
var
ii          : integer;
OpenFileDlg : TOpenDialog;
Strs        : TStringList;
begin
  inherited Create(self);
  if SlideTextFile='' then begin
    OpenFileDlg := TOpenDialog.Create(self);
    OpenFileDlg.Execute;
    SlideTextFile := OpenFileDlg.FileName;
  end;
  SetSlideRect;
  Strs := TStringList.Create;
  Strs.LoadFromFile(SlideTextFile);
  for ii :=0 to Strs.Count-1 do FTextToSlide := FTextToSlide+Strs.Strings[ii]+#13#10;
  Canvas.Font.Color := clBlack;    DrawText(Canvas.Handle,PChar(FTextToSlide),Length(FTextToSlide),@FSlideRect,DT_CENTER);
end;

function TSlidePanel.GetTextFileName:string;
begin
  Result := FSlideTextFile;
end;

procedure TSlidePanel.SetTextFileName(ATextFileName:string);
begin
  FSlideTextFile := ATextFileName;
end;

procedure TSlidePanel.SetSlideRect;
begin
  FSlideRect.Left := Left;
  FSlideRect.Top :=Top;
  FSlideRect.Right := Left+Width;
  FSlideRect.Bottom := Top+Height;
end;

procedure TSlidePanel.ScrollText(ScrollingStep:integer;Orientation:boolean);
begin
  if SlideTextFile='' then begin
    if Orientation then FSlideRect.Top :=Top-ScrollingStep
    else FSlideRect.Top :=Top+ScrollingStep;
    drawtext(Canvas.Handle,PChar(FTextToSlide),Length(FTextToSlide),@FSlideRect,DT_CENTER);
  end;
end;

procedure Register;
begin
  RegisterComponents('TPanel', [TSlidePanel]);
end;

end.


I added it to a new package and saved it as 'SlidePanelPackage.lpk',compiled and installed
it.Now there apears a new component(TSlidPanel) on the 'standard' panel.
I then create a new project,the following is the code of its main form:

Code: [Select]
unit scrolpan;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, MySlidePan,
  ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    SlidePanel1: TSlidePanel;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  SlidePanel1.SetUp;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  SlidePanel1.ScrollText(1,True);
end;

initialization
  {$I scrolpan.lrs}

end.  

Now problem comes.When I press F9 to run the application,the compiler says:
Project raise exception class 'External:SIGSEGV'
and the code editor opens the file compon.inc and the cursor stops at line 229,
here is the code between line 224 and line 237:
Code: [Select]
...
Procedure TComponent.Notification(AComponent: TComponent;
  Operation: TOperation);

Var Runner : Longint;

begin
  If (Operation=opRemove) and Assigned(FFreeNotifies) then
    begin
    FFreeNotifies.Remove(AComponent);
            If FFreeNotifies.Count=0 then
...


I am novice to pascal and Lazarus.Anyone can help me,please?

Guest

  • Guest
The last post was me
« Reply #1 on: December 20, 2005, 09:14:41 am »
Oh,I forgot to tell you:I am using Lazarus0.9.10 under win2k

mattias

  • Administrator
  • Full Member
  • *
  • Posts: 184
    • http://www.lazarus.freepascal.org
RE: The last post was me
« Reply #2 on: December 20, 2005, 03:01:00 pm »
A component's constructor is not 'Create', but

constructor Create(TheOwner: TComponent); override

and it must call inherited Create(TheOwner);

Never use 'Self' as its owner. You create a circle.

A destructor must be

'destructor Destroy; override;'

and must call inherited.

Anonymous

  • Guest
Re: RE: The last post was me
« Reply #3 on: December 21, 2005, 02:27:55 am »
Quote from: "mattias"
A component's constructor is not 'Create', but

constructor Create(TheOwner: TComponent); override

and it must call inherited Create(TheOwner);

Never use 'Self' as its owner. You create a circle.

A destructor must be

'destructor Destroy; override;'

and must call inherited.

I modified the code as you told me:
Code: [Select]
constructor TSlidePanel.Create;
...
  inherited Create(nil);// this was: inherited Create(self);

and
Code: [Select]
Destructor Destroy;override;// this was: Destructor Destroy;
and delete the wrong line
Code: [Select]
procedure TSlidePanel.SetUp;
...
begin
  inherited Create(self);     //this line should be deleted
  if SlideTextFile='' then begin
...

but the same problem still exists.I am confused about the instance of class:when I create the TSlidePanel,I don't know about it's owner,so I pass the value 'nil' to the constructor.And is this the problem?

Anonymous

  • Guest
RE: Re: RE: The last post was me
« Reply #4 on: December 21, 2005, 02:43:29 am »
Ok,I found the problem:After I modified and compiled the source code of the package,the previous compiled *.ppu file was still used in the project. So I must also modifiy the file in my project and re-compile it again.
  The modified code works OK now.Much thanks to Mattias!

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2583
RE: Re: RE: The last post was me
« Reply #5 on: December 21, 2005, 10:24:21 am »
To use this as a component on forms in a way that they can be saved, then change your constructor to:
Code: [Select]

constructor Create(TheOwner: TComponent); override;
Code: [Select]

TSlidePanel.Create(TheOwner: TComponent);
begin
  inherited;
  ...
end;


this is the only way you can stream your components. You cannot introduce your own constructor
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

 

TinyPortal © 2005-2018