怎样改变stinggrid单元格的输入背景颜色?

sunxking 2002-05-05 09:51:51
我再StringGrid里输入时,当前cell背景总是白色的,要怎么把它改为其他色呢?
我再setedit事件里fillrect发现改不了啊?那位给个答案?
...全文
79 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyfhz 2002-05-20
  • 打赏
  • 举报
回复
用控件不好吗?别忘了Delphi5的 dfm文件是文本格式的,一个字符串替换
就可以把控件换掉了:)
sunxking 2002-05-19
  • 打赏
  • 举报
回复
不用控件就真的没法实现吗?
untiltarget 2002-05-15
  • 打赏
  • 举报
回复
我这里有一个程序,要的话发EMAIL给我,我发给你。
shap_it@163.com
yyfhz 2002-05-14
  • 打赏
  • 举报
回复
我查了一下Delphi 的代码,发现它在Grid里面有一个类似Edit的东东,
当要进行修改的时候就把它显示在相应的单元格里,你看见的白色的输入框
其实就是那个edit.不幸的是那个edit是个内部变量,从外面很难进行操作。
不过它的创建函数 function CreateEditor: TInplaceEdit 倒是可以重载的。
因此我自己写了一个控件,目的是当它变成编辑状态时显示的字符是红底的。
当然你也可以用其它的方法得到类似的效果,例如在相应的位置上显示一个
红色的Edit控件骗骗用户,不过你还得让那个Edit能随着Grid的滚动而移动,
代码写起来比较罗嗦,还是写一个控件来得方便。
sunxking 2002-05-07
  • 打赏
  • 举报
回复
以上代码没用,那位高手有好的办法?
sunxking 2002-05-05
  • 打赏
  • 举报
回复
我写了这段代码:
procedure Tgoodsin.StringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
begin

with Sender as TStringGrid do
begin
if gdFocused in State then
begin
Canvas.Brush.Color := $1FFFFF1F;
Canvas.FillRect(Rect);
Canvas.TextOut(Rect.Left,Rect.Top,MSGgin.Sg.Cells[ACol,ARow]);
Canvas.DrawFocusRect(Rect);
end;
end;

end;
但是,没用啊!
lizhenjia 2002-05-05
  • 打赏
  • 举报
回复
StringGrid1.color:=clred
lxycsdn 2002-05-05
  • 打赏
  • 举报
回复
只你在StringGrid1SelectCell事件中利用Canvas+Rect会画出你想要要的颜色。画布你总用过吧!
sunxking 2002-05-05
  • 打赏
  • 举报
回复
to lizhenjia(暴雪):
现象是选中的那一整行颜色为红,
进入编辑后那个单元格的背景还是白色的。
另外,editing肯定是true了,呵呵,
不然怎么输入的呢?

to yyfhz(火山) :
你这个代码实现的功能看起来不像是针对我的问题的啊!?
不过我没具体试过,你能说说你的代码到底实现什么功能吗?
yyfhz 2002-05-05
  • 打赏
  • 举报
回复
写一个控件吧,不难的:)
TGrid = class(TStringGrid)
protected
function CreateEditor: TInplaceEdit; override ;
end ;

function TGrid.CreateEditor: TInplaceEdit;
begin
result := inherited CreateEditor ;
Result.Brush.Color := clRed ;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
rGrid : TGrid ;
begin
rGrid := TGrid.Create(self) ;
rGrid.Parent := self ;
rGrid.Left := 0 ;
rGrid.Top := 0 ;
rGrid.Visible := True ;
rGrid.Options := rGrid.Options + [goEditing] ;
end;

看看效果如何
lizhenjia 2002-05-05
  • 打赏
  • 举报
回复
不可能.现象是什么,之前要确保stringdbgrid的options的goEditing是true!
sunxking 2002-05-05
  • 打赏
  • 举报
回复
to lizhenjia(暴雪) :
你好,我试了,还是不能改变当前正在输入的一个cell的背景色。
还有其他的好方法吗?
lizhenjia 2002-05-05
  • 打赏
  • 举报
回复
ondrawcell中加入:
var
strgrid:Tstringgrid;
begin
strgrid:=sender as tstringgrid;
if (row=strgrid.row) and (strgrid.Focused) then
strgrid.Canvas.Brush.Color := clRed
else
strgrid.Canvas.Brush.Color := clWindow;
strgrid.Canvas.FillRect(Rect);
end;
sunxking 2002-05-05
  • 打赏
  • 举报
回复
那个好兄弟说说啊!
我在select和setedit两个时间中试了都没办法!到底应该怎么做啊?
sunxking 2002-05-05
  • 打赏
  • 举报
回复
to lizhenjia(暴雪):
多谢你的回复,不过,color是用来改变所有的单元格,
我要改变的是正在编辑中的单元格。

5,391

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