loadstringgrid(stringgrid1, 'c:.txt');
end;
*******************************************
打开一个已有的文本文件,并将内容放到stringgrid中,文本行与stringgrid行一致;
在文本中遇到空格则放入下一cells.
搞定!注意,我只写了一个空格间隔的,你自己修改一下splitstring可以用多个空格分隔!
procedure tform1.button1click(sender: tobject);
var
aa,bb:tstringlist;
i:integer;
begin
aa:=tstringlist.create;
bb:=tstringlist.create;
aa.loadfromfile('c:.txt');
for i:=0 to aa.count-1 do
begin
bb:=splitstring(aa.strings[i],' ');
stringgrid1.rows[i]:=bb;
end;
aa.free;
bb.free;
end;
其中splitstring为:
function splitstring(const source,ch:string):tstringlist;
var
temp:string;
i:integer;
begin
result:=tstringlist.create;
temp:=source;
i:=pos(ch,source);
while i<>0 do
begin
result.add(copy(temp,0,i-1));
delete(temp,1,i);
i:=pos(ch,temp);
end;
result.add(temp);
end;
stringgrid组件cells内容对齐
在stringgrid的drawcell事件中添加类似的代码就可以了:
var
vcol, vrow : longint;
begin
vcol := acol; vrow := arow;
with sender as tstringgrid, canvas do
if vcol = 2 then begin ///对于第2列设置为右对齐
settextalign(handle, ta_right);
fillrect(rect);
textrect(rect, rect.right-2, rect.top+2, cells[vcol, vrow]);
end;
end;
当我将stringgird的options属性中包含gorowselect项时每当我选中stringgrid中一行, 则选中行用深蓝色显示,我想将深蓝色改为其他颜色应怎样该?当我将stringgird的options属性中包含gorowselect项时每当我选中stringgrid中一行, 则选中行用深蓝色显示,我想将深蓝色改为其他颜色应怎样该?
procedure tform1.stringgrid1drawcell(sender: tobject; acol, arow: integer;
rect: trect; state: tgriddrawstate);
begin
with stringgrid1 do
begin
if (arow= krow) and not (acol = 0) then
begin
canvas.brush.color :=clyellow;// clblue;
canvas.fillrect(rect);
canvas.font.color:=clblack;
canvas.textout(rect.left , rect.top, cells[acol, arow]);
end;
end;
end;
procedure tform1.stringgrid1selectcell(sender: tobject; acol,
arow: integer; var canselect: boolean);
begin
krow := arow; //*
kcol := acol;
end;
注意:必须把变量krow的值初始为1或其他不为0的值,否则如果锁定第一行的话,第一行的颜色将被自设颜色取代,而锁定行不会被重画。
怎么改变stringgrid控件某一列的背景和某一列的只读属性,stringgrid控件标题栏的对齐.怎么改变stringgrid控件某一列的背景和某一列的只读属性,stringgrid控件标题栏的对齐.
请参考以下代码:
在ondrawcell事件中处理背景色。程序如下:
//将第二列背景变为红色。
procedure tform1.stringgrid1drawcell(sender: tobject; acol, arow: integer;
rect: trect; state: tgriddrawstate);
begin
if not((acol=1) and (arow>=stringgrid1.fixedrows)) then exit;
with stringgrid1 do
begin
canvas.brush.color:=clred;
canvas.fillrect(rect);
canvas.textout(rect.left+2,rect.top+2,cells[acol,arow])
end;
end;
//加入如下代码,那么stringgrid的第四列就只读了.其他列非只读
procedure tform1.stringgrid1selectcell(sender: tobject; acol, arow: integer; var canselect: boolean);
begin
with stringgrid1 do begin
if acol = 4 then
options := options - [goediting]
else options := options + [goediting];
end;
procedure tform1.stringgrid1drawcell(sender: tobject; acol, arow: integer; rect: trect; state: tgriddrawstate);
var
dx,dy:byte;
begin
if (acol = 4) and not (arow = 0) then
with stringgrid1 do
begin
canvas.brush.color := clyellow;
canvas.fillrect(rect);
canvas.font.color := clblue;
dx:=2;//调整此值,控制字在网格中显示的水平位置
dy:=2;//调整此值,控制字在网格中显示的垂直位置
canvas.textout(rect.left+dx , rect.top+dy , cells[acol, arow]);
end;
//控制标题栏的对齐
if (arow = 0) then
with stringgrid1 do
begin
canvas.brush.color := clbtnface;
canvas.fillrect(rect);
dx := 12; //调整此值,控制字在网格中显示的水平位置
dy := 5; //调整此值,控制字在网格中显示的垂直位置
canvas.textout(rect.left + dx, rect.top + dy, cells[acol, arow]);
end;
end;
在stringgrid中使用回车键模拟tab键切换单元格的功能实现......procedure tform1.stringgrid1keypress(sender: tobject; var key: char);
label
nexttab;
begin
if key=#13 then
begin
key:=#0;
nexttab:
if (stringgrid1.col begin
stringgrid1.col:=stringgrid1.col+1;
end
else
begin
if stringgrid1.row>=stringgrid1.rowcount-1 then
stringgrid1.rowcount:=stringgrid1.rowcount+1;
stringgrid1.row:=stringgrid1.row+1;
stringgrid1.col:=0;
goto nexttab;
end;
end;
end;
.........
stringgrid1.col:=stringgrid1.col+1;
end
else
begin
if stringgrid1.row>=stringgrid1.rowcount-1 then
stringgrid1.rowcount:=stringgrid1.rowcount+1;
stringgrid1.row:=stringgrid1.row+1;
stringgrid1.col:=0;
goto nexttab;
end;
end;
end;
.........