tags:

views:

222

answers:

4

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

The user is able to edit the captions of the tree nodes using the controls built in functionality.

If the user hits escape or enter while editing a tree node the dialog will disapper instead of just canceling or accepting the edit to the node caption.

In the case of escape, for example, I would expect to hit escape once to canel the edit of the caption and then hit escape a second time to cancel the dialog.

What is the best way to deal with this situation?

TMemo has the WantReturns property to deal with this but I can't see anything for TTreeView.

+2  A: 

you should remove Default and Cancel properties from the buttons, instead you should case the key pressed on form keyDown and then perform 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.

Wael Dalloul
I went with this one but I had to put a check for 'if not TreeView.IsEditing' around your if...else statement. Can you update your anwser to include this and I can then accept it. Also, you can just set ModalResult instead of calling the CancelClick and OnClick if your buttons have no logic behind them
Jamie
@Jamie: Isn't it more robust to call CancelClick and OkClick as Wael Dalloul has done, rather than assign ModalResult directly? What if you want to modify CancelClick later - will you realize/remember that you're assigning ModalResult elsewhere?
Argalatyr
Yeah but if you don't have any event handlers then you have nothing to call? maybe calling TButton.Click would be better?
Jamie
+2  A: 

Don't set only the ModalResult property on OK and Cancel buttons, but create an OnClick event handler and use

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

or mrCancel respectively

Stijn Sanders
This seems to suffer from the same issue as Mason Wheeler's answer. The dialog wont close but the edit also won't commit or rollback
Jamie
+1  A: 

Maybe it helps to temporarily set Default and Cancel to False in TTreeView.OnEditing and back to True in TTreeView.OnEdited. There's no OnCancelEdit - this might be a problem.

Ulrich Gerhardt
+1  A: 

What I'd do in this situation is add an OnCloseQuery event handler to the form that will keep it from closing if the TTreeView is the focused control.

Mason Wheeler
wouldn't TreeView1.IsEditing bee a better check?
Stijn Sanders
This helps in preventing the form from closing, but "cancel" handling still eats the ESC, so now the keypress does nothing (the user expects their tree node editing to be canceled).
Paul-Jan
Paul-Jan: good point. Maybe then you'd need to augment it with something like what Wael suggested?
Mason Wheeler