我想简化将表单状态保存到磁盘的问题。我使用我自己的从TIniFile派生的INI文件类来读取表单中的"all“控件的状态。就像这样:
procedure TMyIniFile.Read(Comp: TComponent);
begin
if ValueExists(Section, Comp.Name) then
begin
if Comp.InheritsFrom(TAction)
then TAction(Comp).Checked:= ReadBool(Section, Comp.Name, FALSE)
else
if Comp.InheritsFrom(TCheckBox) etc
end;
end;我用我的课像这样:
TYPE
TformTester = class(TForm)
MyAction: TAction;
procedure actMyActionExecute(Sender: TObject);
...
procedure TformTester.FormDestroy(Sender: TObject);
VAR
MyIniFile: TMyIniFile;
begin
MyAction.Checked:= true;
MyIniFile:= TMyIniFile.Create('Main Form');
MyIniFile.write(MyAction); // <------ This saves the 'Checked' property of MyAction.
...
end;我验证了INI文件,状态根据关闭时的属性状态正确保存(真/假)。
procedure TformTester.FormStartUp;
VAR MyIniFile: TMyIniFile;
begin
MyIniFile:= TMyIniFile.Create('Main Form');
MyIniFile.read(MyAction); // <------ This reads the 'Checked' property of MyAction. It should execute the actMyActionExecute but it doesn't.
assert(MyAction.Checked); // <---- Yes, it is checked
...
end;
procedure TformTester.MyActionExecute(Sender: TObject);
begin
if MyAction.Checked
then Caption:= 'Action checked'
else Caption:= 'Action is un-checked!';
end;问题:为什么在执行MyIniFile.read(MyAction)时不调用actMyActionExecute?
PS: MyIniFile.read(MyCheckbox)如果不传递TAction,而是传递其他任何内容(例如,复选框),就可以工作。我是说MyCheckbox.OnClick被处决了!
发布于 2017-09-16 12:39:40
当调用链接控件时,将触发操作OnExecute。例如,按下按钮或选择菜单项。或者,如果在事件上显式调用Execute,则会触发它。
当您修改OnExecute事件的任何属性时,它不会被触发。这是设计上的,也是相当合理的。当用户做某事时,此事件触发。当程序员设置操作时就不会了。
https://stackoverflow.com/questions/46253875
复制相似问题