delphi - StringGrid Objects - access violation -
i trying make use of objects property of stringgrid inside descendant , think doing wrong. so, created simple class use in stringgrid cells, like:
type tkind = (tknone,tkstart, tkend, tkmiddle); tmycell = class(tobject) private fkind:string; // tkind: start, middle, end, none foftype:string; // new, registered, paid, over, none factualdate:tdate; fthetext:string; // if want show text in fisweekend:boolean; function isitweekend(dt:tdate):boolean; procedure setkind(s:string); { private declarations } protected { protected declarations } public constructor create; property kind:string read fkind write setkind; property oftype:string read foftype write foftype; property actualdate:tdate read factualdate write factualdate; property thetext:string read fthetext write fthetext; property isweekend:boolean read fisweekend write fisweekend default false; { public declarations } published { published declarations } end; implementation procedure tmycell.setkind(s:string); begin fkind:=s; end; function tmycell.isitweekend(dt:tdate):boolean; begin if (dayoftheweek(dt)=6) or (dayoftheweek(dt)=7) isitweekend:=true else isitweekend:=false; end; constructor tmycell.create; var i:integer; a,l,z:word; dt:tdate; begin fkind:='none'; foftype:='none'; factualdate:=now; fthetext:=''; fisweekend:=isitweekend(factualdate); end;
then, in stringgrid descendant (tmygrid), following:
tmygrid = class(tstringgrid) private fstartselection:integer; ffixedcolor:tcolor; frowcount:integer; ... published property colcount; property rowcount; ... constructor tmygrid.create(aowner: tcomponent); var i:integer; a,l,z:word; dt:tdate; j: integer; mycell:tmycell; begin inherited; ...// different settings rowcount:=5; := 0 colcount-1 j := 0 rowcount-1 begin objects[i, j] := tmycell.create; end; end; destructor tmygrid.destroy; var i,j:integer; begin := 0 colcount-1 j := 0 rowcount-1 begin tmycell(objects[i, j]).free; end; inherited; end; ... // other stuff procedure register; begin registercomponents('mygrid', [tmygrid]); end;
the problem don't know how tell control there more rows when developer changes rowcount in objectinspector before running app. drop stringrid descendant on form, , set rowcount 10. stringgrid not have objects created new rows, cells on arow=5->9 not have objects created... because in oncreate set initial value of rowcount 5 , create objects i:=0 rowcount-1.
is there event or method can tell stringgrid create objects after developer changes rowcount in objectinspector?
i sure problem because, using above code, when drop stringgrid on form , set it's rowcount (design time or runtime) 10 want assign value kind property of cell on row>4 accessviolation, if row <= 4 assignment works fine.
i found should here: http://embarcadero.newsgroups.archived.at/public.delphi.ide/200904/0904193279.html not know how , place code in stringgrid descendant class called when rowcount changed @ designtime/runtime
edit
after reading comments tried idea (that seemed work) override sizechanged (i did not know method existed, must have skipped when serached before). anyway, added code class:
tmygrid = class(tstringgrid) private ... procedure sizechanged(oldcolcount, oldrowcount: longint); override; procedure updategriddimensions(newcolcount, newrowcount: integer); ... procedure tmygrid.sizechanged(oldcolcount, oldrowcount: longint); begin inherited; if (oldrowcount<>frowcount)or(oldcolcount<>colcount) updategriddimensions(colcount, frowcount); end; procedure tmygrid.updategriddimensions(newcolcount, newrowcount: integer); var c, r: integer; old: integer; begin if newcolcount <> colcount begin if newcolcount < colcount begin r := 0 rowcount-1 begin c := colcount-1 downto newcolcount objects[c, r].free; end; end; old := colcount; colcount := newcolcount; if newcolcount > old begin r := 0 rowcount-1 begin c := old newcolcount-1 objects[c, r] := tmycell.create; end; end; end; if newrowcount <> rowcount begin if newrowcount < rowcount begin c := 0 colcount-1 begin r := rowcount-1 downto newrowcount objects[c, r].free; end; end; old := rowcount; rowcount := newcolcount; if newrowcount > old begin c := 0 colcount-1 begin r := old newrowcount-1 objects[c, r] := tmycell.create; end; end; end; end;
but whenever drop control on form, rowcount 93... set rowcount? because dont.
and still, if increase rowcount 93 else 100, objects exist first 93 rows not created 93-100 rows...
so idea sounded great, not work expect it...
any thoughts? doing wrong?
// sizechanged - called when size of grid has changed. protected procedure sizechanged(oldcolcount, oldrowcount: longint); dynamic;
you can override dynamic method sizechanged , initialize grid according new size. can check designtime or not (lu rd suggested link). , david mentioned, better keep objects property consumers of component. create , use own tlist/tobjectlist instead.
Comments
Post a Comment