I have two forms. Each form has a button to jump to another form.

How to make it so that when switching to another form, the latter nullifies all its variables and objects on it, in other words, it is updated. I wrote this code.

procedure TForm1.Panel8Click(Sender: TObject); begin Form1.Hide; Application.CreateForm(TForm2, Form2); Form2.Show; end; 

The idea is that when you click on a button, Form2 should be created anew, but for some reason, the variables on it, if they were changed before, retain their values.

    1 answer 1

    Delete the existing Form2 object before creating a new one.

     Form2.Free; Application.CreateForm(TForm2, Form2); 

    Uh-uh ... Please show me an example of a "variable on it."

    • I meant the variables that change in the unit attached to this form, sorry for the inaccuracy. - JohnS
    • @JohnS - That is, global variables? They will not take the original values ​​when creating a new instance of the form. - Igor
    • one
      @JohnS - Make them members of the TForm2 class and the problem will be solved. - Igor
    • one
      @JohnS - or assign them the initial values ​​in the TForm2 constructor. - Igor
    • Thank you for your answers, but could you explain in more detail how to make variables members of a class. There is an implementation string and probably {TForm2} means a class, then I need to create a var after this line with my variables? - JohnS