Recent

Author Topic: From which main menu item was the form called  (Read 1539 times)

dietmar

  • Full Member
  • ***
  • Posts: 170
From which main menu item was the form called
« on: September 24, 2021, 03:27:45 pm »
Hi,

I have a form which I want to be opened via several main menu items.
To have the form behave in different ways, I need the information which main menu item called it.

How can I get this information (out of Sender: TObject?) ?

Thx!
--Dietmar
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: From which main menu item was the form called
« Reply #1 on: September 24, 2021, 05:11:16 pm »
Well, how does the menu item open that form (i.o.w. show us the code in the menu items onclick or action handler).

Bart

JanRoza

  • Hero Member
  • *****
  • Posts: 672
    • http://www.silentwings.nl
Re: From which main menu item was the form called
« Reply #2 on: September 24, 2021, 05:32:23 pm »
A very simple way would be to put a hidden Tedit on the form and in the OnClick procedures of the menu items fill it with an unique value.
That way you always know who the caller was.
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
       Lazarus 3.2 FPC 3.2.2
       CodeTyphon 8.40 FPC 3.3.1

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: From which main menu item was the form called
« Reply #3 on: September 24, 2021, 05:38:12 pm »
That should do the trick:

Code: Pascal  [Select][+][-]
  1. if Sender is TMenu {or TAction, if you're using TAction} then begin
  2.   if ( Sender as TMenu {or TAction, if you're using TAction}).name = 'mnuFoo' ) then
  3.     ...\...
  4.   else if
  5.     ...\...
  6. end;
« Last Edit: September 24, 2021, 05:51:50 pm by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: From which main menu item was the form called
« Reply #4 on: September 24, 2021, 05:58:53 pm »
Well, if the OnClick of the MenuIte does something like:
Code: Pascal  [Select][+][-]
  1.   SomeForm.ShowModal
then SomeForm has no way of checking which MenuItem invoked it, hence my requets to show us the code.

The SomeForm could have a property Caller: TObject.
Then you can set that before calling ShowModal.

But, again, without the actual code we're just guessing.

Bart

dietmar

  • Full Member
  • ***
  • Posts: 170
Re: From which main menu item was the form called
« Reply #5 on: September 24, 2021, 06:08:10 pm »
Yes, I open the form via ShowModal, but I am open to other solutions ;)

--Dietmar
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: From which main menu item was the form called
« Reply #6 on: September 24, 2021, 07:00:37 pm »
The attached very simple project shows a modal form displaying which menu item invoked it.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: From which main menu item was the form called
« Reply #7 on: September 25, 2021, 12:23:46 pm »
You don't need to call ShowModal directly in order to show your form. You can implement some other method, like
Code: Pascal  [Select][+][-]
  1. procedure TMyForm.ShowMyForm(AFromWhere:TFromWhere);
  2. begin
  3.   FromWhere := AFromWhere;//To know, where it's called from
  4.   ShowModal;
  5. end;
  6.  
« Last Edit: September 25, 2021, 03:06:42 pm by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

 

TinyPortal © 2005-2018