Delphi 7: StringGrid DrawCell event coloring all the cells black on startup

My program is supposed to read the data from a textfile which is used to set the values of the colors of the differnet cells on startup. However this is not the case as the cells are colored black on startup, instead of the desired color. The following are the values I used in my textfile('Columns.txt')(*Note:16777215/ = white,32768=green):

Karel Bezuidenhoudt/16777215/32768/16777215/16777215/ Jan Pieterse/16777215/16777215/16777215/16777215/ Frik Bezuidenhoudt/16777215/16777215/16777215/16777215/ Megan Smit/16777215/16777215/16777215/16777215/

Here is a screenshot of my main form

This is the output I am currently getting

Instead all the columns should be white, except for the cell in the second column of the first row, which should be green as indicated by the textfile

The current code is as follows(I apologize for all the other random code and variables):

 private
iHideAddPatient,iHideAddCol,iColumnCount,iGenerates,iChangeColor,MouseX,LeftPos,iColValue,iColorValue,iAddField,iShowHide,iPos,iLoopCount,iPatients,iRowOrder,iRowCount:Integer;
**sLine,sTextValues:string;**
**tColumns,tNames:TextFile;**
PnlTest0:TPanel;
PnlField,PnlFieldlbl,pnlPatient,PnlName:TPanel;
PnlColor,PnlColor2,PnlColor3,PnlColor4,PnlColor5,PnlColor6,PnlColor7:TPanel;
btnColor1:TButton;
arrNotes:array[1..400] of string;
arrFieldName:array[1..400] of string;
arrColumnName:array[1..400] of string;
arrColor:array[1..4] of Integer;
**arrNames:array[1..400] of string;**
**arrColCount:array[1..400] of Integer;**
arr2DColor:array[1..100,0..100] of Integer;

procedure GenerateField;
procedure DeleteDone;
procedure SortA;
procedure ChangeColor(k,l:Integer;paneltype:pnlType);
procedure ChangeColor1;
function CopyNext:string;
function FindPos:Integer;
function RemoveSpaces(sName:string):string;  



procedure TForm1.FormActivate(Sender: TObject);
var
  k,iRow,iCol:Integer;
  TheRect:TRect;
begin
pnlColomb.BringToFront;
pnlAddPatient.BringToFront;
pnlColomb.Visible:=False;
pnlAddPatient.Visible:=False;
StringGrid.ColWidths[0]:=200;
sTextValues:='/None/Not received/Received/Analysed/0/';
redtTest.lines.Clear;
iHideAddPatient:=0;
iHideAddCol:=0;
iLoopCount:=1;
iPatients:=0;
iRowCount:=0;
iColumnCount:=0;
iShowHide:=0;
AssignFile(tColumns,'Columns.txt');
 //Append(tColumns);
 try
  Reset(tColumns);
  except
  Showmessage('Textfile missing') ;
  Exit;
  end;
while not Eof(tColumns) do
begin
  Inc(iColumnCount);
  Readln(tColumns,sLine);
  if sLine=''
  then
  begin
  CloseFile(tColumns)
  end
  else
  begin
  arrColumnName[iColumnCount]:=CopyNext;
  DeleteDone;
  end;
end;
CloseFile(tColumns);

AssignFile(tNames,'Patients.txt');

try
  Reset(tNames);
  except
    ShowMessage('Patients.txt doesn''t exist');
  Exit;
  end;

  while not Eof(tNames) do
begin
  Inc(iPatients);
  Readln(tNames,sLine);

  arrNames[iPatients]:=CopyNext;
  DeleteDone;

  for k:=1 to iColumnCount do
  begin
    arr2DColor[iPatients,k]:=StrToInt(CopyNext);
    //ShowMessage(IntToStr(iPatients)+':'+IntToStr(k)+' = '+inttostr(arr2DColor[iPatients,k]));
    DeleteDone;
  end;
  redtTest.Lines.Add('Name:'+IntToStr(iPatients)+' '+arrNames[iPatients]);

  end;


