Remove and Replace a visual component at runtime

前端 未结 3 1146
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 21:17

Is it possible to, for instance, replace and free a TEdit with a subclassed component instantiated (conditionally) at runtime? If so, how and when it should be done? I\'ve t

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-31 21:32

    You have to call RemoveControl of the TEdit's parent to remove the control. Use InsertControl to add the new control.

    var Edit2: TEdit;
    begin
      Edit2 := TEdit.Create(self);
      Edit2.Left := Edit1.Left;
      Edit2.Top := Edit2.Top;
      Edit1.Parent.Insertcontrol(Edit2);
      TWinControl(Edit1.parent).RemoveControl(Edit1);
      Edit1.Free;
    end;
    

    Replace TEdit.Create to the class you want to use, and copy all properties you need like I did with Left and Top.

提交回复
热议问题