Strange error "Component named TFrm1 already exists"


I want to allow the user to create multiple instances of the same form (let her name Form1, which is a child form of MDI). Therefore, I have two procedures, such as when I create forms.

procedure MyProcedure1;           // procedure 2 is similar. it also has a var called MyFrm
var MyFrm: TFrm1;
begin
  ... 
  MyFrm:= TFrm1.create(MainForm);
  MyFrm.BringToFront;
  MyFrm.LoadFromFile(someFile);
end;

As you can see, MyFrm is a local var. This is normal for me, since I do not need to programmatically access the form after it is created. There is no other global variable named Frm1. In the OnClose MyFrm event, I have Action: = caFree;

What could cause the error above? The user posted this error. This happened only once, and I cannot reproduce it.


Edit:

  • The error appears in the line "MyFrm: = TFrm1.create".

  • , . , , , , MyProcedure1.
    Delphi , , MyFrm.name = MyFrm,
    MyFrm.name = MyFrm_1,
    MyFrm.name = MyFrm_2,
    MyFrm.name = MyFrm_3 ..

  • MyFrm.Name LoadFromFile. ( ) "MyFrm.Name" MyProcedure1; LoadFromFile. .

  • , SetName TMyFrm. .

    procedure TMyFrm.SetName(const : TComponentName);

     ShowMessage (Value);
     ;
    ;

  • , MainForm.

  • . , ( , ).

+5
4

MainForm TFrm1.Create MainForm. , ( FindComponent ). , .

TFrm1.Create, , LoadFromFile, , , .

nil Owner, , , , MainForm.InsertComponent.

procedure MyProcedure1;           
var MyFrm: TFrm1;
begin
  ... 
  MyFrm:= TFrm1.create(nil);
  MyFrm.BringToFront;
  MyFrm.LoadFromFile(someFile);
  MyFrm.Name := ''; // or some unique name
  MainForm.InsertComponent(MyFrm);
end;
+8

, .

, . , , , .

MyFrm.Name := MyFrm.Name + <something unique>;

a >

MyFrm.Name := '';

Create,

+6

MyFrm.Name ...

, MyFrm.Name ...

+3

, , " " , ​​ Name. (). TForm/TFrame/TPanel (), , . - , - : V_Btn = new TBitBtn (this), V_Btn- > Color = clTeal, V_Btn- > OnClick = Close_The_Window, Name Name . Name , . , , :

TMyeditor* Editor_01 = new TMyeditor(Main_Form);
TMyeditor* Editor_02 = new TMyeditor(Main_Form);
  Editor_01->Parent = Tab_Sheet_Addresses;
  Editor_02->Parent = Tab_Sheet_Billing;

, , . " ".

            End of answer.

, , , , , , , . / , , .. , :

void RegisterClassesWithStreamingSystem(void)
{   
  // Make sure that as part of the startup   
  // code TMyEditor is registered   
  // with the streaming system.   
  #pragma startup RegisterClassesWithStreamingSystem  
  Classes::RegisterClass(__classid(TMyEditor)); 
}

ComponentToString < --- > StringToComponent [* 1] (). , [* 2] . TReader/TWriter. { TReader/TWriter, Delphi}

[, TMyEditor, , Editor_01 Editor_02 TClientDataSet "CDS" ]

//How to write the Editors
String_Version_Of_Editor = ComponentToString(Editor_01);
CDS->Insert();
CDS->FieldByName("Data")->AsString = String_Version_Of_Editor;
CDS->Post();
String_Version_Of_Editor = ComponentToString(Editor_02);
CDS->Insert();
CDS->FieldByName("Data")->AsString = String_Version_Of_Editor;
CDS->Post();

//How to read, create an instance of, set the Owner of 
//(allowing for automatic destruction/deletion
// if desired, Vis-à-vis Let the compiler/runtime package handle that), 
//& setting the form Parent
AnsiString   String_Version_Of_Editor; 
TWinControl* New_Editor;
String_Version_Of_Editor = CDS->FieldByName("Data")->AsString;
//The next line creates/constructs the new editor
New_Editor = StringToComponent(String_Version_Of_Editor);
//The next line sets the new editor Owner to Main_Form 
//It also assigns Main_Form the responsibility of object cleanup
Main_Form->Insert(New_Editor);
//The next line sets the Editor Parent causing it to be part of the 
//displayed user interface (it has been invisble since creation)
New_Editor->Parent = Tab_Sheet_Addresses;
//Move on to the next editor;
CDS->Next();
String_Version_Of_Editor = CDS->FieldByName("Data")->AsString;
New_Editor = StringToComponent(String_Version_Of_Editor);
Main_Form->Insert(New_Editor);
New_Editor->Parent = Tab_Sheet_Billing;

, , , , New_Editor TWincontrol TMyEditor - , , . , , TReader Delphi, , / , RegisterClass. , . TMyEditor , , , - TWinControl * TMyEditor * - , TWinControl * TMyEditor - Example TMyEditor , , ​​. ( DataModule, #include datamodule TMyEditor).

: , , , , . #include <typeinfo> . . TMyEditor, TMyEditor_Generation_01, TMyEditor_Generation_02 .., , :

 if (typeid(New).name() == "TMyEditor *")
   New_Editor->Parent = Tab_Sheet_Addresses;
  else
   if (typeid(New).name() == "TMyEditor_Generation_01 *")
     New_Editor->Parent = Tab_Sheet_Billing;
   else
    if (typeid(New).name() == "TMyEditor_Generation_02 *")
      New_Editor->Parent = Tab_Sheet_Other_Editor;

typeid (__). name() , , "*".

. . TReader, Delphi, , , .

[* 1] . ComponentToString StringToComponent delpi/++.

[* 2] . , , , , __published . , , , , / / _property. , .

0

All Articles