W
W
White_Bambie2019-11-12 13:47:57
Delphi
White_Bambie, 2019-11-12 13:47:57

How to save PopupMenu items in INI?

Good day!
There is a form, on the form MainMenu with items("save form" and "load form"), Edit and Button. The Edit has a PopupMenu attached to it.
When a Button is clicked, an item is added to the PopupMenu.

procedure TfMain.ButtonClick(Sender: TObject);
var
newitem: TMenuItem;
begin
newitem:= TMenuItem.Create(TfMain.PopupMenu);
newitem.Caption:= '1';
newitem.OnClick:= ClickItem; //ClickItem - какая-то процедура
TfMain.PopupMenu.Items.Add(newitem);
end;

When you click on "save form" the following happens:
Ini:= TIniFile.Create(ExtractFilePath(ParamStr(0))+'Set.ini');
//и вот тут начинается проблема...
//Ini.Write(???)('Настройки','Список пунктов',TfMain.PopupMenu.Items);
Ini.Free;

-
What needs to be written in the form save procedure so that PopupMenu items are saved in INI?
In essence, PopupMenu.Items is a MENU, that is, it contains several objects that include a Caption and a number of other parameters, including the OnClick event.
-
I look forward to your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zed, 2019-11-12
@zedxxx

You need to manually, for each menu item, write down its properties in ini:

for i := 0 to TfMain.PopupMenu.Items.Count - 1 do begin
  ini.WriteString('Настройки', 'Пункт_' + IntToStr(i) + '_Caption', TfMain.PopupMenu.Items[i].Caption);
  ... // и так далее, для всех остальных параметров, которые надо сохранить
end;

As for the OnClick event, there are 2 options:
1. (bad) assign/match each event with some ID (number) and write it as an Integer, so when reading parameters from ini you will know which function to use
2. (good) for all menu items, use one OnClick event, and for each menu item, write its own unique value to the Tag property, by which you will distinguish which specific item the user clicked on and perform this or that action. Accordingly, in ini it will be necessary to write the Tag value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question