在Delphi中重新创建TFORM的功能
我需要一个重新创建表格的程序。
原因是我有许多不同组成部分的形式。这些组件值(编辑框文本,检查或不检查的复选框等)保存在内部,并再次加载Isnide Onshow。这样可以确保在程序的运行之间保留所有用户设置。
当他们进行更改(故意或其他方式)导致问题时,问题就来了。我希望能够首先安装应用程序时将表单“重置”回默认设置。
我可以创建一个运行此代码的重置按钮
FormName.free;
DeleteFile('FormNameSettings.ini');
FormName:=TFormName.Create(Application);
FormName.show;
,以执行所需的操作。表单已关闭,清除设置文件(因此,在再次显示时未恢复状态),然后重新创建表单。该表单现在具有原始默认设置。
我的问题是试图将该代码从多种形式轻松调用的函数中。
procedure ResetForm(form:tform;filename:string);
begin
form.free;
if fileexists(filename)=true then deletefile(filename);
<what goes here to recretae the form by the passed tform?>
end;
谁能帮助使该重置程序正常工作?最新的Delphi 11。
I need a procedure that recreates a form.
The reason being is that I have forms with many different components. These component values (edit box text, checkbox checked or not, etc) are saved inside onhide and loaded again isnide onshow. This makes sure all user settings are retained between runs of the program.
The problem comes when they make a change (intentionally or otherwise) that leads to problems. I want to be able to "reset" the form back to the default settings when the application is first installed.
I can create a reset button that runs this code
FormName.free;
DeleteFile('FormNameSettings.ini');
FormName:=TFormName.Create(Application);
FormName.show;
That does what is required. The form is closed, clears the settings file (so states are not restored when it shows again) and then recreates the form. The form now has the original default settings.
My problem is trying to get that code into a function that I can call easily from multiple forms.
procedure ResetForm(form:tform;filename:string);
begin
form.free;
if fileexists(filename)=true then deletefile(filename);
<what goes here to recretae the form by the passed tform?>
end;
Can anyone help get that ResetForm procedure working? Latest Delphi 11.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要返回新创建的表单,我们实际上需要该表单的VAR参数,但是仅这并不是很优雅,因为一个人不能将派生的表单类别传递给Type tform的VAR参数,并且必须做一个硬铸件以取悦编译器。即使使用返回tform的函数也不好得多,因为结果很可能被分配给了派生表单类的变量,并且还将被编译器拒绝。
多亏了仿制药,我们可以编写一些克服这些限制的代码。由于Delphi不支持独立的通用过程或功能,因此我们将其包装在记录声明中:
我们还需要保存有关以后使用的表单的一些信息:
是当前显示的 。
To return the newly created form we actually need a var parameter for the form, but that alone is not very elegant, because one cannot pass a derived form class to a var parameter of type TForm and has to do a hard cast to please the compiler. Even using a function that returns a TForm is not much better as the result is most likely assigned to a variable of a derived form class and that would also be rejected by the compiler.
Thanks to generics we can write some code that overcomes these restrictions. As standalone generic procedures or functions are not supported in Delphi, we wrap it inside a record declaration:
We also need to save some information about the form for later use:
This allows to recreate the form.