color and saving in stringgrids

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.
SRPAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
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(Sender: 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.Color  := clBlack;
  StringGrid1.Canvas.Pen.Color := clBlack;
  StringGrid1.Canvas.Pen.Width := 1;
  if ( gdFocused in State ) then begin
    StringGrid1.Canvas.Font.Style  := [fsBold];
    StringGrid1.Canvas.Font.Color  := clRed;
    StringGrid1.Canvas.Brush.Color  := clblue;
  end
else
    StringGrid1.Canvas.Font.Style  := [];
  InflateRect(FocusedRect, -2, -2);
  StringGrid1.Canvas.FillRect(FocusedRect);
   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.StringGrid1SelectCell(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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SRPAuthor Commented:
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?
0
intheCommented:
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.StringGrid1SelectCell(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.

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.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

intheCommented:
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..
0
SRPAuthor Commented:
I need the button click to change the cell color, so that it remains colored after it is selected and will always be colored.
0
intheCommented:
hi,
i dunno how to do it so ive re-asked the question on your behalf see:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=10240202 

tell me when a suitable answer has been given
cheers.  

0
intheCommented:
ps :
probably best to leave a comment in that question straight away otherwise if someone answers it you may have to pay points to see the answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.