procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char); begin if not (Key in ['1','0']) then Key:=#0 //建议写成 //if not (Key in ['1','0',#13,#8]) then Key:=#0 //这样就可以回车和退格了 end;
stringgrid的onkeypress事件中 if not (key in ['0','1',#13,#8]) and (length(stringgrid1.cells[stringgrid1.row,stringgrid1.col]) <> 0) then begin key := #0; end;
procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char); begin if key in ['1'] then begin stringgrid1.Cells[stringgrid1.Col,stringgrid1.Row]:=''; stringgrid1.Cells[stringgrid1.Col,stringgrid1.Row]:='1'; end else if key in ['0'] then begin stringgrid1.Cells[stringgrid1.Col,stringgrid1.Row]:=''; stringgrid1.Cells[stringgrid1.Col,stringgrid1.Row]:='0'; end else begin key:=#0; exit; end end;
//////////////// .......... private { Private declarations } currvalue : String; ///////////////............. procedure TForm1.StringGrid1GetEditText(Sender: TObject; ACol, ARow: Integer; var Value: String); begin currvalue := value; end;procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer; const Value: String); begin if ( (StringGrid1.Cells[ACol,ARow] <> '1') and (StringGrid1.Cells[ACol,ARow] <>'0')) then StringGrid1.Cells[ACol,ARow] := currvalue; end;_____________________________________________________________________暮春三月,羊欢草长,天寒地冻,问谁饲狼?人心怜羊,狼心独怆,天心难测,世情如 霜……
if key 不在0和1 then
key=#0
begin
if not (Key in ['1','0']) then Key:=#0
//建议写成
//if not (Key in ['1','0',#13,#8]) then Key:=#0
//这样就可以回车和退格了
end;
if not (key in ['0','1',#13,#8]) and
(length(stringgrid1.cells[stringgrid1.row,stringgrid1.col]) <> 0) then
begin
key := #0;
end;
begin
if key in ['1'] then
begin
stringgrid1.Cells[stringgrid1.Col,stringgrid1.Row]:='';
stringgrid1.Cells[stringgrid1.Col,stringgrid1.Row]:='1';
end
else
if key in ['0'] then
begin
stringgrid1.Cells[stringgrid1.Col,stringgrid1.Row]:='';
stringgrid1.Cells[stringgrid1.Col,stringgrid1.Row]:='0';
end
else
begin
key:=#0;
exit;
end
end;
..........
private
{ Private declarations }
currvalue : String;
///////////////.............
procedure TForm1.StringGrid1GetEditText(Sender: TObject; ACol,
ARow: Integer; var Value: String);
begin
currvalue := value;
end;procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,
ARow: Integer; const Value: String);
begin if ( (StringGrid1.Cells[ACol,ARow] <> '1') and (StringGrid1.Cells[ACol,ARow] <>'0')) then
StringGrid1.Cells[ACol,ARow] := currvalue;
end;_____________________________________________________________________暮春三月,羊欢草长,天寒地冻,问谁饲狼?人心怜羊,狼心独怆,天心难测,世情如
霜……