Delphi StringGrid控件如何单击选一行,双击编辑单元格,完后继续单击选择一行??

转自:http://zhidao.baidu.com/question/526574472.html //有图有说明
1、这个问题小儿科。能用麻溜结贴,不能用就追问,别当没看到。
2、解决思路。StringGrid是这样运作的:goRowSelect就是把某行背景涂上颜色,而goEditing是用一个Edit置于选中的Cell之上。狗屎Delphi自以为聪明,把编辑框对象封装了起来,所以从前者作为突破口要比后者简单得多。
3、解决办法。将StringGrid的Options属性,goEditing设为True,goRowSelect设为False。然后在Form中加入如下代码:
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
FSelRow: Integer;
public
{ Public declarations }
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FSelRow := -1;
end;

procedure TForm1.StringGrid1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
col, row: Longint;
begin
if Button = mbLeft then
begin
StringGrid1.MouseToCell(X, Y, col, row);
if row <> FSelRow then
begin
FSelRow := row;
StringGrid1.Repaint;
end;
end;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if (State = []) and (FSelRow = ARow) then
begin
StringGrid1.Canvas.Brush.Color := clYellow;
StringGrid1.Canvas.FillRect(Rect);
StringGrid1.Canvas.TextOut(Rect.left , Rect.top, StringGrid1.Cells[ACol, ARow]);
end;
end;
posted @ 2013-03-31 16:18  stma  阅读(2981)  评论(0编辑  收藏  举报