Line breaks in TMemo on a form with Default button
Asked Answered
U

1

8

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.

Unhandled answered 16/3, 2013 at 10:33 Comment(8)
Try Memo.Lines.BeginUpdate; before changing Memo content and Memo.Lines.EndUpdate; after that.Presley
Emba missed to implement TMemo WantReturns, WantTabs properties :o( I would use an OnIdle Event on the form to check if there is a focused TMemo to set the Default property. But Emba also have no TApplicationEvent for FMX :o( Only TActionList has OnUpdate Event which fired on idle if at least one action is defined and assigned to a controlMalt
In your default button OnClick event, test if the memo is focused and set ModalResult to mrNone if that is the case.Damar
@LURD The problem is caused by TButton.Default := True so pressing RETURN has no effect on TMemo. If TButton.ModalResult is set, there is no need for an OnClick eventMalt
@SirRufo, you are correct, my mistake.Damar
@Presley BeginUpdate/EndUpdate was a good idea I failed to try, but unfortunately delay comes from somewhere else (code in OK buttons event handler).Subsidiary
@LURD I tried your advice and I realized that delay actually was caused by the code in OnClick event handler and by checking the focused item type I can now skip the code that caused the delay if memo is focused. So basically the delay issue is resolved. Thanks!Subsidiary
That has to be an FMX bug. When the memo has focus, it should consume the ENTER key press.Mezereum
C
2

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

Catt answered 17/3, 2013 at 17:12 Comment(2)
It's not always that easy. I have forms where memo is on another form that is embedded into form with the OK button (like in https://mcmap.net/q/160839/-how-embed-a-firemonkey-form-inside-a-control). Therefore sometimes memo has no way of knowing whether there's a button.Subsidiary
In that case, you could create a descendant of TMemo which searches through its parent's controls for a button with Default set to True, and set it to False in the CMEnter() method, and back to True in CMExit()Catt

© 2022 - 2024 — McMap. All rights reserved.