就是关于StringGrid输入的问题,我在Edit里可以做到如下的限制:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['.','0'..'9',#8]) then key:=#0;
   {控制小数点不能写两次}
 if (key in ['.']) and (pos('.',Edit1.Text)>0) then key:=#0;
    {控制第一位不能写小数点}   
 if (key in ['.']) and(length(Edit1.Text)<1) then key:=#0;
     {控制第一位只能写一个0} 
 if (key in ['0']) and (pos('.',Edit1.Text)<1)and(copy(Edit1.Text,1,1)='0') then key:=#0;
end;  
 那么我在StringGrid里,把Edit1.Text改为StringGrid1.Cells[StringGrid1.Col,StringGrid1.Row]为什么不行?

解决方案 »

  1.   

    //try
    function GetGridEdit(mGrid: TCustomGrid): TCustomEdit;
    var
      I: Integer;
    begin
      Result := nil;
      for I := 0 to mGrid.ControlCount - 1 do
        if mGrid.Controls[I] is TCustomEdit then
          TControl(Result) := mGrid.Controls[I];
    end; { GetGridEdit }procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
    var
      vEdit: TCustomEdit;
    begin
      vEdit := GetGridEdit(TStringGrid(Sender));
      if not Assigned(vEdit) then Exit;
      if not (Key in ['.', '0'..'9', #8]) then Key := #0;
      if (Key in ['.']) and (Pos('.', vEdit.Text) > 0) then Key := #0;
      if (Key in ['.']) and (Length(vEdit.Text) < 1) then Key := #0;
      if (Key in ['0']) and (Pos('.', vEdit.Text) < 1) and
        (Copy(vEdit.Text, 1, 1) = '0') then Key := #0;
    end;
      

  2.   

    楼主: 这个判断if (key in ['.']) and(length(Edit1.Text)<1) then key:=#0;
         {控制第一位不能写小数点}
    是不是不安全呀?
    如果操作员先在EDIT1里写了有值,没有注意将光标定位到第一位然后输入点数点,就不能拦截了!