How to stop the default dialog and cancel the behavior when editing a TTreeView node

I have a dialog with a TTreeView control and an OK and Cancel button. The buttons have the Default and Canel properties set to true, and ModalResult has been set correctly.

The user can edit the headers of the tree nodes using the built-in controls.

If the user hit or clicked while editing the node tree , the dialog will disappear instead of just canceling or accepting the editing under the node heading.

In the case of an escape function, for example, I expect that one day I will be able to escape into the escape code to edit the header, and then delete it a second time to cancel the dialog.

What is the best way to handle this situation?

TMemo has the WantReturns property to handle this, but I don't see anything for TTreeView.

+5
source share
5 answers

you must remove the โ€œDefaultโ€ and โ€œCancelโ€ properties using the buttons, instead you must enter the key pressed in the keyDown form, and then execute OK or cancel.

Edit:

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if (Key = VK_ESCAPE)and not (TreeView1.IsEditing) then
  CancelClick(sender)
else
  if (Key = VK_RETURN) and not (TreeView1.IsEditing) then
    OkClick(sender);
end;

also you need to set keypreview to true.

+3
source

Do not set only the ModalResult property on the OK and Cancel buttons, but create an OnClick event handler and use

if not(TreeView1.IsEditing) then ModalResult:=mrOk

or mrCancel respectively

+2
source

, OnCloseQuery , , TTreeView .

+2

, Default Cancel to False TTreeView.OnEditing True TTreeView.OnEdited. OnCancelEdit - .

+1

, . : buttn click Escape press TButton.CMDialogKey, TCustomForm.CMDialogKey TWinControl.CMDialogKey, WndProc . , , , :

// Ignore ESCAPE when TV is in edit mode
procedure TForm1.CMDialogKey(var Msg: TWMKey);
begin
  if (Msg.CharCode = VK_ESCAPE) and (KeyDataToShiftState(Msg.KeyData) = []) and
     (ActiveControl = tvTree) and tvTree.IsEditing
    then // do nothing
    else inherited; // continue as usual
end;

- , , ModalResult Cancel .

0

All Articles