野
野芒
Unregistered / Unconfirmed
GUEST, unregistred user!
本人在学习设计一个组件时遇到了一点麻烦,虽几经周折也不行,
组件单元是可以被装入组件包的,在pakage edit中编译是可以的,
只是不会在component palate上出现该组件,用组件向导搞出一个
新组件是没有问题的,虽反复对比两个单元的代码(结构部份)找不
出问题所在。
现给出原代码,希望大家不要介意,只看重要部份好了。大家可以
自己把它拿过去安装一下,看看怎么搞的。
unit datepas;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
tdate=class(tcomponent)
private
fyear,fday,fmonth:integer;
fonchange:tnotifyevent;
public
procedure increase;
procedure add(numberofdays:integer);
function gettext:string;
procedure setvalue(m,d,y:integer);
function leapyear:boolean;
constructor create(aowner:tcomponent);override;
protected
procedure setyear(value:integer);
procedure setmonth(value:integer);
procedure setday(value:integer);
function daysinmonth:integer;
procedure dochange;virtual;
published
property year:integer read fyear write setyear;
property day:integer read fday write setday;
property month:integer read fmonth write setmonth;
property text:string read gettext;
property onchange:tnotifyevent read fonchange write fonchange;
end;
procedure register;
implementation
procedure register ;
begin
registercomponents('md3',[tdate]);
end;
constructor tdate.create(aowner:tcomponent);
var y,m,d:word;
begin
inherited create(aowner);
decodedate(now,y,m,d);
setyear;
setmonth(m);
setday(d);
end;
procedure tdate.dochange;
begin
if assigned(fonchange) then
fonchange(self);
end;
procedure tdate.setyear(value:integer);
begin
fyear:=value;
dochange;
end;
procedure tdate.setmonth(value:integer);
begin
if (value>=1)and (value<=12) then
begin
fmonth:=value;
dochange;
end
else
showmessage('月份超出范围');
end;
procedure tdate.setday(value:integer);
begin
if (value>=1) and (value<=daysinmonth) then
begin
fday:=value;
dochange;
end
else
showmessage('天超过范围');
end;
procedure tdate.increase;
begin
if fday<daysinmonth then
inc(fday)
else
if fmonth<12 then
begin
inc(fmonth);
fday:=1;
end
else
begin
inc(fyear);
fmonth:=1;
fday:=1;
end;
dochange;
end;
function tdate.daysinmonth:integer;
begin
case fmonth of
1,3,5,7,8,10,12:daysinmonth:=31;
4,6,9,11:daysinmonth:=30;
2:
if leapyear then
daysinmonth:=29
else
daysinmonth:=28;
else
daysinmonth:=0;
end;
end;
function tdate.gettext:string;
begin
gettext:=format('%.2d.%.2d.%4d',
[fmonth,fday,fyear]);
end;
procedure tdate.add(numberofdays:integer);
var n:integer;
begin
for n:=1 to numberofdays do
increase;
dochange;
end;
procedure tdate.setvalue(m,d,y:integer);
begin
setyear;
setmonth(m);
setday(d);
end;
function tdate.leapyear:boolean;
begin
if (fyear mod 4 <>0 ) then
result:=false
else if (fyear mod 100<>0)then
result:=true
else if (fyear mod 400<>0) then
result:=false
else
result:=true;
end;
end.
[][][]
组件单元是可以被装入组件包的,在pakage edit中编译是可以的,
只是不会在component palate上出现该组件,用组件向导搞出一个
新组件是没有问题的,虽反复对比两个单元的代码(结构部份)找不
出问题所在。
现给出原代码,希望大家不要介意,只看重要部份好了。大家可以
自己把它拿过去安装一下,看看怎么搞的。
unit datepas;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
tdate=class(tcomponent)
private
fyear,fday,fmonth:integer;
fonchange:tnotifyevent;
public
procedure increase;
procedure add(numberofdays:integer);
function gettext:string;
procedure setvalue(m,d,y:integer);
function leapyear:boolean;
constructor create(aowner:tcomponent);override;
protected
procedure setyear(value:integer);
procedure setmonth(value:integer);
procedure setday(value:integer);
function daysinmonth:integer;
procedure dochange;virtual;
published
property year:integer read fyear write setyear;
property day:integer read fday write setday;
property month:integer read fmonth write setmonth;
property text:string read gettext;
property onchange:tnotifyevent read fonchange write fonchange;
end;
procedure register;
implementation
procedure register ;
begin
registercomponents('md3',[tdate]);
end;
constructor tdate.create(aowner:tcomponent);
var y,m,d:word;
begin
inherited create(aowner);
decodedate(now,y,m,d);
setyear;
setmonth(m);
setday(d);
end;
procedure tdate.dochange;
begin
if assigned(fonchange) then
fonchange(self);
end;
procedure tdate.setyear(value:integer);
begin
fyear:=value;
dochange;
end;
procedure tdate.setmonth(value:integer);
begin
if (value>=1)and (value<=12) then
begin
fmonth:=value;
dochange;
end
else
showmessage('月份超出范围');
end;
procedure tdate.setday(value:integer);
begin
if (value>=1) and (value<=daysinmonth) then
begin
fday:=value;
dochange;
end
else
showmessage('天超过范围');
end;
procedure tdate.increase;
begin
if fday<daysinmonth then
inc(fday)
else
if fmonth<12 then
begin
inc(fmonth);
fday:=1;
end
else
begin
inc(fyear);
fmonth:=1;
fday:=1;
end;
dochange;
end;
function tdate.daysinmonth:integer;
begin
case fmonth of
1,3,5,7,8,10,12:daysinmonth:=31;
4,6,9,11:daysinmonth:=30;
2:
if leapyear then
daysinmonth:=29
else
daysinmonth:=28;
else
daysinmonth:=0;
end;
end;
function tdate.gettext:string;
begin
gettext:=format('%.2d.%.2d.%4d',
[fmonth,fday,fyear]);
end;
procedure tdate.add(numberofdays:integer);
var n:integer;
begin
for n:=1 to numberofdays do
increase;
dochange;
end;
procedure tdate.setvalue(m,d,y:integer);
begin
setyear;
setmonth(m);
setday(d);
end;
function tdate.leapyear:boolean;
begin
if (fyear mod 4 <>0 ) then
result:=false
else if (fyear mod 100<>0)then
result:=true
else if (fyear mod 400<>0) then
result:=false
else
result:=true;
end;
end.
[][][]