I have a form where there are 2 buttons, one which randomly adds buttons onto the form, and one that resets the form to default.

When the form is opened there are two buttons, but after clicking the random button, there are many buttons.

For the reset button, I currently use:

MainForm.free;
Application.CreateForm(TMainForm, MainForm);
Application.Run;

Is there a (better) different way?

Yes... use FindComponent() to find all TButton's except for the two static buttons you want to remain. Free the buttons and remove them from the control collection. If you don't know the name then search through Self.Components on the form and be sure to check the component collection of controls found if they are containers.

Recreating your main form is not recommended. I can't think of any reason why it would cause problems but it just seems like it would have to be a bad idea.

Here is the code. When you create a TObjectList with the aOwnsObject = true then it will free the controls when you free the object list.

procedure TForm1.Button1Click(Sender: TObject);
Var
  btn: Tbutton;
begin
  btn := TButton.Create(Self);
  btn.Left := 50;
  btn.Top := 50;
  btn.Parent := Self;


  btn := TButton.Create(Self);
  btn.Left := 300;
  btn.Top := 300;
  btn.Parent := Self;
end;

{---------------------------------------------}

procedure TForm1.Button2Click(Sender: TObject);
Var
  i1: Integer;
  btn: TButton;
  objList: TObjectList;
begin
  objList := TObjectList.Create(true);
  for i1 := 0 to Self.ControlCount-1 do
  begin
    if not (Self.Controls[i1] is TButton) then
      continue;
      
    btn := (Self.Controls[i1] as TButton);

    if Assigned(btn) and (btn <> Button1) and (btn <> Button2) then
    begin
      objList.Add(btn)
    end;
  end;
  FreeAndNil(objList);
end;

1. How do you create the TObjectList class. Sorry I don't know a lot about classes, learnt it all yesterday.

2. Totally different problem: How can I change the background to any color I want, instead of the defaults? Like a hex color. I tried to define my own constant colour with a value, but it seems to round to the nearest color.

Thanks,

2. Totally different problem: How can I change the background to any color I want, instead of the defaults? Like a hex color. I tried to define my own constant colour with a value, but it seems to round to the nearest color.Thanks,

If you drag and drop a button to the form and double click on it and write these lines then the Form1 background will be green totally.If you want different color then look at the object ispector color option

procedure TForm1.Button1Click(Sender: TObject);
begin
   Form1.Color:=CLGREEN;
end;

Next time one thread one question and if you get the answer then mark as solved

1. How do you create the TObjectList class. Sorry I don't know a lot about classes, learnt it all yesterday.

2. Totally different problem: How can I change the background to any color I want, instead of the defaults? Like a hex color. I tried to define my own constant colour with a value, but it seems to round to the nearest color.

Thanks,

Add "Contnrs" to your using clause.

Flamingclaw answered your second question which should have been a separate thread so please mark this thread as solved if we have answered your question.

This question has already been answered. Start a new discussion instead.