Re: array of labels or array of buttons

From: Jim P (Jim_P_at_mad.scientist.com)
Date: 11/10/04


Date: Wed, 10 Nov 2004 13:01:37 -0800

Alan

  a lot of information here. I will look it over and ponder it. This
should help.

Thanks Jim P.

AlanGLLoyd wrote:

> In article <062dnT0OTOBS2wzcRVn-pg@comcast.com>, Jim P
> <Jim_P@mad.scientist.com> writes:
>
>
>>I am and old Pascal programmer and been forced to do VB coding and
>>finally got a project that I can do in Delphi. Forced it upon my customer.
>>
>>But ran into some aspects of Delphi that I know has to be present but
>>can not seem to find them.
>>
>>The main one is to have on a form an array of controls. Such as buttons.
>> or Text fields (Tlabels)
>>
>>In VB the compiler keeps trying to make all controls an array - -each
>>time you cut and past one it asks if you want to make this an array.
>>
>>So how do I do it in Delphi?
>>
>
>
> Use a TStringGrid, and if you really want a buttons display (and not just
> clicking o the cell in a string grid) use Delphi's DrawButtonFace function to
> draw the buttons in an OnDrawCell event handler of the stringgrid like ...
>
> uses
> Buttons; // Buttons.pas contains the DrawButtonFace function. it is most
> likely not in Delphi help
>
> var
> MouseCol, MouseRow : integer;
>
> procedure TForm1.FormCreate(Sender: TObject);
> begin
> with StringGrid1 do begin
> {strings for top row}
> Rows[0].CommaText := 'Sun,Mon,Tue,Wed,Thu,Fri,Sat';
> {set these in Object Inspector if desired}
> Width := 361;
> Height := 130;
> FixedCols := 0;
> FixedRows := 0;
> ColCount := 7;
> RowCount := 6;
> DefaultColWidth := 50;
> DefaultRowHeight := 20;
> ScrollBars := ssNone;
> end;
> FillStringGrid(11, 2004);
> end;
>
> procedure TForm1.FillStringGrid(Mn, Yr : integer);
> var
> FirstDay, i, j : integer;
> FirstDate : TDateTime;
> begin
> FirstDate := EncodeDate(Yr, Mn, 1);
> FirstDay := DayOfWeek(FirstDate);
> for i := FirstDay - 1 to FirstDay - 1 + MonthDays[IsLeapYear(Yr), Mn] do
> StringGrid1.Cells[(i mod 7), (i div 7) + 1] := IntToStr(i - FirstDay + 2);
> end;
>
> procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
> ARect: TRect; State: TGridDrawState);
> {draws each cell as called by paint}
> var
> CellDown : boolean;
> begin
> if (ARow > 0) and (StringGrid1.Cells[ACol, ARow] > '') then begin
> CellDown := (ACol = MouseCol) and (ARow = MouseRow);
> DrawButtonFace(StringGrid1.Canvas, ARect, 2, bsNew, CellDown, false,
> false);
> end;
> if (ARow = 0) then
> StringGrid1.Canvas.FillRect(ARect); // clears background, DBF fills ARect
> for other cells
> DrawText(StringGrid1.Canvas.Handle, PChar(StringGrid1.Cells[ACol, ARow]),
> -1, ARect, DT_CENTER or DT_SINGLELINE or DT_VCENTER); // Win API
> call
> end;
>
> procedure TForm1.StringGrid1MouseDown(Sender: TObject;
> Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
> begin
> {store mouse cell for OnClick usage}
> StringGrid1.MouseToCell(X, Y, MouseCol, MouseRow);
> end;
>
> procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
> Shift: TShiftState; X, Y: Integer);
> begin
> {set selection off display}
> StringGrid1.Selection := TGridRect(Rect(-1, -1, -1, -1));
> end;
>
> procedure TForm1.StringGrid1Click(Sender: TObject);
> begin
> {do what you want when clicked}
> ShowMessage('Clicked ' + StringGrid1.Cells[MouseCol, 0] + ', ' +
> StringGrid1.Cells[MouseCol,MouseRow]);
> end;
>
>
>>In one case, I want to use the index value of the button array to change
>> font color to reflect a change in status. - -
>>
>
>
> In this case I would use the Objects array of the string grid to store a value
> representing an appropriate colour, and access that value and change the font
> colour in the OnDrawCell event. Because the Objects array holds TObjects you
> have to typecast the value when storing and when accessing it (Delphi has
> strong typing).
>
> StringGrid1.Objects[MouseCol, MouseRow] := TObject(clRed); // set it
>
> ... and in OnDrawCell
>
> StringGrid1.Canvas.Font.Color := TColor(StringGrid1.Objects[ACol, ARect]);
>
> A TStringGrid with OnDrawCell code is a really useful device, it's worth
> getting to know it.
>
> Alan Lloyd
> alanglloyd@aol.com


Quantcast