Recent

Author Topic: Instance Component of Class  (Read 595 times)

J-23

  • Full Member
  • ***
  • Posts: 108
Instance Component of Class
« on: May 30, 2020, 08:49:54 pm »
Hello,

I am writing my own component that will work with the Lazarus component palette.

I need to create a component instance based on a class but something goes wrong:

Code: Pascal  [Select][+][-]
  1. function TSpaceDesingerPaletteEditorForm.GetComponentForNameRegisteredComponent(
  2.   const ComponentName: string): TRegisteredComponent;
  3. var
  4.   i: integer;
  5.   Component: TRegisteredComponent;
  6. begin
  7.   for i := 0 to IDEComponentPalette.Comps.Count - 1 do
  8.   begin
  9.     Component := IDEComponentPalette.Comps[i];
  10.     if (Component.OrigPageName <> '') and
  11.       (Component.ComponentClass.ClassName = ComponentName) then
  12.     begin
  13.       if Assigned(Component) and Component.Visible then
  14.       begin
  15.         Result := Component;
  16.         Break;
  17.       end;
  18.     end;
  19.   end;
  20. end;                                                    
  21.  

This function is fine returns the selected component.

And I'm trying to create a component instance like this:

Code: Pascal  [Select][+][-]
  1. procedure TSpaceDesingerPaletteEditorForm.Button1Click(Sender: TObject);
  2. var
  3.   Component: TRegisteredComponent;
  4.   C: TComponent;
  5. begin
  6.   with ListBoxPalette do
  7.   begin
  8.     Component := GetComponentForNameRegisteredComponent('TButton');
  9.     C := TComponent(Component.newinstance).Create(Self);
  10.     ShowMessage(C.ClassName);
  11.   end;
  12. end;                                      
  13.  

After compiling and installing the component after opening its editor. I get an error here:

Code: Pascal  [Select][+][-]
  1. ShowMessage(C.ClassName); //this error
  2.  




Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Instance Component of Class
« Reply #1 on: May 30, 2020, 10:12:35 pm »
Code: Pascal  [Select][+][-]
  1. C := TComponent(Component.newinstance).Create(Self);
TObject.Create constructor automatically calls TObject.NewInstance, thus you are calling it twice here. Please remove call to Component.newinstance and use Component.Create.

Although it seems that you do not have need for creating a class instance, so you can call Component.ClassName (it is a class function method) directly.


J-23

  • Full Member
  • ***
  • Posts: 108
Re: Instance Component of Class
« Reply #2 on: May 30, 2020, 11:20:00 pm »
Code: Pascal  [Select][+][-]
  1. C := TComponent(Component.newinstance).Create(Self);
TObject.Create constructor automatically calls TObject.NewInstance, thus you are calling it twice here. Please remove call to Component.newinstance and use Component.Create.

Although it seems that you do not have need for creating a class instance, so you can call Component.ClassName (it is a class function method) directly.

Thanks for the help.

this code seems to work:
Code: Pascal  [Select][+][-]
  1. procedure TSpaceDesingerPaletteEditorForm.Button1Click(Sender: TObject);
  2. var
  3.   Component: TRegisteredComponent;
  4.   C: TComponent;
  5.   ccr: TComponentClass;
  6. begin
  7.   with ListBoxPalette do
  8.   begin
  9.     Component := GetComponentForNameRegisteredComponent('TButton');
  10.    ccr := Component.GetCreationClass;
  11.     C := TComponent(ccr.newinstance);
  12.   end;
  13. end;                                                                      
  14.  
Tomorrow, more experiments

 

TinyPortal © 2005-2018