德尔福小时:在tEdit中输入的分钟数



我没有想法,所以我呼吁巨大的StackOverflow超级头脑。我想要的是一个 tedit 或任何文本控件,让我输入以下格式的文本:"nnnnnn:nn",其中 n 是一个整数。示例:如果我键入"100",我需要一个"100:00"文本属性。如果我输入"123:1",我应该得到"123:01"。也许我应该只在固定位置使用":"分隔符以计算器样式键入数字。我想拒绝这样的"10:1"、"10:95"(0-59 分钟)、"0100:10"等。有什么想法或组件吗? 问候,马塞洛。

输入文本在输入过程中更改的任何格式都是错误的,因此下面向您展示了如何操作,但只有在退出字段或按 Enter 键时才会更改输入:

编辑1 是有问题的编辑字段。Edit2 只是为了允许 Tab 键退出 Edit1。

请注意,我使用的是标准的 evets 和事件名称(OnKeyPress 和 OnExit)

unit Unit10;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.StrUtils, Vcl.Mask;
type
TForm10 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure Edit1Exit(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function EntryValid( const pVal : string ) : boolean;
end;
var
Form10: TForm10;
implementation
{$R *.dfm}
{ TComboBox }
procedure TForm10.Edit1Exit(Sender: TObject);
var
iPos : integer;
iCheck : string;
i1 : string;
begin
iPos := Pos( ':', Edit1.Text );
if iPos > 0 then
begin
// we already know there can only be one ':'
i1 := Copy( Edit1.Text, 1, iPos );
iCheck := Copy(Edit1.Text, iPos + 1 );
if iCheck = '' then
begin
Edit1.Text := i1 + '00';
end
else if StrToInt( iCheck ) < 10 then
begin
Edit1.Text := i1 + '0' + iCheck;
end
else
begin
// already correct, so ignore
end;
end
else
begin
Edit1.Text := Edit1.Text + ':00';
end;
end;
procedure TForm10.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
// only allow numbers and a single :
case Key of
'0'..'9': ;
':':
begin
if Pos( ':', Edit1.Text ) <> 0 then
begin
Key := #0; // don't allow
Beep;
end;
end;
#13:
begin
Key := #0; // silently this time
Edit1Exit( Sender );
end
else
begin
Key := #0;
Beep;
end;
end;
end;
function TForm10.EntryValid(const pVal: string): boolean;
var
iPos : integer;
iCheck : string;
begin
iPos := Pos( ':', pVal );
if iPos > 0 then
begin
// we already know there can only be one ':'
iCheck := Copy( pVal, iPos + 1 );
if iCheck = '' then
begin
Result := TRUE;
end
else if StrToIntDef( iCheck, 60 ) < 60 then
begin
Result := TRUE;
end
else
begin
Result := FALSE;
end;
end
else
begin
Result := TRUE;
end;
end;
end.

最新更新