Recent

Author Topic: TForm.Menu question  (Read 4490 times)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
TForm.Menu question
« on: February 27, 2015, 05:37:49 pm »
I have an application with a hidden form, MainForm, and another, DataForm, that displays data. Then I do this in the running application:

Select 'File Open' this calls MainForm.FileOpen from MainForm.Menu which calls DataForm.ShowOnTop and data is displayed.

All good, but now I expect DataForm.Menu to be active, but MainForm.Menu is still active. :/

To get DataForm.Menu active, I have to click on another window, and back on DataForm. Then DataForm.Menu works properly.

Does anyone have an idea what might be going on? Any information, or possible solutions would be greatly appreciated.

Cheers,
VTwin
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: TForm.Menu question
« Reply #1 on: February 27, 2015, 05:55:05 pm »
Quote
All good, but now I expect DataForm.Menu to be active, but MainForm.Menu is still active. :/
What does this mean? DataForm has not focus?
If so, you can try:
Code: [Select]
DataForm.SetFocus;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: TForm.Menu question
« Reply #2 on: February 27, 2015, 06:06:47 pm »
Blazzen,

Thanks , it seems that is the case somehow, but I can edit the data grid and it otherwise is active. Just DataForm.Menu is not active, somehow events are still going to MainForm.Menu

I tried both DataForm.SetFocus and DataForm.Activate, but nether do anything. :/

VTwin
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TForm.Menu question
« Reply #3 on: February 27, 2015, 06:45:24 pm »
A two-form, two-menu project behaves here as you would expect.
Can you post a simplified project that demonstrates the problem you have?

What do you mean by writing "DataForm.Menu is not active, somehow events are still going to MainForm.Menu" ?

...because events are distributed by the OS to all windows in a project - that is how event-driven GUI apps work.
Which event(s) seem not to be reaching DataForm.Menu, and how are you expecting the menu would respond if it received them?

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: TForm.Menu question
« Reply #4 on: February 27, 2015, 08:01:14 pm »
howardpc,

Thank you, I was thinking incorrectly about it.

The MainForm has menu handlers, as FileOpen. The DataForm has different menu handlers, as DataStatistics. So, as you say, each Form should get the message intended for it.

So the problem is that after I open a file using MainForm.FileOpen, then DataForm.ShowOn top, the DataForm.DataStatistics menu item is enabled in the menubar, but does nothing.

DataForm is not getting DataForm.Menu messages. If I click outside DataForm and back on it, it starts getting DataForm.Menu messages and works as expected. 

In other words, I can select 'Data Statistics' from the DataForm.Menu, but the handler DataForm.DoStatistics is never called. Click off and back on DataForm fixes this.

It seems like a bug. I can try to reproduce it in a simple case.

If it is a bug, maybe someone can suggest a workaround?

VTwin





« Last Edit: February 27, 2015, 08:10:15 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: TForm.Menu question
« Reply #5 on: February 27, 2015, 08:50:41 pm »
I tried to reproduce it in a simple project, but it seems to work. I probably have an error elsewhere. I'd greatly appreciate any comments on this code though.

Code: [Select]

{ Unit 1, uses Unit 2 ---------------- }

procedure TForm1.FormCreate(Sender: TObject);
var
  mnu, itm: TMenuItem;
begin
  Menu := TMainMenu.Create(Self);
  mnu := TMenuItem.Create(Menu);
  mnu.Caption := 'File';
  Menu.Items.Add(mnu);
  itm := TMenuItem.Create(mnu);
  itm.Caption := 'Open...';
  mnu.Add(itm);
  itm.OnClick := @DoFileOpen;
end;

procedure TForm1.DoFileOpen(Sender: TObject);
begin
  ShowMessage('Open File');
  Form2.ShowOnTop;
end;

{ Form2 ------------------------------------ }     

procedure TForm2.FormCreate(Sender: TObject);
var
  mnu, itm: TMenuItem;
begin
  Menu := TMainMenu.Create(Self);
  mnu := TMenuItem.Create(Menu);
  mnu.Caption := 'Data';
  Menu.Items.Add(mnu);
  itm := TMenuItem.Create(mnu);
  itm.Caption := 'Statistics...';
  mnu.Add(itm);
  itm.OnClick := @DoDataStatistics;
end;

procedure TForm2.DoDataStatistics(Sender: TObject);
begin
  ShowMessage('Data Statistics');
end;               


It seems to work fine, but I wouldn't mind a more expert opinion.

Cheers,
VTwin

“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TForm.Menu question
« Reply #6 on: February 27, 2015, 10:07:25 pm »
Straightforward code, like the above, which behaves as expected is not very interesting...
Perhaps your original misbehaving project is too big to publish easily?

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: TForm.Menu question
« Reply #7 on: February 27, 2015, 10:16:52 pm »
howardpc,

Thanks again. I isolated the problem, I was posting a message using PostMessage so other forms would know about the open file. This is where the problem lies.

In my test example, if I post a message from Form1 to Form2, Form2.Menu is displayed and activated, even though Form2 is not even visible.

Code: [Select]
{ TForm1 ------------------------ }

procedure TForm1.FormCreate(Sender: TObject);
var
  mnu, itm: TMenuItem;
begin
  Menu := TMainMenu.Create(Self);
  mnu := TMenuItem.Create(Menu);
  mnu.Caption := 'File';
  Menu.Items.Add(mnu);
  itm := TMenuItem.Create(mnu);
  itm.Caption := 'Open...';
  mnu.Add(itm);
  itm.OnClick := @DoFileOpen;
end;

procedure TForm1.DoFileOpen(Sender: TObject);
begin
  ShowMessage('Open File');
  PostMessage(Form2.Handle, LM_USER+1, 0, 0);
end;                       

{ TForm2 ----------------------------- }

procedure TForm2.FormCreate(Sender: TObject);
var
  mnu, itm: TMenuItem;
begin
  Menu := TMainMenu.Create(Self);
  mnu := TMenuItem.Create(Menu);
  mnu.Caption := 'Data';
  Menu.Items.Add(mnu);
  itm := TMenuItem.Create(mnu);
  itm.Caption := 'Statistics...';
  mnu.Add(itm);
  itm.OnClick := @DoDataStatistics;
end;

procedure TForm2.DoDataStatistics(Sender: TObject);
begin
  ShowMessage('Data Statistics');
end;             

I attached a example project. Comment out the PostMessage line and the behavior is fine.

Cheers,
VTwin
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: TForm.Menu question
« Reply #8 on: February 27, 2015, 11:08:42 pm »
As described before the menu is restored when you click off and back on the form. :(
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: TForm.Menu question
« Reply #9 on: February 27, 2015, 11:13:48 pm »
I found a workaround at least! :)

Code: [Select]
procedure TForm1.DoFileOpen(Sender: TObject);
var
  tmenu: TMainMenu;
begin
  ShowMessage('Open File');
  PostMessage(Form2.Handle, LM_USER+1, Self.Handle, 0);
  tmenu := Menu;
  Menu := nil;
  Menu := tmenu;
end;

 %)

Can anyone confirm if this is just a Carbon bug?
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

 

TinyPortal © 2005-2018