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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
intheConnect With a Mentor Commented:
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
 
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
All Courses

From novice to tech pro — start learning today.