Страница 1 из 1

Разработка компонента

Добавлено: 01 дек 2004, 21:07
Vano
Подскажите кто знает почему код из книги не работает, а другой работает т.е. компилируется??
Вот код из примера в книге(он не компилируется, а выдает ошибку)

Код: Выделить всё

unit CountBtn;

interface

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

type
TCountBtn = class(TButton)
private
{ Private declarations }
FCount:integer;
protected
{ Protected declarations }
procedure Click;override;
public
{ Public declarations }
procedure ShowCount;
published
{ Published declarations }
property Count:integer read FCount write FCount;
constructor Create(aowner:Tcomponent);override;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Mihan Components', [TCountBtn]);
end;

constructor TCountBtn.Create(aowner:Tcomponent);
begin
inherited create(Aowner);
end;

procedure Tcountbtn.Click;
begin
inherited click;
FCount:=FCount+1;
end;

procedure TCountBtn.ShowCount;
begin
Showmessage('По кнопке '+ caption+' вы сделали: '+inttostr(FCount)+' клик(а/ов)'); //здесь выскакивает ошибка
end;
end. 
А вот другой код(он сработал, но почему без переменной p он отказывается работать я не догнал)

Код: Выделить всё

unit CountBtn;

interface

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

var
  p: TObject;
type
  TCountBtn = class(TButton)
  private
  { Private declarations }
    FCount:integer;
  protected
  { Protected declarations }
    procedure Click;override;
  public
  { Public declarations }
    procedure ShowCount;
  published
  { Published declarations }
    property Count:integer read FCount write FCount;
    constructor Create(aowner:Tcomponent);override;
end;

procedure Register;

implementation

procedure Register;

begin
  RegisterComponents('MyComponent', [TCountBtn]);
end;

constructor TCountBtn.Create(aowner:Tcomponent);
begin
inherited create(Aowner);
end;

procedure Tcountbtn.Click;
begin
inherited click;
FCount:=FCount+1;
end;

procedure TCountBtn.ShowCount;
begin
Showmessage('По кнопке '+ TButton(p).Caption+' вы сделали: '
  +inttostr(TCountBtn(p).FCount)+' клик(а/ов)'); // при компилящии ошибки нет, а вот при нажатии на кнопку уже в приложении здесь выскакивает ошибка
end;

end.

Добавлено: 02 дек 2004, 14:45
Romeo
В runtime выскакивает ошибка потому, что переменная p не инициализирована.

Добавлено: 04 дек 2004, 02:35
Vano
Вот такой код работает нормально:

Код: Выделить всё

unit CountBtn; 

interface 

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

type 
TCountBtn = class(TButton) 
private 
{ Private declarations } 
FCount:integer; 
protected 
{ Protected declarations } 
procedure Click;override; 
public 
{ Public declarations } 
procedure ShowCount; 
published 
{ Published declarations } 
property Count:integer read FCount write FCount; 
constructor Create(aowner:Tcomponent);override; 
end; 

procedure Register; 

implementation 

procedure Register; 
begin 
RegisterComponents('Mihan Components', [TCountBtn]); 
end; 

constructor TCountBtn.Create(aowner:Tcomponent); 
begin 
inherited create(Aowner); 
end; 

procedure Tcountbtn.Click; 
begin 
inherited click; 
FCount:=FCount+1; 
end; 

procedure TCountBtn.ShowCount; 
begin 
Showmessage('По кнопке '+ TCountBtn(self).caption+' вы сделали: '+inttostr(TCountBtn(self).FCount)+' клик(а/ов)'); 
end; 

end.

Добавлено: 06 дек 2004, 17:06
Romeo
И где, собственно говоря, вопрос?