掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
Editで最大、最小などの制限を設けたもの (ID:37468)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
TEditを拡張したコンポを作ってみました。ほぼSampleのSpinEditの移植です。 一応ちゃんと動くようですが、コンポ作りは(も)素人なので、どなたか突っ込みを入れてください。 あくまでこんな事もできるよ、ということで。 ここから unit EditLimited; interface uses SysUtils, Classes, Controls, StdCtrls, Messages, Windows ; type TEditLimited = class(TEdit) private FMinValue: LongInt; FMaxValue: LongInt; FEditorEnabled: Boolean; function CheckValue (NewValue: LongInt): LongInt; function GetValue: LongInt; procedure SetValue (NewValue: LongInt); protected function IsValidChar(Key: Char): Boolean; virtual; procedure KeyPress(var Key: Char); override; procedure Change; override; public constructor Create(AOwner: TComponent); override; published property MaxValue: LongInt read FMaxValue write FMaxValue; property MinValue: LongInt read FMinValue write FMinValue; property Value: LongInt read GetValue write SetValue; end; procedure Register; implementation procedure Register; begin RegisterComponents('Sample', [TEditLimited]); end; constructor TEditLimited.Create(AOwner: TComponent); begin inherited Create(AOwner); Text := '0'; FEditorEnabled := True; end; function TEditLimited.GetValue: LongInt; begin try Result := StrToIntDef(Text, 0); except Result := FMinValue; end; end; procedure TEditLimited.SetValue (NewValue: LongInt); begin Text := IntToStr (CheckValue (NewValue)); end; function TEditLimited.CheckValue (NewValue: LongInt): LongInt; begin Result := NewValue; if (FMaxValue <> FMinValue) then begin if NewValue < FMinValue then Result := FMinValue else if NewValue > FMaxValue then Result := FMaxValue; end; end; procedure TEditLimited.KeyPress(var Key: Char); begin if not IsValidChar(Key) then begin Key := #0; end; if Key <> #0 then begin inherited KeyPress(Key); end end; procedure TEditLimited.Change; begin if CheckValue (Value) <> Value then SetValue (Value); end; function TEditLimited.IsValidChar(Key: Char): Boolean; begin Result := (Key in [DecimalSeparator, '+', '-', '0'..'9']) or ((Key < #32) and (Key <> Chr(VK_RETURN))); if not FEditorEnabled and Result and ((Key >= #32) or (Key = Char(VK_BACK)) or (Key = Char(VK_DELETE))) then Result := False; end; end. ここまでをEditLimited.pasというファイル名で保存して、コンポーネント-インストールしてください。 使い方は普通のEditと同じで、SpinEditのようにMaxValue、MinValueがあります。 インストールの仕方が分からないときは、また質問してください。
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.