Hide selection in TTMSFMXGrid

Hello,

My customer requests me to hide the selection (do not show any cells highlighted in blueish color) when the grid has no input focus. And to paint the selection again when the grid gains input focus (i.e. do not forget it while it is hidden).

I have not found any way to do this.
I even cannot see any way to set the selection to "none" (to implement myself hiding the selection in OnExit and showing it in OnEnter).
I expected a simple property like the HideSelection of TEdit in old Delphi 7...

Is there a way to implement this?

Thanks,

Peter

Hi, 


You can try with the following code that applies the default normal and fixed layout for selected cells if the grid isn't focused:

procedure TForm72.TMSFMXGrid1Enter(Sender: TObject);
begin
  TMSFMXGrid1.UpdateGridCells;
end;

procedure TForm72.TMSFMXGrid1Exit(Sender: TObject);
begin
  TMSFMXGrid1.UpdateGridCells;
end;

procedure TForm72.TMSFMXGrid1GetCellLayout(Sender: TObject; ACol, ARow: Integer;
  ALayout: TTMSFMXGridCellLayout; ACellState: TCellState);
begin
  if not TMSFMXGrid1.IsFocused then
  begin
    if (ACellState = csSelected) or (ACellState = csFocused) then
      ALayout.Assign(TMSFMXGrid1.GetDefaultNormalLayout.Layout)
    else if (ACellState = csFixedSelected) then
      ALayout.Assign(TMSFMXGrid1.GetDefaultFixedLayout.Layout)
  end;
end;

Kind Regards, 
Pieter

Hello,

Thanks for the suggestion.
It has shown me the correct direction to go.

I still had two sub-problems:

1. assigning the whole layout has cancelled some other changed that I made using different means (e.g. centering some cells and right-aligning other cells). Of course, I could rearrange my code so that all the layout-related settings would happen in the OnGetCellLayout event.
But I decided not to change my code too much, so I just set those properties of the layout that are important to cancel the "selection look":

procedure TForm1.grGetCellLayout(Sender: TObject; ACol, ARow: Integer;
  ALayout: TTMSFMXGridCellLayout; ACellState: TCellState);
var la:TTMSFMXGridCellLayout;
begin
 if not TTMSFMXGrid(Sender).IsFocused then
  begin
    if ACellState in [csSelected,csFocused,csFixedSelected] then
      begin
        if ACellState = csFixedSelected then
          la:=TTMSFMXGrid(Sender).GetDefaultFixedLayout.Layout
        else
          la:=TTMSFMXGrid(Sender).GetDefaultNormalLayout.Layout;
        ALayout.Fill.assign(la.Fill);
        ALayout.Stroke.Assign(la.Stroke);
        ALayout.FontFill.assign(la.FontFill);
      end;
  end;
end;

So this sub-problem is solved for me.

2. Entering the grid by mouse click on a different cell than it is currently selected (but not visualized as selected because of our code) is not visualized nicely.
The problem is that the mouse down event triggers the focus and the grid is repainted (even if I delete the onEnter event, repainting on entering is apparently programmed somewhere "deeper" in the component) with the old selected cell shown. Only later the mouse up event makes the clicked cell selected and repaints the grid once more.
If you do it slowly (mouse down then wait for a second then mouse up), you can see clearly what happens. If you do it quickly (which is the usual way to do clicks) it is clearly visible that the grid is repainted twice and it is pretty ugly.
I have some ideas what to do, but all of them are pretty tricky (e.g. intercept mouse down and mouse up and then change the test to "not TTMSFMXGrid(Sender).IsFocused or FocusingClickIsStillDown", or maybe change the grid so that the cell selection is made on mouse down not on mouse up, or...)
Any better idea? Or has anybody solved a similar problem?

Thanks,

Peter


Hi, 


We have investigated this further and we also have a property that can control the type of selection. When setting it to TMSFMXGrid1.SelectionMode := smNone, the selection fill isn't applied as well. Perhaps you can toggle the selectionmode in the OnEnter / OnExit. Internally the component uses SetFocus to set the focus to itself when clicking on it.

procedure TForm73.TMSFMXGrid1Enter(Sender: TObject);
begin
  TMSFMXGrid1.SelectionMode := smSingleCell;
end;

procedure TForm73.TMSFMXGrid1Exit(Sender: TObject);
begin
  TMSFMXGrid1.SelectionMode := smNone;
end;

We will investigate sub-problem 2 with the original code but the code above might be a solution for both problems.

Kind Regards,
Pieter