Page 1 of 1

[Editing TTreeNodeShape] 'Return' key to validate a new name

Posted: Fri Feb 17, 2006 11:30 am
by 9343260
Hello,

I think the actual way of beeing able to put a 'Line Return' in the name of a TTreeNodeShape is not very intuitive.

As a I user, I would think that pressing the 'Return' key while editing the name of a TTreeNodeShape will validate the new name, which is not the case.

Is there a way to intercept the 'Return' key to force stopEditing ? I tried to use the 'onKeyPress' event of the TreeView, but this event isn't called when editing a TTreeNodeShape.

Posted: Tue Feb 21, 2006 10:23 am
by Tom
Hi bertrod,

There are cases in which people want to have full text inside a node (including return characters). It just depends on the functionality of the application.

When in 'text' editing mode, the OnKeyPress event of the tree isn't used, since the focus is on the internal memo of the tree.

Off course you can still intercept the 'return' key, see code below

Code: Select all

procedure TForm2.Tree1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    Tree1.StopEditing;
  end;
end;

procedure TForm2.Tree1StartEditing(Shape: TTreeNodeShape;
  var AllowEditing: Boolean);
begin
  if Assigned(Tree1.TextEditor.Memo) then
  begin
    //Tree1.TextEditor.Memo.WantReturns := False;
    Tree1.TextEditor.Memo.OnKeyPress := Tree1KeyPress;
  end;
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  if Assigned(Tree1.TextEditor.Memo) then
   Tree1.TextEditor.Memo.OnKeyPress := nil;
end;
You need to assign the keypress event in the OnStartEditing event, since the memo will not be created at first (only at the first use of it)

You can also disable the use of a return key (see commented code concerning WantReturns property of the internal memo. For more information on this, I refer you to the standard delphi helpfile concerning TMemo)

Best regards,
Tom.

Posted: Wed Feb 22, 2006 9:27 am
by 9343260
Thanks a lot ! :D