#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Handling runtime objects


    Iím trying to write a program in Delphi in which you can type some text in an edit box, then press the button next to it to make a memobox containing the text you just typed and a deletebutton next to it. The delete button should delete both itself and the memobox next to it. This means that both the memo box and the delete button will be made in runtime. The problem I have is finding a way to handle each button individually. Here is the code I already have:

    procedure TForm1.btn_createClick(Sender: TObject);
    var
    text: string;
    n: real;
    i:integer;
    NewMemo : TMemo;
    NewButton : TButton;

    namearray:Array of string;

    begin

    n:=self.ControlCount*0.5;

    text:=edt_memo.Text;

    NewMemo:=TMemo.Create(self);
    NewButton:=TButton.Create(self);

    with NewMemo do
    begin
    Height :=24;
    Left:= 8;
    Width := 120;
    Top:= 55 + 12*(self.ControlCount-2);
    Parent:=self;
    NewMemo.Name:='mem_'+floattostr(n);
    end;

    NewMemo.Text:=text;

    with NewButton do
    begin
    Height:= NewMemo.Height;
    Left:=NewMemo.Left+NewMemo.Width;
    Top:=Newmemo.Top;
    Width:=50;
    Parent:=self;
    NewButton.Caption:=floattostr(n);
    NewButton.Name:='btn_'+floattostr(n);

    OnClick:=CustomButtonClick;

    end;

    end;

    procedure TForm1.CustomButtonclick(Sender: TObject);

    begin
    if Self.ControlCount>2 then
    TButton (controls[ControlCount-1]).destroy;
    TButton (controls[Controlcount-1]).destroy;
    end;


    end.

    Right now, I have only gotten the delete button to delete the last two items that were created, but I would like the button to only delete itself and the memo next to it (preferably by checking the components name, which I canít seem to get working). Any help would be greatly appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    Seems like a very poor design approach. Is there a reason you do not use either a messageDlg or simply create and free a custom form?
    If you have to use this approach, I suggest you move the memo and button variables to the private section of the form and simply create and free them as needed (I am assuming you do not create more than one instance at a time).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0
    I'm very new to delphi, so I wasn't sure if this method is possible or not.

    There is only one instance running at a time. I will look into the messageDlg method. Thank you for your help.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    55
    Rep Power
    7
    NEVER use this:

    1 - NEVER use "DESTROY" directly. Use Free!

    2 - NEVER use "destroying" one component into its EVENT HANDLERS

    3 - When make a component in runtime, you can to use CREATE(Owner) (if possible) with param :

    xxx:= TButton.Create( "Self" or "TForm" or another container );

    So, the "OWNER" (the param) will be who delete the new component when the OWNER is deleted.

    See the help in Delphi for parameters in CREATE() function.
    See: SELF, NIL, (another component)

    Container, are that as: Forms, Frames, DataModules, Panels, etc...

IMN logo majestic logo threadwatch logo seochat tools logo