Delphi Programming Series: 48.0 - Dynamically creating a Button component

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ม.ค. 2025

ความคิดเห็น • 13

  • @danielenapoli7845
    @danielenapoli7845 2 ปีที่แล้ว

    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

  • @ahwee321
    @ahwee321 5 ปีที่แล้ว +1

    whatsapp invite link revoked...

  • @makhloufloucif7020
    @makhloufloucif7020 2 ปีที่แล้ว

    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);

  • @MQuaritch
    @MQuaritch 8 ปีที่แล้ว +3

    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;

  • @BeaniemanGaming
    @BeaniemanGaming 6 ปีที่แล้ว

    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

  • @andrewvanolst4599
    @andrewvanolst4599 6 ปีที่แล้ว

    Thanks Dude, helped alot

  • @fxy6194
    @fxy6194 4 ปีที่แล้ว +1

    thx, I always forget to set Parent...

    • @ShaunRoselt
      @ShaunRoselt  4 ปีที่แล้ว

      Setting parent is pretty important if you want it to show

  • @jamesdastan5278
    @jamesdastan5278 6 ปีที่แล้ว

    Thank you...

  • @cr6877
    @cr6877 ปีที่แล้ว

    🎯 Key Takeaways for quick navigation:
    Made with HARPA AI

  • @heinrich_burgers
    @heinrich_burgers 7 ปีที่แล้ว +4

    Afrikaner haha