Hold InPlaceEditor in case of loss of focus

Is there a way in Delphi XE2 to keep the InPlaceEditor highlighted in a StringGrid when the grid loses focus on another modeless form?

My current StringGrid options:

enter image description here

If not, I was hoping to use the code below to keep highlighting the current cell after losing focus, but I have some problems with this, leaving the cells selected when they are no longer the current cell.

Do I need to add the "else" code to the code below to change the color to the original on unselected cells? Any reservations?

procedure TForm1.sgMultiDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin if (ACol = sgMulti.Col) and (ARow = sgMulti.Row) then begin sgMulti.Canvas.Brush.Color := clYellow; sgMulti.Canvas.FillRect(Rect); sgMulti.Canvas.TextRect(Rect, Rect.Left, Rect.Top, sgMulti.Cells[ACol, ARow]); if gdFocused in State then sgMulti.Canvas.DrawFocusRect(Rect); user end; end; { sgMultiDrawCell} 

Edit: The screenshot below explains how it behaves today. I want the current cell, having lost focus, to be more understandable than the bottom screen capture.

enter image description here

+4
source share
1 answer

If you want to enable the goAlwaysShowEditor parameter and select only the always displayed editor, you need access to the InplaceEditor property. This should subclass the row grid class and change the color of the inplace editor, which by default is the TCustomMaskEdit control class.
This code shows how to change the color of the inplace editor, depending on when the string grid is focused or not:

 type TStringGrid = class(Grids.TStringGrid) private procedure CMEnter(var Message: TCMEnter); message CM_ENTER; procedure CMExit(var Message: TCMExit); message CM_EXIT; protected function CreateEditor: TInplaceEdit; override; end; implementation { TStringGrid } procedure TStringGrid.CMEnter(var Message: TCMEnter); begin inherited; if Assigned(InplaceEditor) then TMaskEdit(InplaceEditor).Color := $0000FFBF; end; procedure TStringGrid.CMExit(var Message: TCMExit); begin inherited; if Assigned(InplaceEditor) then TMaskEdit(InplaceEditor).Color := $0000A6FF; end; function TStringGrid.CreateEditor: TInplaceEdit; begin Result := inherited; if Focused then TMaskEdit(Result).Color := $0000FFBF else TMaskEdit(Result).Color := $0000A6FF; end; 

And the result with a focused and unfocused state of the grid:

enter image description here

+6
source

All Articles