for iRow := 1 to iPatients do
  begin
    for iCol := 1 to iColumnCount do
    begin
      StringGrid.Canvas.Brush.Color:=arr2DColor[iRow,iCol];
      TheRect:=StringGrid.CellRect(iCol,iRow);
      StringGrid.Canvas.FillRect(TheRect);
      StringGrid.Cells[0,iRow]:=arrNames[iRow];
      StringGrid.Cells[iCol,0]:=arrColumnName[iCol];
end;

end;

end;
procedure TForm1.btnAddClick(Sender: TObject);
begin
   if iShowHide=0 then
   begin
    pnlColomb.Visible:=True;
      iShowHide:=1;
   end
  else
   begin
    pnlColomb.Visible:=False;
    iShowHide:=0;
   end;
end;
procedure TForm1.DeleteDone;
begin
   Delete(sLine,1,FindPos);
end;

function TForm1.FindPos: Integer;
begin
   Result:=Pos('/',sLine);
end;


function TForm1.CopyNext: string;
begin
  Result:=Copy(sLine,1,FindPos-1);
end;

The code I use to change the colors of the cells when I click on them are as follows(also I believe I copied all the necessary code, but let me know if something seems missing):

procedure TForm1.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
const
  clOrange = TColor($008CFF);
  CellColors: array[0..3] of TColor = (16777215, clRed, clOrange, clGreen);
begin
  if (ACol in [1..iColumnCount]) and (ARow in [1..iPatients]) then
  begin
    StringGrid.Canvas.Brush.Color := CellColors[arr2Dcolor[ARow, ACol]];
    StringGrid.Canvas.FillRect(Rect);
  end;
end;

type
  TStringGridAccess = class(TStringGrid)
  end;


procedure TForm1.StringGridSelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
begin
  if (ACol in [1..iColumnCount]) and (ARow in [1..iPatients]) then
  begin
    arr2DColor[ARow, ACol] := (arr2dColor[ARow, ACol] + 1) mod 4;
    TStringGridAccess(StringGrid).InvalidateCell(ACol, ARow);
  end;
 if (ACol=0) and (ARow>0) then
  begin
    ShowMessage(arrNotes[ARow]);
    TStringGridAccess(StringGrid).InvalidateCell(ACol, ARow);
  end;
  end;

delphi,delphi-7,

0

Ответов: 1


4

Okay I figured it out. Thanks to everyone that pointed out the logic errors! Yes My Columns had to start at iCols=0 till iColumnCount-1 same for iRows. Also at the place where I set the 2D color I had to change it to the following:

arr2DColor[iPatients,k+1]:=StrToInt(CopyNext);

And where I set the text for iCol=0 and iRow=0 I changed it as follows:

StringGrid.Cells[0,iRow+1]:=arrNames[iRow];
StringGrid.Cells[iCol+1,0]:=arrColumnName[iCol];

Also arrColors, should've had a value of 1,2 or 3 (instead of the int value of the color) as a parameter wherefrom arrColors selects the color from that parameter, so I had to change the textfile as follows:

Karel Bezuidenhoudt/1/2/0/0/ Jan Pieterse/0/0/0/0/ Frik Bezuidenhoudt/0/0/0/0/ Megan Smit/0/1/2/3/

And the code for OnDrawCell as follows

procedure TForm1.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
const
  clOrange = TColor($008CFF);
  CellColors: array[0..3] of TColor = (clWhite, clRed, clOrange, clGreen);
begin
  if (ACol in [1..iColumnCount]) and (ARow in [1..iPatients]) then
  begin
    StringGrid.Canvas.Brush.Color := CellColors[arr2Dcolor[ARow, ACol]];
    StringGrid.Canvas.FillRect(Rect);
  end;
end;

Again thank you so much for the feedback!

delphi,delphi-7,
Похожие вопросы