Arsip: komponen TMyDateEdit


by masdi2t in Articles more 14 years ago 1219
Berikut ini source komponen TMyDateEdit.
masukkan data tanggal, bulan dan tahun pada property Day, Month dan Year. liat apa yang terjadi. edit juga ketika run-time
unit uMyEdit;
interface
uses
Classes, SysUtils, Windows, JvEdit;
type
TMyCustomDateEdit = class(TJvCustomEdit)
private
FSupressOnChange: Boolean;
FChanging: Boolean;
FMonth: Byte;
FDay: Byte;
FYear: Integer;
procedure SetDay(const Value: Byte);
procedure SetMonth(const Value: Byte);
procedure SetYear(const Value: Integer);
protected
procedure UnFormatText; virtual;
procedure DoEnter; override;
procedure DoExit; override;
procedure Change; override;
procedure KeyPress(var Key: Char); override;
function CheckDay(Value: Byte): Byte;
function CheckMonth(Value: Byte): Byte;
function CheckYear(Value: Integer): Integer;
public
constructor Create(AOwner: TComponent); override;
procedure Loaded; override;
procedure FormatText; virtual;
property Day: Byte read FDay write SetDay default 1;
property Month: Byte read FMonth write SetMonth default 1;
property Year: Integer read FYear write SetYear default 2006;
end;
TMyDateEdit = class(TMyCustomDateEdit)
public
constructor Create(AOwner: TComponent); override;
published
property Align;
property Alignment;
property Anchors;
property BiDiMode;
property BorderStyle;
property Color;
property Constraints;
property Ctl3D;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property HideSelection;
property ImeMode;
property ImeName;
property MaxLength;
property OEMConvert;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property Day;
property Month;
property Year;
property TabOrder;
property TabStop;
property Visible;
property OnChange;
property OnClick;
property OnDblClick;
property OnEnter;
property OnExit;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property AutoSelect;
property ClipBoardCommands;
property DisabledTextColor;
property DisabledColor;
end;
const
ERR_MSG = 'invalid date';
procedure Register;
implementation
{ TMyCustomDateEdit }
procedure Register;
begin
RegisterComponents('Samples', [TMyDateEdit]);
end;
procedure TMyCustomDateEdit.Change;
begin
if not FSupressOnChange then
inherited Change;
{ change after lose focus? o-er! }
if not Focused and not FChanging then
begin
FChanging := True;
try
FormatText;
finally
FChanging := False;
end;
end;
end;
function TMyCustomDateEdit.CheckDay(Value: Byte): Byte;
begin
{$MESSAGE HINT 'need more checking for valid date'}
if (Value in [1..31]) then
Result := Value
else
Result := 1;
end;
function TMyCustomDateEdit.CheckMonth(Value: Byte): Byte;
begin
if (Value in [1..12]) then
Result := Value
else
Result := 1;
end;
function TMyCustomDateEdit.CheckYear(Value: Integer): Integer;
begin
Result := Value;
end;
constructor TMyCustomDateEdit.Create(AOwner: TComponent);
begin
FSupressOnChange := True;
FChanging := False;
inherited Create(AOwner);
end;
procedure TMyCustomDateEdit.DoEnter;
begin
FSupressOnChange := True;
try
UnFormatText;
inherited DoEnter;
finally
FSupressOnChange := False;
end;
end;
procedure TMyCustomDateEdit.DoExit;
begin
FSupressOnChange := True;
try
inherited DoExit;
SetDay(StrToInt(Text));
FormatText;
finally
FSupressOnChange := False;
end;
end;
procedure TMyCustomDateEdit.FormatText;
var
dDate: TDateTime;
c: Char;
begin
c := DateSeparator;
if (TryStrToDate(IntToStr(FDay) + c + IntToStr(FMonth) + c +
IntToStr(FYear), dDate)) then
Text := FormatDateTime('dddd, dd mmmm yyyy', dDate)
else
Text := ERR_MSG;
end;
procedure TMyCustomDateEdit.KeyPress(var Key: Char);
begin
if (Key in ['0'..'9', Chr(VK_BACK)]) then
inherited KeyPress(Key)
else
Key := #0;
end;
procedure TMyCustomDateEdit.Loaded;
begin
inherited Loaded;
FSupressOnChange := False;
end;
procedure TMyCustomDateEdit.SetDay(const Value: Byte);
begin
FDay := CheckDay(Value);
FormatText;
end;
procedure TMyCustomDateEdit.SetMonth(const Value: Byte);
begin
FMonth := CheckMonth(Value);
FormatText;
end;
procedure TMyCustomDateEdit.SetYear(const Value: Integer);
begin
FYear := CheckYear(Value);
FormatText;
end;
procedure TMyCustomDateEdit.UnFormatText;
var
lsText: string;
begin
if (Text = ERR_MSG) then
Text := '1'  // default value for Day property
  else
Text := IntToStr(FDay);
end;
{ TMyDateEdit }
constructor TMyDateEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDay := 1;
FMonth := 1;
FYear := 2006;
FormatText;
end;
end.
Syntax Highlighted with https://pascal-id.org/syntax


Silahkan dicoba!
Local Business Directory, Search Engine Submission & SEO Tools FreeWebSubmission.com SonicRun.com