-1

The component I use is a descendant of TStringGrid, called TDataGrid (which can be found on Torry). Unfortunately, it has a small bug which doesn't seem to be present in the original TStringGrid component. I have the options goEditing and goAlwaysShowEditor both enabled, so I'm expecting the text of a cell to be selected when the control receives input focus. But in the case of TDataGrid this doesn't happen if I press TAB to move from one control into the grid control. It receives input focus, but nothing is selected, and the caret isn't even visible. Obviously, this is very misleading for a user.

The original TStringGrid component has similar inconsistencies, where if you have goAlwaysShowEditor enabled, there is always one cell with its text "exposed" (focused, sort of, even if the grid control itself doesn't have focus), so if you left-click in that cell, it also won't select the text, just enable a caret. I have been able to get around this however, by simply disabling goAlwaysShowEditor when the grid control doesn't have focus and subsequently enabling it when the grid receives focus.

So does a TStringGrid have any way of selecting the text of a cell? I know how to set the focus to a cell, but I haven't figured out a way to actually select the text. Really would appreciate any ideas to get around this!

FWIW, I'm using Delphi 10.3

user966939
  • 692
  • 8
  • 27
  • I can't reproduce your problem (or am unclear what you're expecting). I have a form with a stringgrid which, in tab order, is between 2 TEdits and has your SG options set. When the form first shows, Edit1 is focused and its text is selected. At that point, the text in cell[1,1] is displayed with the default light-blue background. Whether I press [Tab] or click in cell[1,1], focus shifts to that cell and its contents are selected, which is what I'd expect. What do you expect or get that's different? – MartynA Feb 22 '20 at 15:33
  • @MartynA Have you tested TDataGrid? The issue with TABing into the control seems specific to that component. – user966939 Feb 22 '20 at 15:42
  • No. I don't have it or want to install it. But I don't get the behaviour you describe in the 2nd para of your q. – MartynA Feb 22 '20 at 15:43
  • @MartynA I'll try to rephrase. Assuming you have **goEditing** and **goAlwaysShowEditor** enabled, and some text in cell[1,1] (programmatically added), then left-clicking (mouse) cell[1,1] immediately after the grid is shown will not mark the text. Instead, you're just setting a caret inside of the already-exposed inplace editor at whatever coordinate you click. Hope that clears it up. – user966939 Feb 22 '20 at 16:13
  • Sorry, but if I do exactly that, the text of cell[1,1] is immediately selected and a caret is displayed at its RH end. This is in a barebones project whose only code is the code to set the cell's text, and all properties are at their off-palette defaults except the 2 SG editing options. I suggest you try a new projject like that, if your current one has more code or property-settings. – MartynA Feb 22 '20 at 16:22
  • @MartynA Don't know what to say.. I've done that myself already, and the result isn't any different. It sets a caret where I click, but it does not automatically put a selection on the text. What version of Delphi do you use? And OS? I am on Windows 7 here. – user966939 Feb 22 '20 at 16:28
  • I'm using Win10 64-bit and Delphi Seattle (v.10.2.3). Sounds like a heads-up from someone else might help. – MartynA Feb 22 '20 at 16:33

1 Answers1

0

I've found one solution that actually seems to work, but it only handles the very specific scenario when you TAB into the grid control and there may be other cases that need to be handled..

So, controlling the selection of the text within a cell IS possible, but it will not work if you try to control it during the OnEnter event. So instead, I've had to resort to checking for TAB in the OnKeyUp event, where it will work.

You need to expose protected members of a TStringGrid to access the appropriate methods. It's not the prettiest solution, but it works at least...

type TStringGridHelper = class helper for TStringGrid
  procedure HHideEditor;
  procedure HShowEditor;
end;

procedure TDataGridHelper.HHideEditor;
begin
  HideEditor;
end;

procedure TDataGridHelper.HShowEditor;
begin
  ShowEditor;
end;

And in the OnKeyUp event handler...

if Key = VK_TAB then begin
  StringGrid.HHideEditor;
  StringGrid.HShowEditor;
end;
user966939
  • 692
  • 8
  • 27