views:

100

answers:

1

I have an app that opens a non-modal form from the main form. The non-modal form has a TMemo on it. The main form menu uses "space" as one of its accelerator characters.

When the non-modal form is open and the memo has focus, every time I try to enter a space into the memo on the non-modal form, the main form event for the "space" shortcut fires!

I have tried turning MainForm.KeyPreview := false while the other form is open but no dice.

Any ideas?

TIA

+1  A: 

Disable the menu item on the main form while the memo has focus, and re-enable it when the memo loses it. You can do this from the TMemo.OnEnter and TMemo.OnExit events.

procedure TOtherForm.Memo1Enter(Sender: TObject);
begin
  if Application.MainForm is TYourMainForm then
    TYourMainForm(Application.MainForm).MenuItemWithSpace. Enabled := False;
end;

procedure TOtherForm.Memo1Exit(Sender: TObject);
begin
  if Application.MainForm is TYourMainForm then
    TYourMainForm(Application.MainForm).MenuItemWithSpace. Enabled := True;
end;

The use of Application.MainForm and the typecast are to prevent hard-coding in a form variable nae in the child form.

Ken White
I don't like that your asking TOtherForm to control TYourMainForm. It feels like a tight coupling. The problem is down in the key events somewhere.
Preston
I agree, its a bit inelegant, but I just put: MyMainForm.MenuEntry1.Enabled := false ;in the OnShow event, and MainForm.MenuEntry1.Enabled := true ;in the OnClose event. Seems to work a treat.
@Preston: Neither do I. The problem, though, is that the OP wants to use a non-modal form and disable a shortcut only when a *specific control* on that non-modal form is active. The non-modal form is the only place to do it, unless you disable the shortcut for the entire period the child form is in existence (which the OP said wasn't desired).
Ken White