2013-03-26 51 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不允許您請求的功能。