Jump to content
Mike Warren

Access TStringGrid InplaceEditor

Recommended Posts

How do I access the InplaceEditor of a TStringGrid?

 

Specifically, I'll like to respond to KeyDown and KeyUp.

 

The first task I need is to convert every entered character to uppercase, but I expect to need to do more later.

Share this post


Link to post

Thanks for the reply Lars.

Am I mistaken that I can't find OnSetEditText in FireMonkey? It seems to be VCL only.

Share this post


Link to post
5 minutes ago, Mike Warren said:

Thanks for the reply Lars.

Am I mistaken that I can't find OnSetEditText in FireMonkey? It seems to be VCL only.

Argh, Mike - my bad.  I was speaking from a VCL point of view - not noticing that you posted in the FMX section.  Not the first time I've made that mistake.

Share this post


Link to post

@Mike Warren

 

question: doesn't the "inplace editor" have the mission of showing a "custom" control to edit the value of what is shown in the grid? That is, when associating the variable "Control" to a new control, couldn't you simply access the properties of this "control"?

Quote

procedure TForm1.StringGrid1CreateCustomEditor(Sender: TObject; const Column: TColumn; var Control: TStyledControl);

 

function MyComboBoxToGridCustomEditor(AOwner: TComponent { TForm - Form1 } ): TComboBox;
begin
  result := TComboBox.Create(AOwner); // now, you access the properties/events/etc..
  //
  result.Items.AddStrings(MyAllEnumsNames);
  //
  result.OnChange := Form1.MyComboBoxChange;
end;


procedure TForm1.Grid1CreateCustomEditor(Sender: TObject; const Column: TColumn; var Control: TStyledControl);
begin
  // Control is nil (???) = using default controls as defined on grid
  // Sender is TStyledGrid;
  //
  if Column.Index = 1 then // now using my ComboBox 
    Control := MyComboBoxToGridCustomEditor(Self); // destroyed when out scope this procedure...
end;

 

  • Like 1

Share this post


Link to post
Quote

No, it doesn't.

I was about to suggest to create a custom inplace editor. But the form that contains the TStringGrid does get KeyDown notifications, even when editing content. Out of curiosity, I wanted to know the internal structure of the grid, when it's editing.

 

procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char;
  Shift: TShiftState);
var
  ctl: tfmxobject;
  s: string;
begin
  if key = vkF5 then
  begin
    ctl := activecontrol;
    while ctl <> form2 do
    begin
      s := '> ' +ctl.classname + S;
      ctl := ctl.Parent;
    end;
    showmessage(s);
  end;
end;

It says:

 

TStringGrid > TGridScrollContent > TDefaultEditor

 

So if you check the OnKeyDown on form level and detect that ActiveControl is a TDefaultEditor with a TStringGrid as grandparent, you should be all set. Aren't you?

Share this post


Link to post

the "OnKeyDown" event works, you need to be on column desired.

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    // ComboBox1: TComboBox; not necessary because will be create on-the-fly
    StringColumn1: TStringColumn;
    StringColumn2: TStringColumn;
    StringColumn3: TStringColumn;
    Memo1: TMemo;
    procedure StringGrid1CreateCustomEditor(Sender: TObject; const Column: TColumn; var Control: TStyledControl);
  private
    function MyComboBoxToGridCustomEditor(AOwner: TComponent { TForm - Form1 } ): TComboBox; { Private declarations }
    procedure MyComboBoxKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);

...

implementation

{$R *.fmx}

procedure TForm1.MyComboBoxKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
  Caption := Format('%s, Key=%d, KeyChar=%s ', [TimeToStr(now), Key, KeyChar]);
end;

function TForm1.MyComboBoxToGridCustomEditor(AOwner: TComponent { TForm - Form1 } ): TComboBox;
begin
  result := TComboBox.Create(AOwner); // now, you access the properties/events/etc..
  //
  result.Items.AddStrings(['hello', 'world']);
  //
  result.OnKeyDown := MyComboBoxKeyDown;
end;

procedure TForm1.StringGrid1CreateCustomEditor(Sender: TObject; const Column: TColumn; var Control: TStyledControl);
begin
  // Control is nil (???) = using default controls as defined on grid
  // Sender is TStyledGrid;
  //
  if Column.Index = 0 then                         // now using my ComboBox
    Control := MyComboBoxToGridCustomEditor(Self); // destroyed when out scope this procedure...
end;

 

Project1_iKAuHwZEZZ.gif

Edited by programmerdelphi2k

Share this post


Link to post

Thanks for the ideas.

I decided to go with the custom editor because for other cells I need different types of editing.

Share this post


Link to post

Except that in FMX:

[dcc32 Error] uAPMain.pas(1103): E2003 Undeclared identifier: 'ecUpperCase'

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×