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

时间:2017-06-12 14:40:57

标签: delphi delphi-7

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

1 个答案:

答案 0 :(得分:1)

在输入过程中输入文本中更改的任何格式都不正确,因此以下显示了如何执行此操作,但输入仅在退出字段或按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.