Recent

Author Topic: [Solved]MenuItem in multiple menus  (Read 2550 times)

dbannon

  • Hero Member
  • *****
  • Posts: 3742
    • tomboy-ng, a rewrite of the classic Tomboy
[Solved]MenuItem in multiple menus
« on: April 01, 2019, 01:47:06 am »
I have decided I need to create some menus dynamically because I will need at least three versions with exactly the same captions and one call back function with a case statement looking at the MenuItem's name. So, in a simplified form, I'd like to do this -

Code: Pascal  [Select][+][-]
  1. procedure TForm1.AddMenuItem(Item, ItemName : string; OClick : TNotifyEvent);
  2. var
  3.     MItem : TMenuItem;
  4. begin
  5.     MItem := TMenuItem.Create(Self);
  6.     MItem.Caption := Item;
  7.     MItem.OnClick := OClick;
  8.     MItem.Name := ItemName;
  9.     PopupMenu.Items.Add(MItem);
  10.     MainMenu.Items.Add(MItem);
  11. end;    
       

However, I find menus are not willing to share menu items, when I add a particular item to a second menu, exception !

I can, obviously make an separate menuitem for each menu, but each item will have to have a new name, making it messy to handle in the one call back function I want to use. Is there a better way for me to do this ?

Davo

« Last Edit: April 01, 2019, 05:22:17 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

jamie

  • Hero Member
  • *****
  • Posts: 7603
Re: MenuItem in multiple menus
« Reply #1 on: April 01, 2019, 02:23:44 am »
You can connect a single OnClick event to many objects of the same type, the SENDER variable will report which
menu ITEM is actually calling it.
 
Also, each Object (Item) has a TAG property that you can use to custom ID it..

So while in the OnClick Event you can do something like this

Case TMenuItem(Sender).Tag of
 
   1. First one;
   2. Second One;
   3. Third etc..
 End;

 The Name property can remain the same for all of them, but change the TAG value which you can read
at this point.
 
 This means you'll need to create unique menu items of course but they can be the same types, just different tag
id's

P.S.
 You can create a MenuItem and simply Assign the values of one that already exist to it. this way you only need to
