Recent

Author Topic: [Solved] Remove a certain sub menu item runtime  (Read 2066 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 618
[Solved] Remove a certain sub menu item runtime
« on: August 08, 2021, 06:38:40 pm »
I have a menu like this:
Program
  Connections
    con a
    con b
    ...
Quit


Now I want to delete everything under Connections and then delete connections from the menu.
Deleting everything under connections works fine but I can't remove de "connection" menu item.

Code: Pascal  [Select][+][-]
  1. procedure TFrm_main.PutOracleConnectionsInMenu();
  2. var
  3.   MainMemuOraSubItem, MainMemuOraConnSubItem, tmp : TMenuItem;
  4.   GetOraConNames :  TOraConmaintain;
  5.   i,j : Word;
  6.   OraConItem  : TOraConnectionData;
  7.   Found : Boolean;
  8. begin
  9.   // First delete all connections from menu
  10.   Cursor := crHourGlass;
  11.   Found := false;
  12.  
  13.   for i:= 0 to MainMenu1.Items.Count-1 do
  14.     begin
  15.       for j:= 0 to  MainMenu1.Items[i].Count -1 do  // Main menu
  16.         begin
  17.           if MainMenu1.Items[i].Items[j].Caption = 'Connections' then // subitem of a main menu
  18.             begin
  19.               Found := True;
  20.  
  21.               While MainMenu1.Items[i].Items[j].Count > 0 Do
  22.                   MainMenu1.Items[i].Items[j].Delete(MainMenu1.Items[i].Items[j].Count - 1);   // delete the menu items under item "Connections"
  23.  
  24.               if Found then
  25.                 begin
  26.                    //MainMenu1.Items[i].Items[j].Remove(MainMenu1.Items[i].Items[j]);
  27.                    found := False;
  28.                    tmp := MainMenu1.Items[i].Items[j];
  29.                     MainMenu1.Items[i].Remove(tmp);  // wrong
  30.  
  31.                   //  While MainMenu1.Items[i].Count >= 0 Do
  32.                   //      MainMenu1.Items[i].Delete(MainMenu1.Items[i].Items[j]); wrong
  33.                 end;
  34.  
  35.             end;
  36.         end;
  37.     end;    
  38. end;
« Last Edit: August 08, 2021, 07:38:21 pm by Hansvb »

wp

  • Hero Member
  • *****
  • Posts: 11910
Re: Remove a certain sub menu item runtime
« Reply #1 on: August 08, 2021, 07:00:32 pm »
Use .Free rather than .Remove; it destroys recursively the child items:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var i, j: Integer;
  3. begin
  4.   for i := 0 to MainMenu1.Items.Count-1 do
  5.   begin
  6.     for j := MainMenu1.Items[i].Count-1 downto 0 do
  7.     begin
  8.       if MainMenu1.Items[i].Items[j].Caption = 'Connection' then
  9.       begin
  10.         MainMenu1.Items[i].Items[j].Free;
  11.         exit;
  12.       end;
  13.     end;
  14.   end;
  15. end;

But be careful. When the menu was created at designtime every menu item has a variable in the form declaration. After destruction of the "connection" item all its child items remain in the form declaration although the objects do not exist any more. You can produce nice crashes by accessing them...

Hansvb

  • Hero Member
  • *****
  • Posts: 618
Re: Remove a certain sub menu item runtime
« Reply #2 on: August 08, 2021, 07:38:02 pm »
Thanks again Wp.
.Free works.

And you're warning... I create the "Connections" menu item run time in the form create method.
The idea is that a user will create connections to Oracle schemas. The connection data is stored in a SQlite database. These connections (table) will be retrieved when the app starts and then added to the menu. Or when a new connection is saved and then i remove all of them from the menu and re create the connection menu item.



 

TinyPortal © 2005-2018