读书人

StringGrid编辑状态怎么让文字换行

发布时间: 2012-04-04 16:38:51 作者: rapoo

StringGrid编辑状态如何让文字换行
直接打回车就变成编辑完成了,还请指点

[解决办法]
1、如果楼主的意图是“在某一格里输入回车键,则退出编辑状态”:

只需把StringGrid的Options设置为[goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRangeSelect,goEditing]即可

注意,仅是比默认设置多了一个goEditing

---------------------------------

2、如果楼主的意图是“在某一格里输入回车键,则该格文本换行继续输入”:

procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
with TStringGrid(Sender) do
if Key=#13 then
begin
Cells[Col,Row] := Cells[Col,Row]+#13+#10;
Options := [goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRangeSelect];
Options := [goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRangeSelect,goEditing,goAlwaysShowEditor];
end;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var //DrawCell代码参考自 老冯 的一个例子
HCell: Integer;
HRow: Integer;
SCell: string;
begin
with TStringGrid(Sender) do
begin
SCell := Cells[ACol, ARow];
HRow := RowHeights[ARow];
Canvas.FillRect(Rect);
HCell := DrawText(Canvas.Handle, PChar(SCell), Length(SCell), Rect, 0 );
if HCell > HRow then RowHeights[ARow] := HCell;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Options := [goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRangeSelect,goEditing,goAlwaysShowEditor];
end;
//楼主可以在某一格里输入一行文本,然后打回车再输入一行,然后把焦点移至其他格子看一下

读书人网 >.NET

热点推荐