create the TEXT for the first one.
« Last Edit: April 01, 2019, 02:25:29 am by jamie »
The only true wisdom is knowing you know nothing

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: MenuItem in multiple menus
« Reply #2 on: April 01, 2019, 02:48:21 am »
As Jamie states make a onclick-event and assign the tags.
Then you can have something like this in your code:
Code: Pascal  [Select][+][-]
  1. procedure TForm_RefereeMain.MainMenu_Click(Sender: TObject);
  2. var cSQL, ss: string;
  3.     maxid, y: integer;
  4.     fa: TForm_About;
  5.     fo: TForm_Options;
  6.     fb: TForm_Betalingen;
  7.     fco: TForm_Coordinatoren_Overzicht;
  8.     fdo: TForm_Divisies_Overzicht;
  9.     feo: TForm_Evaluatie_Overzicht;
  10.     fkg: TForm_KM_Gereden;
  11.     fkm: TForm_KM_Vergoeding;
  12.     flo: TForm_Licentie_Overzicht;
  13.     fro: TForm_Referee_Overzicht;
  14.     frs: TForm_RapportStraffenOverzicht;
  15.     fso: TForm_Supervisors_Overzicht;
  16.     fto: TForm_Teams_Overzicht;
  17.     fwo: TForm_Wedstrijd_Overzicht;
  18.     fyo: TForm_IJsbanen_Overzicht;
  19.  
  20.  
  21. begin
  22.   for maxid := 1 to Screen.FormCount - 1 do begin
  23.   // bij 1 beginnen en niet bij 0 omdat het beginscherm altijd open is.
  24.     if Screen.Forms[maxid].Visible then begin
  25.       Form_Message.MsgWindow(mVraag, bOk,
  26.         'First close the active window' + sCrLf +
  27.         'before opening a new one !!', PrgNaam);
  28. //      Screen.Forms[maxid].SetFocus;
  29.       exit;
  30.     end;  // if
  31.   end;  // for
  32.   case TMenuItem(Sender).Tag of
  33.     11: begin    // Files Exit
  34.           Form_Message.MsgWindow(mVraag, bYesNo,
  35.             'Are you sure that you want to exit' + sCrLf + PrgNaam + ' ?', PrgNaam);
  36.           if Form_MessageButtonPressed = 0 then begin
  37.             Trans_RefereeDB.Active := False;
  38.             Connect_RefereeDB.Connected := False;
  39.             Application.Terminate;
  40.             Exit;
  41.           end;  // if
  42.         end;     // Files Exit
  43.     12: begin
  44.           fo := TForm_Options.Create(Self);
  45.           fo.Show;
  46.         end;     // 12
  47.     21: begin
  48.           fwo := TForm_Wedstrijd_Overzicht.Create(Self);
  49.           fwo.Show;
  50.         end;     // 21
  51.     22: begin
  52.           fdo := TForm_Divisies_Overzicht.Create(Self);
  53.           fdo.Show;
  54.         end;     // 22
  55.     23: begin
  56.           fto := TForm_Teams_Overzicht.Create(self);
  57.           fto.Show;
  58.         end;     // 23
  59.     24: begin
  60.           frs := TForm_RapportStraffenOverzicht.Create(Self);
  61.           frs.Show;
  62.         end;     // 24
  63.     31: begin
  64.           fro := TForm_Referee_Overzicht.Create(Self);
  65.           fro.Show;
  66.         end;     // 31
  67.     32: begin
  68.           feo := TForm_Evaluatie_Overzicht.Create(Self);
  69.           feo.Show;
  70.         end;     // 32
  71.     33: begin
  72.           fkg := TForm_KM_Gereden.Create (Self);
  73.           fkg.Show;
  74.         end;     // 33
  75.     71: begin
  76.           if CheckReport (ResourceDir + 'rpt_Wedstrijden_Seizoen.lrf') = False then begin
  77.             Form_Message.MsgWindow(mUitroep, bOk,
  78.               'The report you want to' + sCrLf +
  79.               'access is not available.' + sCrLf + sCrLf +
  80.               'Please contact vendor for' + sCrLf +
  81.               'further information.', PrgNaam);
  82.             Exit;
  83.           end;  // if
  84.           // set de query
  85.           cSQL := 'SELECT * FROM qry_Overzicht_Wedstrijden WHERE Seizoen = ' +
  86.                   BepaalHuidigSeizoen.ToString;
  87.           TQ_Rapport_Master.Active := False;
  88.           TQ_Rapport_Master.DataBase := Connect_RefereeDB;
  89.           TQ_Rapport_Master.SQL.Text := cSQL;
  90.           TQ_Rapport_Master.Active := True;
  91.           // open het rapport in LazReport
  92.           frReport_Evaluatie.LoadFromFile(ResourceDir + 'rpt_Wedstrijden_Seizoen.lrf');
  93.           frReport_Evaluatie.Title:='All Games played in Season ' +
  94.                MLookUp ('Sei_Periode', 'tbl_Seizoenen', 'Sei_ID='+BepaalHuidigSeizoen.ToString);
  95.           frReport_Evaluatie.ShowReport;
  96.         end;  // 71
  97.     72: begin
  98.           if CheckReport (ResourceDir + 'rpt_Wedstrijden_Alle.lrf') = False then begin
  99.             Form_Message.MsgWindow(mUitroep, bOk,
  100.               'The report you want to' + sCrLf +
  101.               'access is not available.' + sCrLf + sCrLf +
  102.               'Please contact vendor for' + sCrLf +
  103.               'further information.', PrgNaam);
  104.             Exit;
  105.           end;  // if
  106.           // set de query
  107.           cSQL := 'SELECT * FROM qry_Overzicht_Wedstrijden';
  108.           TQ_Rapport_Master.Active := False;
  109.           TQ_Rapport_Master.DataBase := Connect_RefereeDB;
  110.           TQ_Rapport_Master.SQL.Text := cSQL;
  111.           TQ_Rapport_Master.Active := True;
  112.           // open het rapport in LazReport
  113.           frReport_Evaluatie.LoadFromFile(ResourceDir + 'rpt_Wedstrijden_Alle.lrf');
  114.           frReport_Evaluatie.ShowReport;
  115.         end;  // 72
  116.     73: begin
  117.           fb := TForm_Betalingen.Create (Self);
  118.           fb.Show;
  119.         end;  // 73
  120.     74: begin
  121.           if CheckReport (ResourceDir + 'rpt_Wedstrijden_Club.lrf') = False then begin
  122.             Form_Message.MsgWindow(mUitroep, bOk,
  123.               'The report you want to' + sCrLf +
  124.               'access is not available.' + sCrLf + sCrLf +
  125.               'Please contact vendor for' + sCrLf +
  126.               'further information.', PrgNaam);
  127.             Exit;
  128.           end;  // if
  129.           // set de query
  130.           cSQL := 'SELECT * FROM qry_Overzicht_Wedstrijden WHERE (Seizoen = ' +
  131.                   IntToStr(BepaalHuidigSeizoen) + ' AND ABS(Wed_Meetellen) = 1)';
  132.           TQ_Rapport_Master.Active := False;
  133.           TQ_Rapport_Master.DataBase := Connect_RefereeDB;
  134.           TQ_Rapport_Master.SQL.Text := cSQL;
  135.           TQ_Rapport_Master.Active := True;
  136.           // open het rapport in LazReport
  137.           frReport_Evaluatie.LoadFromFile(ResourceDir + 'rpt_Wedstrijden_Club.lrf');
  138.           frReport_Evaluatie.Title:='All Club Games played in Season ' +
  139.                MLookUp ('Sei_Periode', 'tbl_Seizoenen', 'Sei_ID='+BepaalHuidigSeizoen.ToString);
  140.           frReport_Evaluatie.ShowReport;
  141.         end;  // 74
  142.     75: begin
  143.           if CheckReport (ResourceDir + 'rpt_Wedstrijden_Overige.lrf') = False then begin
  144.             Form_Message.MsgWindow(mUitroep, bOk,
  145.               'The report you want to' + sCrLf +
  146.               'access is not available.' + sCrLf + sCrLf +
  147.               'Please contact vendor for' + sCrLf +
  148.               'further information.', PrgNaam);
  149.             Exit;
  150.           end;  // if
  151.           // set de query
  152.           cSQL := 'SELECT * FROM qry_Overzicht_Wedstrijden WHERE (Seizoen = ' +
  153.                   BepaalHuidigSeizoen.ToString + ' AND Wed_Meetellen = 0)';
  154.           TQ_Rapport_Master.Active := False;
  155.           TQ_Rapport_Master.DataBase := Connect_RefereeDB;
  156.           TQ_Rapport_Master.SQL.Text := cSQL;
  157.           TQ_Rapport_Master.Active := True;
  158.           // open het rapport in LazReport
  159.           frReport_Evaluatie.LoadFromFile(ResourceDir + 'rpt_Wedstrijden_Overige.lrf');
  160.           frReport_Evaluatie.Title:='All Non-Club Games played in Season ' +
  161.                MLookUp ('Sei_Periode', 'tbl_Seizoenen', 'Sei_ID='+BepaalHuidigSeizoen.ToString);
  162.           frReport_Evaluatie.ShowReport;
  163.         end;  // 75
  164.     76: begin
  165.           if CheckReport (ResourceDir + 'rpt_Evaluatie_Overzicht_Seizoen.lrf') = False then begin
  166.             Form_Message.MsgWindow(mUitroep, bOk,
  167.               'The report you want to' + sCrLf +
  168.               'access is not available.' + sCrLf + sCrLf +
  169.               'Please contact vendor for' + sCrLf +
  170.               'further information.', PrgNaam);
  171.             Exit;
  172.           end;  // if
  173.           // set de query
  174.           cSQL := 'SELECT * FROM qry_Overzicht_Evaluaties ' +
  175.                   'WHERE Seizoen = ' + BepaalHuidigSeizoen.ToString + ';';
  176.           TQ_Rapport_Master.Active := False;
  177.           TQ_Rapport_Master.DataBase := Connect_RefereeDB;
  178.           TQ_Rapport_Master.SQL.Text := cSQL;
  179.           TQ_Rapport_Master.Active := True;
  180.           // open het rapport in LazReport
  181.           frReport_Evaluatie.LoadFromFile(ResourceDir + 'rpt_Evaluatie_Overzicht_Seizoen.lrf');
  182.           frReport_Evaluatie.ShowReport;
  183.         end;  // 76
  184.     77: begin
  185.           if CheckReport (ResourceDir + 'rpt_Evaluatie_Overzicht_Alle.lrf') = False then begin
  186.             Form_Message.MsgWindow(mUitroep, bOk,
  187.               'The report you want to' + sCrLf +
  188.               'access is not available.' + sCrLf + sCrLf +
  189.               'Please contact vendor for' + sCrLf +
  190.               'further information.', PrgNaam);
  191.             Exit;
  192.           end;  // if
  193.           // set de query
  194.           cSQL := 'SELECT * FROM qry_Overzicht_Evaluaties';
  195.           TQ_Rapport_Master.Active := False;
  196.           TQ_Rapport_Master.DataBase := Connect_RefereeDB;
  197.           TQ_Rapport_Master.SQL.Text := cSQL;
  198.           TQ_Rapport_Master.Active := True;
  199.           // open het rapport in LazReport
  200.           frReport_Evaluatie.LoadFromFile(ResourceDir + 'rpt_Evaluatie_Overzicht_Alle.lrf');
  201.           frReport_Evaluatie.ShowReport;
  202.         end;  // 77
  203.     78: begin
  204.           if CheckReport (ResourceDir + 'rpt_Wedstrijden_Toernooi.lrf') = False then begin
  205.             Form_Message.MsgWindow(mUitroep, bOk,
  206.               'The report you want to' + sCrLf +
  207.               'access is not available.' + sCrLf + sCrLf +
  208.               'Please contact vendor for' + sCrLf +
  209.               'further information.', PrgNaam);
  210.             Exit;
  211.           end;  // if
  212.           // set de query
  213.           cSQL := 'SELECT * FROM qry_Overzicht_Wedstrijden WHERE (Seizoen = ' +
  214.                   BepaalHuidigSeizoen.ToString + ' AND T = ' + SingleQuotedStr('x') + ')';
  215.           TQ_Rapport_Master.Active := False;
  216.           TQ_Rapport_Master.DataBase := Connect_RefereeDB;
  217.           TQ_Rapport_Master.SQL.Text := cSQL;
  218.           TQ_Rapport_Master.Active := True;
  219.           // open het rapport in LazReport
  220.           frReport_Evaluatie.LoadFromFile(ResourceDir + 'rpt_Wedstrijden_Toernooi.lrf');
  221.           frReport_Evaluatie.Title:='All Tournament Games played in Season ' +
  222.                MLookUp ('Sei_Periode', 'tbl_Seizoenen', 'Sei_ID='+BepaalHuidigSeizoen.ToString);
  223.           frReport_Evaluatie.ShowReport;
  224.         end;  // 78
  225.     81: begin
  226.           // bepaal het laatste seizoen
  227.           maxid := strtointdef(MLookup('MAX(Sei_ID)','tbl_Seizoenen','1=1'), 0);
  228.           y := MLookup('Sei_Tot','tbl_Seizoenen','Sei_ID='+maxid.ToString);
  229.           ss := MLookup('Sei_Letter','tbl_Seizoenen','Sei_ID='+maxid.ToString);
  230.           Form_Message.MsgWindow(mVraag, bYesNo,
  231.             'Are you sure that you want to add' + sCrLf +
  232.             'Season ' + IntToStr(y) + '-' + IntToStr(y+1) + ' ?', PrgNaam);
  233.           if Form_MessageButtonPressed = 0 then begin
  234.             // De letter A is ASCII 65.
  235.             // SS[2] := Dus wordt het 64 + (maxid mod 26) is letter.
  236.             // SS[1] := 65 + (maxid DIV 26)
  237.             maxid := maxid + 1;
  238.             SS := '  ';
  239.             SS[2] := CHR(64 + (maxid MOD 26));
  240.             SS[1] := CHR(65 + (maxid DIV 26));
  241.             // insert recordgegevens met het juiste seizoen en id.
  242.             cSQL := 'INSERT INTO tbl_Seizoenen ' +
  243.                     'VALUES (' + IntToStr(maxid) + ',"' + IntToStr(y) + '-' + IntToStr(y+1) + '",'  +
  244.                     IntToStr(y) + ',' + IntToStr(y+1) + ',1,0.0,0.0,"' + SS + '")';
  245.             Connect_RefereeDB.ExecuteDirect(cSQL);
  246.             Trans_RefereeDB.Commit;
  247.             SS := 'Season ' + IntToStr(y) + '-' + IntToStr(y+1) + ' added !!' + sCrLf + sCrLf +
  248.                   'Milage Compensaion is set to € 0.00.' + sCrLf +
  249.                   'Food Compensaion is set to € 0.00.';
  250.             Form_Message.MsgWindow(mInfo, bOk, SS, PrgNaam);
  251.           end;  // if
  252.         end;  // 81
  253.     82: begin
  254.           fco := TForm_Coordinatoren_Overzicht.Create (Self);
  255.           fco.Show;
  256.         end;     // 82
  257.     83: begin
  258.           fyo := TForm_IJsbanen_Overzicht.Create (Self);
  259.           fyo.Show;
  260.         end;     // 83
  261.     84: begin
  262.           fso := TForm_Supervisors_Overzicht.Create (Self);
  263.           fso.Show;
  264.         end;     // 84
  265.     85: begin
  266.           flo := TForm_Licentie_Overzicht.Create(Self);
  267.           flo.Show;
  268.         end;     // 85
  269.     86: begin
  270.           fkm := TForm_KM_Vergoeding.Create(Self);
  271.           fkm.Show;
  272.         end;     // 86
  273.     91: begin
  274.           fa := TForm_About.Create (Self);
  275.           fa.Show;
  276.         end;     // 91
  277.     else
  278.       Form_Message.MsgWindow(mStop, bOk, 'Work in Progress !!', Prgnaam);
  279.   end;     // case
  280. end;     // MainMenu_Click
  281.  

I open other forms and make reports and if I have a tag that is not available I just give an 'Work in Progress'-message.

As you can see you can do a lot.
P.S. this is actual code from a program of mine
« Last Edit: April 01, 2019, 02:50:01 am by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

dbannon

  • Hero Member
  • *****
  • Posts: 3742
    • tomboy-ng, a rewrite of the classic Tomboy
Re: MenuItem in multiple menus
« Reply #3 on: April 01, 2019, 05:21:47 am »

Yep, the .tag seems to be exactly what I need, thanks to both of you !
 
Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018