【问题标题】:Delphi mask for binary numbers二进制数的德尔福掩码
【发布时间】:2013-03-27 03:48:36
【问题描述】:

我有 StringGrid 并且只想在其中包含单元格 10。我尝试使用 StringGridGetEditMask

procedure TForm1.StringGrid1GetEditMask(Sender: TObject; ACol,
  ARow: Integer; var Value: String);
begin
  Value := '0';
  if not (strToInt(Value) in [0,1]) then value := #0;
end;

但是我可以输入从 0 到 9 的所有数字。如何过滤除 0 和 1 之外的所有数字?

【问题讨论】:

  • 我没有收到您的代码。如果将值'0' 分配给Value,那么StrToInt(Value) 肯定等于0
  • 我将 value 转换为整数并与 0 和 1 进行比较。如果 value 不是 0 或不是 1,则 value 将等于空字符。

标签: delphi mask


【解决方案1】:

为了您的意图,您需要将TStringGrid 类子类化,并在此类子类中分配给就地编辑器的例如OnKeyPress 事件,如此插入器类中所示:

type
  TStringGrid = class(Grids.TStringGrid)
  private
    procedure InplaceEditKeyPress(Sender: TObject; var Key: Char);
  protected
    function CreateEditor: TInplaceEdit; override;
  end;

implementation

{ TStringGrid }

function TStringGrid.CreateEditor: TInplaceEdit;
begin
  Result := inherited CreateEditor;
  TMaskEdit(Result).OnKeyPress := InplaceEditKeyPress;
end;

procedure TStringGrid.InplaceEditKeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in [#8, '0', '1']) then
    Key := #0;
end;

【讨论】:

  • 当然,您应该更喜欢使用 CharInSet 而不是 in 运算符,正如 Unicode Delphi 警告所暗示的那样,但我没有在这里使用它,因为您没有告诉我们您的 Delphi 版本.
【解决方案2】:

您误解了OnGetEditMask 事件。 Value 不是您可以更改的新字符串,而是您应该给控件一个掩码 的地方。然而不幸的是,edit masks 不允许您请求的功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多