在Delphi6中写的
Unit1.dfm的代码:
object Form1: TForm1
Left = 192
Top = 107
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 56
Top = 28
Width = 60
Height = 13
Caption = #20986#29983#26085#26399#65306
end
object DateTimePicker1: TDateTimePicker
Left = 52
Top = 52
Width = 137
Height = 21
CalAlignment = dtaLeft
Date = 37598
Time = 37598
DateFormat = dfShort
DateMode = dmComboBox
Kind = dtkDate
ParseInput = False
TabOrder = 0
end
object Button1: TButton
Left = 220
Top = 52
Width = 75
Height = 25
Caption = #24180#40836
TabOrder = 1
OnClick = Button1Click
end
end
Unit1.pas的代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;
type
TForm1 = class(TForm)
DateTimePicker1: TDateTimePicker;
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses DateUtils; //要增加的引用单元。
procedure TForm1.Button1Click(Sender: TObject);
var
TempInt:Integer;
begin
TempInt:=YearsBetween(Now,DateTimePicker1.DateTime);
ShowMessage('现在'+IntToStr(TempInt)+'岁了!');
end;
end.
Project1.dpr的代码:
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.