Recent

Author Topic: TStringGrid Improved cbsEllipsis Button Behaviour  (Read 6391 times)

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
TStringGrid Improved cbsEllipsis Button Behaviour
« on: March 17, 2013, 08:32:01 pm »
I have noticed that using an ellipsis column and a button column have different behaviours when calling a ColorDialog to change the colour of the cell the ellipsis/button is in.

The cbsButton can call the DrawCell event and change the colour immediately the ColorDialog closes.

The cbsEllipsis can only call the DrawCell event when focus moves to another cell.

Is there an easy way to get an immediate result with cbsEllipsis without changing cell?

The Grids help describes cbsEllipsis as a more general purpose cell than cbsButton, but the opposite seems to be true.

I looked in Grids.pas and cbsEllipsis calls TButtonCellEditor, while cbsButton calls both TButtonCellEditor and TStringEditor. I assume this accounts for the different behaviour.

The following code can be dropped on a form with a StringGrid and used as the basis for testing.

var
 Mycolor: TColor;

procedure TForm1.StringGrid1ButtonClick(Sender: TObject; aCol, aRow: integer);
begin
If (aCol = 0) and ColorDialog1.Execute then //Press cbsButton or cbsEllipsis
  begin
    Mycolor := Colordialog1.Color;
    StringGrid1.Invalidate; //Could also use 'Repaint' te force DrawCell event
  end;
end;
 
procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: integer;
  aRect: TRect; aState: TGridDrawState);
begin
  If (aRow > 0) and (ACol = 0) then        //Use DrawCell to paint rectangle
  begin
    stringgrid1.canvas.Brush.Color:= MyColor;
    stringgrid1.canvas.FillRect(aRect);
  end;
end;

Laz 1.0.6 FPC 2.6 Windows 8 I7 processor

jesusr

  • Sr. Member
  • ****
  • Posts: 499
Re: TStringGrid Improved cbsEllipsis Button Behaviour
« Reply #1 on: March 18, 2013, 10:03:03 pm »
I have noticed that using an ellipsis column and a button column have different behaviours when calling a ColorDialog to change the colour of the cell the ellipsis/button is in.

The cbsButton can call the DrawCell event and change the colour immediately the ColorDialog closes.

The cbsEllipsis can only call the DrawCell event when focus moves to another cell.

Is there an easy way to get an immediate result with cbsEllipsis without changing cell?

As far as I understood the problem:

It works fine, what you may not have noticed is that cbsEllipsis it's a compound editor that consists in a StringEditor and a ButtonEditor, as the StringEditor occupy most part of the cell and the button the rest the whole cell is covered. As a test set the grid.color to say clAqua and you will see the StringEditor surface.

On the why in your code the column do no change color, it may be because the condition in ButtonClick specify  if (aCol = 0) and ColorDialog1.Execute then, as this is "by column" editor, either is at column 0 or not, if you change the condition to  if ColorDialog1.Execute  then, it will work (but notice the cell that have the editor, the cbsEllipsis column) will be covered by the editor, so if you move it to another column it will be painted with the background color.


Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: TStringGrid Improved cbsEllipsis Button Behaviour
« Reply #2 on: March 19, 2013, 09:39:29 am »
Thanks JesusR,

I did discover that it was a compound editor, and that the cell colour changed when leaving the cell. I had not understood that the editor was covering the cell until the exit. (Even though I had read old posts showing how to place the editor rect over the cell rect, and written test code to do it!)

 

TinyPortal © 2005-2018