2013-03-26 53 views
2

我有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?

+1

我没有得到您的代码。如果将值“0”赋值给“Value”,那么当然'StrToInt(Value)'将等于'0'? – 2013-03-26 19:38:43

+0

我将值转换为整数并与0和1进行比较。如果值不是0或不是1,则值将等于空字符。 – Vlad 2013-03-26 19:45:38

回答

3

为了您的意图,您需要继承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; 
+0

当然,你应该更喜欢使用'CharInSet'而不是''in'运算符,因为Unicode Delphi警告提示,但是我没有在这里使用它,因为你没有告诉我们你的Delphi版本。 – TLama 2013-03-26 19:59:31

2

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