在Tstringgrid.ondrawcell事件中:DrawText(StringGrid1.Canvas.Handle,pchar(StringGrid1.Cells[Acol,Arow]),Length(StringGrid1.Cells[Acol,Arow]),Rect,DT_WORDBREAK or DT_LEFT);可以实现文字换行!
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;
在Tstringgrid.ondrawcell事件中:DrawText(StringGrid1.Canvas.Handle,pchar(StringGrid1.Cells[Acol,Arow]),Length(StringGrid1.Cells[Acol,Arow]),Rect,DT_WORDBREAK or DT_LEFT);可以实现文字换行!
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;
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;
或者在AfterEdit(或类似的事件)中截断字符串。
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;