inthe
asked on
color and saving in stringgrids (part 2)
Howdy all
im reask this question on behalf of SRP as he accepted the first answer but its not exactly what he after and i dunno how to make a grid color a cell on a button click and keep it saved after other cells are selected :
original question:
<SRP>
I am working on a project in Delphi and need to use a stringgrid and allow the user to select cells and click a button. The onclick event needs to change the color of selected cells and save the cells coordinates in a file.
<ME>
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, StdCtrls;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1DrawCell(Sender : TObject; Col, Row: Longint;
Rect: TRect; State: TGridDrawState);
procedure StringGrid1SelectCell(Send er: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function WriteToFile(const path1, s1 : string): boolean;
var
f1 : textfile;
begin
WriteToFile:= false;
try
AssignFile(f1, path1);
if FileExists(path1)= false then Rewrite(f1);
Append(f1);
Writeln(f1, s1);
Flush(f1);
CloseFile(f1);
WriteToFile:= true;
except on EinOutError do
WriteToFile:= false;
end;
end;
procedure TForm1.StringGrid1DrawCell (Sender: TObject; Col, Row: Longint;
Rect: TRect; State: TGridDrawState);
var
FocusedRect : TRect;
begin
FocusedRect := Rect;
StringGrid1.Canvas.Font.Co lor := clBlack;
StringGrid1.Canvas.Pen.Col or := clBlack;
StringGrid1.Canvas.Pen.Wid th := 1;
if ( gdFocused in State ) then begin
StringGrid1.Canvas.Font.St yle := [fsBold];
StringGrid1.Canvas.Font.Co lor := clRed;
StringGrid1.Canvas.Brush.C olor := clblue;
end
else
StringGrid1.Canvas.Font.St yle := [];
InflateRect(FocusedRect, -2, -2);
StringGrid1.Canvas.FillRec t(FocusedR ect);
if ( gdSelected in State ) then
StringGrid1.Canvas.TextOut (Rect.Left +8, Rect.Top+2, StringGrid1.Cells[Col,Row] ) else
StringGrid1.Canvas.TextOut (Rect.Left +2, Rect.Top+2, StringGrid1.Cells[Col,Row] )
end;
procedure TForm1.StringGrid1SelectCe ll(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
var
s : string;
begin
s := Inttostr(ACol) + Inttostr(ARow);
writetofile('f:\cells.txt' ,s);
end;
end.
dunno who wrote the writetofile function but it works nicely
Regards Barry
<SRP>
The solution works very well, but I need to change how it works. I need the button click to save the coordinates and color the cell and not the selectcell event. Also is it possible to color the all of the selcted cells?
<ME>
saving the row/col is easy enough on a button click by saving it in a global var instead then writing the file on the button click..
var
Form1: TForm1;
s : string;
implementation
{$R *.DFM}
procedure TForm1.StringGrid1SelectCe ll(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
s := Inttostr(ACol) + Inttostr(ARow);
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
writetofile('f:\cells.txt' ,s);
paint;
end;
end.
<ME>
the difficult part is painting the selected cells and not using the ondrawcells event,ive never seen code that doesnt use the drawcell event, i had a go but couldnt get that part to work right.
<ME>
you could try doing nothing on drawcell event and calling stringgrid.repaint on buttonclick but that will only color the selected cell to default blue..maybe thats what you want im not sure..
<SRP>
I need the button click to change the cell color, so that it remains colored after it is selected and will always be colored.
srp feel free to comment and tell me when it is as you need
cheers ;-)
im reask this question on behalf of SRP as he accepted the first answer but its not exactly what he after and i dunno how to make a grid color a cell on a button click and keep it saved after other cells are selected :
original question:
<SRP>
I am working on a project in Delphi and need to use a stringgrid and allow the user to select cells and click a button. The onclick event needs to change the color of selected cells and save the cells coordinates in a file.
<ME>
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, StdCtrls;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1DrawCell(Sender
Rect: TRect; State: TGridDrawState);
procedure StringGrid1SelectCell(Send
var CanSelect: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function WriteToFile(const path1, s1 : string): boolean;
var
f1 : textfile;
begin
WriteToFile:= false;
try
AssignFile(f1, path1);
if FileExists(path1)= false then Rewrite(f1);
Append(f1);
Writeln(f1, s1);
Flush(f1);
CloseFile(f1);
WriteToFile:= true;
except on EinOutError do
WriteToFile:= false;
end;
end;
procedure TForm1.StringGrid1DrawCell
Rect: TRect; State: TGridDrawState);
var
FocusedRect : TRect;
begin
FocusedRect := Rect;
StringGrid1.Canvas.Font.Co
StringGrid1.Canvas.Pen.Col
StringGrid1.Canvas.Pen.Wid
if ( gdFocused in State ) then begin
StringGrid1.Canvas.Font.St
StringGrid1.Canvas.Font.Co
StringGrid1.Canvas.Brush.C
end
else
StringGrid1.Canvas.Font.St
InflateRect(FocusedRect, -2, -2);
StringGrid1.Canvas.FillRec
if ( gdSelected in State ) then
StringGrid1.Canvas.TextOut
StringGrid1.Canvas.TextOut
end;
procedure TForm1.StringGrid1SelectCe
ARow: Integer; var CanSelect: Boolean);
var
s : string;
begin
s := Inttostr(ACol) + Inttostr(ARow);
writetofile('f:\cells.txt'
end;
end.
dunno who wrote the writetofile function but it works nicely
Regards Barry
<SRP>
The solution works very well, but I need to change how it works. I need the button click to save the coordinates and color the cell and not the selectcell event. Also is it possible to color the all of the selcted cells?
<ME>
saving the row/col is easy enough on a button click by saving it in a global var instead then writing the file on the button click..
var
Form1: TForm1;
s : string;
implementation
{$R *.DFM}
procedure TForm1.StringGrid1SelectCe
ARow: Integer; var CanSelect: Boolean);
begin
s := Inttostr(ACol) + Inttostr(ARow);
end;
procedure TForm1.Button1Click(Sender
begin
writetofile('f:\cells.txt'
paint;
end;
end.
<ME>
the difficult part is painting the selected cells and not using the ondrawcells event,ive never seen code that doesnt use the drawcell event, i had a go but couldnt get that part to work right.
<ME>
you could try doing nothing on drawcell event and calling stringgrid.repaint on buttonclick but that will only color the selected cell to default blue..maybe thats what you want im not sure..
<SRP>
I need the button click to change the cell color, so that it remains colored after it is selected and will always be colored.
srp feel free to comment and tell me when it is as you need
cheers ;-)
That sounds like a good way of storing the cells values, but can you explain more on how the the cells relate to the array.
Does the cell default to false and if the boolean array indicates true it changes color? - if so why not say if true cell color = red indstead of storing another array for colors?
If the two arrays were saved I assume it is possible for the grid to check what each cells color should be in the OnFormCreate procedure.
Does the cell default to false and if the boolean array indicates true it changes color? - if so why not say if true cell color = red indstead of storing another array for colors?
If the two arrays were saved I assume it is possible for the grid to check what each cells color should be in the OnFormCreate procedure.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
is this question answered ?
can i give scrappy the points ?
can i give scrappy the points ?
inthe
Hi, sorry forgot that you reasked this question for me (cheers!).
I was waiting to see what else would come up, but it has been a while and i have manged to solve my problem using scrapdogs' answer.
By all means give him the points, it was a very good answer. What I had aimed to do, is now accumlished!
Thanks for asking it again.
Scrapdog - thanks for the answer about arrays. Very effective.
Hi, sorry forgot that you reasked this question for me (cheers!).
I was waiting to see what else would come up, but it has been a while and i have manged to solve my problem using scrapdogs' answer.
By all means give him the points, it was a very good answer. What I had aimed to do, is now accumlished!
Thanks for asking it again.
Scrapdog - thanks for the answer about arrays. Very effective.
ASKER
ok thanks for responding
happy christmas and new year ;-)
happy christmas and new year ;-)
I used a separate array (rows x columns) for the colors of the cell, and another array to store a boolean indicating whether or not the color has changed.
When a user clicks on a cell, update the color in the array for the corresponding cell.
In the OnDrawCell event you could just check the array and if it has changed paint it according to the color in the array.