Thanks Mr for all that explain but I have problem to aplly this instruction in Lazarus: dynButton.OnClick:=dynButtonClick; can someboy help us the error is: Compile Project, Target: project1.exe: Exit code 1, Errors: 2 uhome.pas(52,22) Error: Wrong number of parameters specified for call to "dynButtonClick" uhome.pas(19,17) Error: Found declaration: dynButtonClick(TObject);
Two issues: #1 Correction of your definition of "Self": "Self" does NOT mean that the instiantiated object is "an owner of itself". "Self" is a pseudo-variable available in all object methods of non-abstract class types and refers to the current instance of the class. Let's take this form class for example: type TForm1 = class(TForm) private function GetCurrentInstance: TForm1; end; Here, you have a custom class type declared, similar as it is done in all form applications created with Delphi by default. Here, we have a custom "GetCurrentInstance" object method which is supposed to return a reference to the current object instance of the "TForm1" class we are declaring. Now, to get the current class instance, we can simply use the pseudo-variable "Self" which is available without an actual declaration (like the "Result" variable is in functions). function TForm1.GetCurrentInstance: TForm1; begin Result := Self; end; When we now create an instance of "TForm1" somewhere and add a Button on it, we can use the return value of the "GetCurrentInstance" as the owner and get the exact same value as if we were using the instance of "Form1" there: var Form1: TForm1; Button1: TButton; begin Form1 := TForm1.Create(Application); Button1 := TButton.Create(Form1.GetCurrentInstance); //Is equal to: Button1 := TButton.Create(Form1); end; In the "OnCreate" event method, we can use the "GetCurrentInstance" of course as a replacement for "Self" (obviously, as the return value of "GetCurrentInstance" is defined with "Self"). procedure TForm1.FormCreate(Sender: TObject); var Button1: TButton; begin Button1 := TButton.Create(Self); //Is equal to: Button1 := TButton.Create(GetCurrentInstance); end; #2 Instantiation and freeing of class instances Even though I did it here in the examples above, you should NEVER EVER create a class instance anywhere out of a local variable which you do not manually free in the end of the method. If you want to create components dynamically, you should use a TObjectList to store them and automatically free them because once the method has finished, the reference to the class instance in memory is marked as "unused" and the memory can be re-allocated again. This could lead to access violations and other unppleasent behaviour. The "better" and "safer" way of creating components at runtime should be: type TForm1 = class(TForm) procedure TForm1.FormCreate(Sender: TObject); procedure TForm1.FormDestroy(Sender: TObject); private procedure TForm1.AddButton; Buttons: TObjectList; end; procedure TForm1.FormCreate(Sender: TObject); begin //Without parameter, the "OwnsObjects" of //an object list is set to true. Then, the list itself //takes care of free-ing the containing objects. Buttons := TObjectList.Create; end; procedure TForm1.FormDestroy(Sender: TObject); begin //As "OwnsObjects" is set to true (default), //all Buttons in this object list will be automatically //free-ed as well. Buttons.Free; end; //This sample procedure adds a new button to the form procedure TForm1.AddButton; begin //Setting of owner here not necessary as the "Buttons" //object list owns the buttons itself. So, it can be "nil". Buttons.Add(TButton.Create(nil)); //To display the button on the form you need to set the //"Parent" property. Buttons.Last.Parent := Self; //Position on the form. Buttons.Last.Left := 50; Buttons.Last.Top := 150; end;
I have a table with products.
there is a way to associate a record of the table to each button created?
then a button a product
whatsapp invite link revoked...
Thanks Mr for all that explain
but I have problem to aplly this instruction in Lazarus:
dynButton.OnClick:=dynButtonClick;
can someboy help us
the error is:
Compile Project, Target: project1.exe: Exit code 1, Errors: 2
uhome.pas(52,22) Error: Wrong number of parameters specified for call to "dynButtonClick"
uhome.pas(19,17) Error: Found declaration: dynButtonClick(TObject);
Two issues:
#1 Correction of your definition of "Self":
"Self" does NOT mean that the instiantiated object is "an owner of itself".
"Self" is a pseudo-variable available in all object methods of non-abstract class types and refers to the current instance of the class. Let's take this form class for example:
type
TForm1 = class(TForm)
private
function GetCurrentInstance: TForm1;
end;
Here, you have a custom class type declared, similar as it is done in all form applications created with Delphi by default. Here, we have a custom "GetCurrentInstance" object method which is supposed to return a reference to the current object instance of the "TForm1" class we are declaring. Now, to get the current class instance, we can simply use the pseudo-variable "Self" which is available without an actual declaration (like the "Result" variable is in functions).
function TForm1.GetCurrentInstance: TForm1;
begin
Result := Self;
end;
When we now create an instance of "TForm1" somewhere and add a Button on it, we can use the return value of the "GetCurrentInstance" as the owner and get the exact same value as if we were using the instance of "Form1" there:
var
Form1: TForm1;
Button1: TButton;
begin
Form1 := TForm1.Create(Application);
Button1 := TButton.Create(Form1.GetCurrentInstance);
//Is equal to:
Button1 := TButton.Create(Form1);
end;
In the "OnCreate" event method, we can use the "GetCurrentInstance" of course as a replacement for "Self" (obviously, as the return value of "GetCurrentInstance" is defined with "Self").
procedure TForm1.FormCreate(Sender: TObject);
var
Button1: TButton;
begin
Button1 := TButton.Create(Self);
//Is equal to:
Button1 := TButton.Create(GetCurrentInstance);
end;
#2 Instantiation and freeing of class instances
Even though I did it here in the examples above, you should NEVER EVER create a class instance anywhere out of a local variable which you do not manually free in the end of the method. If you want to create components dynamically, you should use a TObjectList to store them and automatically free them because once the method has finished, the reference to the class instance in memory is marked as "unused" and the memory can be re-allocated again. This could lead to access violations and other unppleasent behaviour. The "better" and "safer" way of creating components at runtime should be:
type
TForm1 = class(TForm)
procedure TForm1.FormCreate(Sender: TObject);
procedure TForm1.FormDestroy(Sender: TObject);
private
procedure TForm1.AddButton;
Buttons: TObjectList;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//Without parameter, the "OwnsObjects" of
//an object list is set to true. Then, the list itself
//takes care of free-ing the containing objects.
Buttons := TObjectList.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
//As "OwnsObjects" is set to true (default),
//all Buttons in this object list will be automatically
//free-ed as well.
Buttons.Free;
end;
//This sample procedure adds a new button to the form
procedure TForm1.AddButton;
begin
//Setting of owner here not necessary as the "Buttons"
//object list owns the buttons itself. So, it can be "nil".
Buttons.Add(TButton.Create(nil));
//To display the button on the form you need to set the
//"Parent" property.
Buttons.Last.Parent := Self;
//Position on the form.
Buttons.Last.Left := 50;
Buttons.Last.Top := 150;
end;
How can I make a dynamic shape move the same way as a normal shape? e.g if Tshp1.height > Tshp2 then Tshp1.height := Tshp.height -2
Thanks Dude, helped alot
thx, I always forget to set Parent...
Setting parent is pretty important if you want it to show
Thank you...
🎯 Key Takeaways for quick navigation:
Made with HARPA AI
Afrikaner haha
Jip :)
was meinst du damit?