to japhe 我不想用后判断的这种,我起初也是这样做的
唯一要解决的是怎样将Tstringgrid的每个cell当成TCustomEdit来做
TComponent
TControl
TWinControl
TCustomControl
...TCustomDrawGrid
.....TDrawGrid
.......TStringGrid
TCustomEdit
SelLength, SelStart 能否 在tstringgrid中实现?
这样我就可以通过下列方法控制用户录入
procedure FormatKeyPress(Sender:TObject;var Key:Char;Digits:integer);
var
myEdit:tsuiEdit;
begin
if (Sender is TSuiEdit) then
begin
myEdit:= Sender as Tsuiedit;
if (Key=#27) or (Key=#8) or (myEdit.Tag=0) then exit;
if myEdit.Tag=1 then
if not (Key in ['0'..'9','+','-'] ) then Key:=#0;
if myEdit.Tag=2 then
if not (Key in ['0'..'9','+','-','.'] ) then Key:=#0;
//控制+-
if (Key ='-') or (Key='+' ) then
begin
if ((Pos('-',myEdit.Text) > 0) or (Pos('+',myEdit.Text) > 0 )) and
(myEdit.SelLength=0 ) then Key:=#0;
if myEdit.SelStart > 0 then Key:=#0;
end;
//控制.
if (Key = '.') and (myEdit.Tag=2 ) then
begin
if (Pos('.',myEdit.Text)>0) then
begin
if (myEdit.SelStart+myEdit.SelLength<Pos('.',myEdit.Text))
or (myEdit.SelStart>=Pos('.',myEdit.Text)) then Key:=#0;
if myEdit.SelLength=0 then Key:=#0;
end;
if myEdit.SelStart=0 then Key:=#0;
if (Digits>0) and (myEdit.SelStart>0) and (myEdit.Tag=2 ) then
if length(myEdit.Text)-myEdit.SelStart-myEdit.SelLength>Digits then Key:=#0;
end;
if (Digits>0) and (myEdit.SelStart>0) and (myEdit.SelLength=0) and (myEdit.Tag=2 ) then
if (pos('.',myEdit.Text )>0) and (myEdit.SelStart>=pos('.',myEdit.Text)) then
if length(myEdit.Text)-pos('.',myEdit.Text )>=Digits then Key:=#0;
end;
end;