Recent

Author Topic: [SOLVED] PopupMenu Dynamic  (Read 1705 times)

bourbon

  • New Member
  • *
  • Posts: 22
[SOLVED] PopupMenu Dynamic
« on: August 05, 2025, 06:26:36 pm »
Hello,
I'm working on Linux (Mint 22.1).

I want to create a dynamic pop-up menu using a small file containing the labels for each item to be created. The goal is to populate a Memo with the label of the selected item, adding the current date before it.

I managed to create it, and the display works fine, but I'm running into two problems:

1- During creation: I can't assign a procedure to the OnClick event. This is a standard procedure in the same source code.

Code: Pascal  [Select][+][-]
  1. MenuItem.OnClick := CtrlLigneVierge;
  2. Error: Incompatible types: got "untyped" expected "<procedure variable type of procedure(TObject) of object;Register>"

2- Unable to retrieve the caption label (the date is correctly positioned)
Code: Pascal  [Select][+][-]
  1. Memo1.Lines[WLine] := ' - ' + FormatDateTime('dd/mm/yyyy',now) + ' '
  2. + PopupMenu2.Items[popupmenu2.MenuIndex];
  3.  
  4. Error: identifier idents no member "MenuIndex"

(Syntax that works with a combo box). I tried variations without success.
Currently, to execute this action, I have to use the memo's "OnMouseDown" event to execute the "CtrlLigneVierge" procedure.

Perhaps there is missing information to save when creating the popup menu? I haven't found any additional information.

FYI: The program currently works well, but with a popup menu updated via the menu editor. The downside is that every time there's a change to make to one or more labels, I have to modify the program and recompile it. Furthermore, several people use this program, and the labels may be different, depending on each user's usage.
My goal is to provide the ability to edit the file in the program via a small form with a dbgrid. Everyone will put whatever they want in it. The list will therefore contain what interests the user, and only what interests them, instead of having all the labels for all users.

Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.   WLigne : integer;
  4.   MenuItem : TMenuItem;
  5.  
  6. implementation
  7.  
  8. {$R *.lfm}
  9.  
  10. { TForm1 }
  11.  
  12. // --------------------------------------------------------------------------
  13. // FORME | Create
  14. // --------------------------------------------------------------------------
  15. procedure TForm1.FormCreate(Sender: TObject);
  16. var
  17.   MenuItem : TMenuItem;
  18.   Wi : integer;
  19. begin
  20.   if not dbf1.active then dbf1.open;
  21.   // initialise Items PopupMenu;
  22.   dbf1.first;
  23.   Wi := 0;
  24.   While not Dbf1.EOF do
  25.     begin
  26.       Wi := Wi + 1;
  27.       MenuItem := TMenuItem.Create(PopupMenu2);
  28.       { Ajouter au PopupMenu }
  29.       MenuItem.Caption := Dbf1LIBELSUIVI.Value;
  30.       MenuItem.name := 'Menuitem' + IntToStr(Wi);;
  31.       MenuItem.OnClick := CtrlLigneVierge;
  32.       PopupMenu2.Items.Add(MenuItem);
  33.       dbf1.next;
  34.     end;
  35.   //Dbf1.Close;
  36. end;
  37.  
  38. // --------------------------------------------------------------------------
  39. // MEMO | Controle écrase ligne en cours
  40. // --------------------------------------------------------------------------
  41. procedure TForm1.CtrlLigneVierge;
  42. begin
  43.   if (Memo1.Lines[WLigne] <> '') then
  44.     if MessageDlg('Ecraser le contenu de la ligne Mémo (Ligne ' + intToStr(Wligne + 1) + ') ?',
  45.         mtConfirmation, [mbYes, mbNo], 0, mbNo) = mrNo
  46.         then
  47.           Abort
  48.         else
  49.           // ItemIndex
  50.           Memo1.Lines[WLigne] := ' - ' + FormatDateTime('dd/mm/yyyy',now) + ' '
  51.               + PopupMenu2.Items[popupmenu2.MenuIndex];  // compile pas (err)
  52. end;
  53.  

« Last Edit: August 07, 2025, 11:28:34 am by bourbon »

Tomu

  • New Member
  • *
  • Posts: 14
Re: PopupMenu Dynamic
« Reply #1 on: August 06, 2025, 10:36:49 pm »
Make sure that the procedure CtrlLigneVierge has the same signature as an OnButtonClick event.

bourbon

  • New Member
  • *
  • Posts: 22
Re: PopupMenu Dynamic
« Reply #2 on: August 07, 2025, 07:02:11 am »
Okay, thanks Tomu.
But how can I do this?

bourbon

  • New Member
  • *
  • Posts: 22
Re: PopupMenu Dynamic
« Reply #3 on: August 07, 2025, 07:31:14 am »
Okay, thanks Tomu.
But how can I do this?

Note: I also used the following syntax, without success.

Code: Pascal  [Select][+][-]
  1. MenuItem.OnClick := @CtrlLigneVierge;

LV

  • Sr. Member
  • ****
  • Posts: 427
Re: PopupMenu Dynamic
« Reply #4 on: August 07, 2025, 09:15:27 am »
1. Make CtrlLigneVierge a method of the form with the correct signature.
2. Use the Sender: TObject parameter to get the selected item.

Maybe something like this

Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.     Memo1: TMemo;
  4.     PopupMenu2: TPopupMenu;
  5.     procedure FormCreate(Sender: TObject);
  6.   private
  7.     WLigne: Integer;
  8.     procedure CtrlLigneVierge(Sender: TObject);
  9.   end;
  10.  
  11. procedure TForm1.FormCreate(Sender: TObject);
  12. var
  13.   MenuItem: TMenuItem;
  14.   Wi: Integer;
  15. begin
  16.   if not dbf1.Active then dbf1.Open;
  17.   dbf1.First;
  18.   Wi := 0;
  19.  
  20.   while not dbf1.EOF do
  21.   begin
  22.     Inc(Wi);
  23.     MenuItem := TMenuItem.Create(PopupMenu2);
  24.     MenuItem.Caption := dbf1LIBELSUIVI.Value;
  25.     MenuItem.Name := 'MenuItem' + IntToStr(Wi);
  26.     MenuItem.OnClick := @CtrlLigneVierge;
  27.     PopupMenu2.Items.Add(MenuItem);
  28.     dbf1.Next;
  29.   end;
  30. end;
  31.  
  32. procedure TForm1.CtrlLigneVierge(Sender: TObject);
  33. var
  34.   Item: TMenuItem;
  35. begin
  36.   if Sender is TMenuItem then
  37.   begin
  38.     Item := TMenuItem(Sender);
  39.  
  40.     if Memo1.Lines[WLigne] <> '' then
  41.       if MessageDlg('Overwrite the current memo line (Line ' + IntToStr(WLigne + 1) + ')?',
  42.         mtConfirmation, [mbYes, mbNo], 0, mbNo) = mrNo
  43.       then
  44.         Abort;
  45.  
  46.     Memo1.Lines[WLigne] := ' - ' + FormatDateTime('dd/mm/yyyy', Now) + ' ' + Item.Caption;
  47.   end;
  48. end;
  49.  

bourbon

  • New Member
  • *
  • Posts: 22
Re: PopupMenu Dynamic
« Reply #5 on: August 07, 2025, 11:27:26 am »
Thanks LV.
It works perfectly. I had to make some corrections to manage the line number (which was arriving too late).

 

TinyPortal © 2005-2018