delphifiremonkey-fm2

Line breaks in TMemo on a form with Default button


I have a form in an application written using FireMonkey. On that (modal) form there's an OK button for which I have set Default property to True. There's also a memo component. Now if type press enter while typing in the memo, then the form is closed instead of inserting line break into memo.

What I would like to accomplish, is that when enter (or shift+enter or smth like that) is pressed in memo component, then line break is enter. In other components, where you cannot type line breaks, I would still like hitting enter to close the form. The best I have found thus far is adding following code into forms OnCloseQuery action:

if (Focused.GetObject.ClassName = 'TMemo') and (ModalResult = mrOk) then
begin
  CanClose := False;
  Memo := TMemo(Focused.GetObject);
  Memo.InsertAfter(Memo.CaretPosition, sLineBreak, [TInsertOption.ioMoveCaret,
    TInsertOption.ioCanUndo]);
end
else
  CanClose := True;

This works, but there's now there's a small annoying delay after hitting enter and before the line break appears. Also I would like solution, that would be less hacky.

I should also point out, that I also have forms which contain the OK button, but not the memo component, however a memo will be moved to that form at runtime by changing its parent property.


Solution

  • Set btnOk.Default to False in your memo's OnEnter, and back to True in the memo's OnExit