Lazarus

Programming => General => Topic started by: pcurtis on February 28, 2021, 05:55:59 pm

Title: [SOLVED] Fitting control to StringGrid cell
Post by: pcurtis on February 28, 2021, 05:55:59 pm
Hi All,

why doesn't the control (TButton) fit the cell in a StringGrid when I do this

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer; aRect: TRect;
  2.   aState: TGridDrawState);
  3. begin
  4.   if (aCol = 1) and (aRow = 2) then
  5.     Button2.SetBounds(aRect.Left, aRect.Top, aRect.Width, aRect.Height);
  6.  
  7.   (Sender as TStringGrid).DefaultDrawCell(aCol, aRow, aRect, aState);
  8. end;
  9.  
Its close but about 10 pixels off
Title: Re: Fitting control to StringGrid cell
Post by: Handoko on February 28, 2021, 06:26:09 pm
It should be something like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
  2.   aRect: TRect; aState: TGridDrawState);
  3. var
  4.   TheGrid: TStringGrid;
  5. begin
  6.   if not(Sender is TStringGrid) then Exit;
  7.   TheGrid := (Sender as TStringGrid);
  8.   if (aCol = 1) and (aRow = 2) then
  9.     Button1.SetBounds(aRect.Left + TheGrid.Left, aRect.Top + TheGrid.Top,
  10.       aRect.Width, aRect.Height);
  11. end;
Title: Re: Fitting control to StringGrid cell
Post by: jamie on February 28, 2021, 06:26:34 pm
hard to say...

could be borderspacing values set in the control?

the control may have the wrong parent..?

The control could be scrolled alittle ?
Title: Re: Fitting control to StringGrid cell
Post by: Handoko on February 28, 2021, 06:30:57 pm
Not hard.  :D

It's about the coordinate origin. The button's coordinate origin is the form's top left corner but the aRect in the event is using the grid's top left corner as its origin.
Title: Re: Fitting control to StringGrid cell
Post by: pcurtis on February 28, 2021, 06:43:28 pm
Thanks
TinyPortal © 2005-2018