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(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

<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.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.

<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 ;-)
LVL 17
intheAsked:
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.

scrapdogCommented:
I've done something similar to this before.

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.

0
SRPCommented:
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.
0
scrapdogCommented:
Well, the second array (of booleans) is just there so you can keep the default color of the string grid cells if you want (i.e. the color that appears on a normal string grid).

If you weren't interested in keeping the default color, you could scrap the array of booleans and simply load the entire color array with clWhite (or whatever).

Then, when the user clicks on, say a cell in row 2 column 5, you could do this:

Colors[2,5] := clRed;

{then use whatever code you are using to refresh, whether it is Invalidate, Refresh, etc.}


What *I* personally used the array of booleans for was to check if a cell has changed.  So in the OnDrawCell, I would check in the array if the corresponding cell has changed.  If not, do nothing.  If it has, then repaint the cell using the color in the color array.  You probably don't need to do it this way, if you have your own default color (such as the clWhite example above).

>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

Yep...just load the contents of the file into the array.  The first time the String Grid paints itself, it will check the values in the array.

If you don't understand how the OnDrawCell would work, the OnDrawCell passes the coordinates of the current cell it is painting.  You would use these coordinates as an index into the color array, and then paint the cell the color stored at that location (doing that would be similar to inthe's code)
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
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.

intheAuthor Commented:
is this question answered ?
can i give scrappy the points ?
0
SRPCommented:
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.
0
intheAuthor Commented:
ok thanks for responding
happy christmas and new year ;-)
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.