Every now and then I sometime get the following error "A component named dlgPrinterSettings Already exists." not necesarry the "dlgprintersettings" everytime, but still, I would like to know what might be the cause of this error in the following piece of code:
每隔一段时间,我就会出现以下错误:“一个名为dlgPrinterSettings的组件已经存在”,但我仍然想知道下面这段代码中可能出现的错误的原因:
procedure TfrmApplicationMain.actPrinterExecute(Sender: TObject);
begin
with TdlgPrinterSettings.Create(self) do
try
ChkEncodeMag.IsChecked := GetUserDataBoolean('MAGNETIC_ENCODING');
ChkEncodeFromDatabase.IsChecked := GetUserDataBoolean('MAGNETIC_DATABASE');
ShowModal;
finally
SetUserData('MAGNETIC_ENCODING',BoolToStr(ChkEncodeMag.IsChecked));
SetUserData('MAGNETIC_DATABASE',BoolToStr(ChkEncodeFromDatabase.IsChecked));
free;
end;
end;
should I use "nil" instead of "self" in the create ?
我应该在create中使用“nil”而不是“self”吗?
2 个解决方案
#1
2
Just an observation,
只是一个观察,
if one of the SetUserData
methods throws an exception, your TdlgPrinterSettings instance will not get freed. The next time you call actPrinterExecute
would give you the error you mention.
如果一个SetUserData方法抛出一个异常,那么您的TdlgPrinterSettings实例将不会被释放。下次您调用actprinterexecutete时,会给出您所提到的错误。
ps. Don't use with
. Declare a local variable and use that. You can search on with & Delphi to find some heated debates about it's use. I'm not guilt-free myself here but I would not use it in this case.
ps。不要使用。声明一个本地变量并使用它。你可以搜索& Delphi来发现关于它的一些激烈的争论。我在这里没有负罪感,但在这种情况下我不会使用它。
#2
-1
I suppose that :
我认为:
There is an Component on your Form named " dlgPrinterSettings " , it`s Class Name will be " TdlgPrinterSettings " When you Create an Component by this line :
在您的表单上有一个名为“dlgPrinterSettings”的组件,当您通过这一行创建一个组件时,它的类名将是“TdlgPrinterSettings”:
with TdlgPrinterSettings.Create(self) do
TdlgPrinterSettings.Create(自我)
A Component will create that derived from " TdlgPrinterSettings " , and it`s name is equal to your default component that you put on the form , So you will get an Error !
一个组件将创建从“TdlgPrinterSettings”派生出来的组件,它的名称等于您在表单上放置的默认组件,因此您将会得到一个错误!
why you want to create it ?
为什么要创建它?
If " TdlgPrinterSettings " is a " TPrinterSetupDialog " you do like this :
如果“TdlgPrinterSettings”是一个“TPrinterSetupDialog”,您可以这样做:
procedure TfrmApplicationMain.actPrinterExecute(Sender: TObject);
begin
with TPrinterSetupDialog.Create(self) do
try
ChkEncodeMag.IsChecked := GetUserDataBoolean('MAGNETIC_ENCODING');
ChkEncodeFromDatabase.IsChecked := GetUserDataBoolean('MAGNETIC_DATABASE');
ShowModal;
finally
SetUserData('MAGNETIC_ENCODING',BoolToStr(ChkEncodeMag.IsChecked));
SetUserData('MAGNETIC_DATABASE',BoolToStr(ChkEncodeFromDatabase.IsChecked));
free;
end;
end;
I`m not sure but maybe it is !!
我不确定,但也许是!!
Good Luck ...
祝你好运…
#1
2
Just an observation,
只是一个观察,
if one of the SetUserData
methods throws an exception, your TdlgPrinterSettings instance will not get freed. The next time you call actPrinterExecute
would give you the error you mention.
如果一个SetUserData方法抛出一个异常,那么您的TdlgPrinterSettings实例将不会被释放。下次您调用actprinterexecutete时,会给出您所提到的错误。
ps. Don't use with
. Declare a local variable and use that. You can search on with & Delphi to find some heated debates about it's use. I'm not guilt-free myself here but I would not use it in this case.
ps。不要使用。声明一个本地变量并使用它。你可以搜索& Delphi来发现关于它的一些激烈的争论。我在这里没有负罪感,但在这种情况下我不会使用它。
#2
-1
I suppose that :
我认为:
There is an Component on your Form named " dlgPrinterSettings " , it`s Class Name will be " TdlgPrinterSettings " When you Create an Component by this line :
在您的表单上有一个名为“dlgPrinterSettings”的组件,当您通过这一行创建一个组件时,它的类名将是“TdlgPrinterSettings”:
with TdlgPrinterSettings.Create(self) do
TdlgPrinterSettings.Create(自我)
A Component will create that derived from " TdlgPrinterSettings " , and it`s name is equal to your default component that you put on the form , So you will get an Error !
一个组件将创建从“TdlgPrinterSettings”派生出来的组件,它的名称等于您在表单上放置的默认组件,因此您将会得到一个错误!
why you want to create it ?
为什么要创建它?
If " TdlgPrinterSettings " is a " TPrinterSetupDialog " you do like this :
如果“TdlgPrinterSettings”是一个“TPrinterSetupDialog”,您可以这样做:
procedure TfrmApplicationMain.actPrinterExecute(Sender: TObject);
begin
with TPrinterSetupDialog.Create(self) do
try
ChkEncodeMag.IsChecked := GetUserDataBoolean('MAGNETIC_ENCODING');
ChkEncodeFromDatabase.IsChecked := GetUserDataBoolean('MAGNETIC_DATABASE');
ShowModal;
finally
SetUserData('MAGNETIC_ENCODING',BoolToStr(ChkEncodeMag.IsChecked));
SetUserData('MAGNETIC_DATABASE',BoolToStr(ChkEncodeFromDatabase.IsChecked));
free;
end;
end;
I`m not sure but maybe it is !!
我不确定,但也许是!!
Good Luck ...
祝你好运…