Delphi StringGrid控件如何单击选一行,双击编辑单元格,完后继续单击选择一行??
如上图,单击即可选一行,我知道怎么设置。可是要实现编辑goEditing与goRowSelect冲突。我该怎么实现单击选一行,双击编辑单元格呢?...
如上图,单击即可选一行,我知道怎么设置。
可是要实现编辑 goEditing 与 goRowSelect冲突。
我该怎么实现单击选一行,双击编辑单元格呢? 展开
可是要实现编辑 goEditing 与 goRowSelect冲突。
我该怎么实现单击选一行,双击编辑单元格呢? 展开
2个回答
展开全部
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;
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;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不废话上代码
if ((ACol>1) then
begin
stringgrid1.Options:=stringgrid1.Options-[gorowselect] ;
stringgrid1.Options:=stringgrid1.Options+[goediting] ;
end
else
begin
stringgrid1.Options:=stringgrid1.Options+[gorowselect] ;//行选择
stringgrid1.Options:=stringgrid1.Options-[goediting] ;//单元格不能选
end;
if ((ACol>1) then
begin
stringgrid1.Options:=stringgrid1.Options-[gorowselect] ;
stringgrid1.Options:=stringgrid1.Options+[goediting] ;
end
else
begin
stringgrid1.Options:=stringgrid1.Options+[gorowselect] ;//行选择
stringgrid1.Options:=stringgrid1.Options-[goediting] ;//单元格不能选
end;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询