change cells content on keystroke in TStringGrid

Using Delphi X2
I have a TStringGrid with editable cells
On one cell, when the user type something, it is written in the cell (by the component)
Now I want this tricky thing to happen : when user press keys, I want the value to become something, based on a function that I wrote, something like function MyConvert(S:string):string, where S is what normally is typed in by the user.
The conversion should be done on each key stroke ...
LVL 1
LeTayAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LeTayAuthor Commented:
I partially found the solution by myself
I coded the replacement of the string, in the SetEditText event
But doing this, the data in the field is highlighted : when the user press the first keyboard letter, says A, my function replaces it, says by B but that B is hightlight and now if the user press a SECOND key, the B is replaced
How can I have the "field" not highlighted now ?
Thanks
Sinisa VukSoftware architectCommented:
I don't understand what you trying to do.
try set after MyConvert in SetEditText event:
type
  TStringGridHelper = Class(TStringGrid);

...
TStringGridHelper(Sender).InplaceEditor.SelLength := 0;
TStringGridHelper(Sender).InplaceEditor.SelStart := GetTextLen; 

Open in new window

LeTayAuthor Commented:
I try this : the focus is on a editable cell of TStringGrid SG
Let say it hit the "a" keystroke
I coded this in the SetEditText event :
SG.Cells[ACol,Arow] := MyConvert(Value);
Let say (example) that MyConvert simply uppercase the string
So "A" appears in the cell, but if the user now hit the "b" keystroke, B appears because the entire cell is highlighted
So I need to put the "cursor" at the end of the string in the cell, didn't I ?
How ?
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Sinisa VukSoftware architectCommented:
did you try my code ?
TStringGridHelper(Sender).InplaceEditor.EditText := MyConvert(TStringGridHelper(Sender).InplaceEditor.EditText);
TStringGridHelper(Sender).InplaceEditor.SelLength := 0;
TStringGridHelper(Sender).InplaceEditor.SelStart := GetTextLen; 

Open in new window

LeTayAuthor Commented:
You mean that I just need to put these 3 lines in the SetEditText event ?
LeTayAuthor Commented:
I think I missed something in your code I need to put in mine
Sorry but what exactly should I add ?
Thanks
Sinisa VukSoftware architectCommented:
try something like:
....
type
  TStringGridHelper = Class(TStringGrid);
....
procedure TForm1.MyGridGetEditText(Sender: TObject; ACol, ARow: Integer; var Value: String);
begin
  TStringGridHelper(Sender).InplaceEditor.EditText := MyConvert(TStringGridHelper(Sender).InplaceEditor.EditText);
TStringGridHelper(Sender).InplaceEditor.SelLength := 0;
TStringGridHelper(Sender).InplaceEditor.SelStart := TStringGridHelper(Sender).InplaceEditor.GetTextLen;

end;

Open in new window

LeTayAuthor Commented:
I coded what you suggest but the MyGridEditText is not invoked when I type something in the cell
Is it the GetEditText that need to be coded, I thought it was the SetEditText ?
LeTayAuthor Commented:
In fact, it is a little bit different that my previous comment : MyGridGetEditText gets the control when the "focus" (maybe not the right word) arrives on the cell but when I type something in it, it does not anymore
Sinisa VukSoftware architectCommented:
sorry, should be SetEditText event
this is my project:
function MyConvert(s: String): String;
begin
  Result := UpperCase(s);
end;

procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,
  ARow: Integer; const Value: String);
begin
  TStringGridHelper(Sender).InplaceEditor.EditText := MyConvert(TStringGridHelper(Sender).InplaceEditor.EditText);
  TStringGridHelper(Sender).InplaceEditor.SelLength := 0;
  TStringGridHelper(Sender).InplaceEditor.SelStart := TStringGridHelper(Sender).InplaceEditor.GetTextLen;

end;

procedure TForm1.FormShow(Sender: TObject);
begin
  StringGrid1.RowCount := 5;
  StringGrid1.ColCount := 5;
end;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LeTayAuthor Commented:
It works with SetEditText !
Thanks but ... I do not understand why the declaration :
  TStringGridHelper = Class(TStringGrid);
makes possible the usage of InplaceEditor which "belongs" (?) to another class
Sinisa VukSoftware architectCommented:
Yes, this is a trick to access protected properties and methods outside of main class....
http://delphi.about.com/od/oopindelphi/l/aa082603a.htm
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.