我想控制StringGrid的输入
比如,每个单元只能输入两个字符
继续输入将跳到下一个单元
请问该怎么实现?

解决方案 »

  1.   

    在Tstringgrid.ondrawcell事件中:DrawText(StringGrid1.Canvas.Handle,pchar(StringGrid1.Cells[Acol,Arow]),Length(StringGrid1.Cells[Acol,Arow]),Rect,DT_WORDBREAK or DT_LEFT);可以实现文字换行!
      

  2.   

    procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,
      ARow: Integer; const Value: String);
    begin
       if length(value)>=2 then
       begin
           if (ACol=StringGrid1.ColCount-1) and (ARow=StringGrid1.RowCount-1) then exit;
           if ACol<StringGrid1.ColCount-1 then
           begin
               StringGrid1.Col  :=ACol+1;
           end
           else
           begin
              StringGrid1.Row :=ARow+1;
              StringGrid1.Col  :=1;
           end;
           stringgrid1.SetFocus;
       end;end;
      

  3.   

    在Tstringgrid.ondrawcell事件中:DrawText(StringGrid1.Canvas.Handle,pchar(StringGrid1.Cells[Acol,Arow]),Length(StringGrid1.Cells[Acol,Arow]),Rect,DT_WORDBREAK or DT_LEFT);可以实现文字换行!
      

  4.   

    继承,然后强制设置InplaceEditor的MaxLength。
    或者在AfterEdit(或类似的事件)中截断字符串。
      

  5.   

    procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,
      ARow: Integer; const Value: String);
    begin
       if length(value)>=2 then
       begin
           if (ACol=StringGrid1.ColCount-1) and (ARow=StringGrid1.RowCount-1) then
           begin
              StringGrid1.Row :=1;
              StringGrid1.Col :=1;
              exit;
           end;
           if ACol<StringGrid1.ColCount-1 then
           begin
               StringGrid1.Col  :=ACol+1;
           end
           else
           begin
              StringGrid1.Row :=ARow+1;
              StringGrid1.Col  :=1;
           end;
           stringgrid1.SetFocus;
       end;end;