Recent

Author Topic: Reset a Form!!  (Read 10327 times)

sermo!!

  • New Member
  • *
  • Posts: 12
Reset a Form!!
« on: September 15, 2011, 10:54:11 am »
Hi!!

I have a form which a main menu which has different options, and if you push one option of the menu the problem does differents things (create new buttons, labels, paintBoxs...)
If I want to reset the Form how can I do it??? For example you are in the first option and you realice that you want to do the second option, but the second option is not enabled until you finish the first option, so I would like to reset all the buttons, paintBox, labels, etc. of the first option and have the second option enabled, I think like a "reset a form" or something like that, but I don't know how to do it, because I have tried to do form1.free and then create another one and when I try to close it I can't and the program failed.

How could I do it?? Thanks.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Reset a Form!!
« Reply #1 on: September 15, 2011, 10:57:17 am »
Would you show in code, where and how you tried to free and create the form? In theory that's one way you can do it. Other is having all default options saved in variables and if you want to cancel it, copy those values over real ones.

sermo!!

  • New Member
  • *
  • Posts: 12
Re: Reset a Form!!
« Reply #2 on: September 15, 2011, 05:28:31 pm »
I have put:

for i := 0 to ComponentCount - 1 do
if Components[i ] is TCheckBox then TCheckBox(Components[ i]).free;

and the program failed, however if I put for example:
if Components[ i] is TEdit then TEdit(Components[ i]).Text := '';

the program doesn't failed, so I don't know what it's the problem, because I don't want to hide it the checkbox or the labels, I want to erase it

Shebuka

  • Sr. Member
  • ****
  • Posts: 429
Re: Reset a Form!!
« Reply #3 on: September 15, 2011, 05:48:01 pm »
if you free a component it cease to exist, so it's obvious that if you try to use it after it was destroyed your program fail

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Reset a Form!!
« Reply #4 on: September 15, 2011, 06:04:16 pm »
I was thinking something like:
Code: [Select]
form1.Free;
Application.CreateForm(TForm1, Form1);
I didn't test it, maybe this work too:
form1:=TForm1.Create(nil);

And make sure these are last executed commands in procedure you use.

sermo!!

  • New Member
  • *
  • Posts: 12
Re: Reset a Form!!
« Reply #5 on: September 15, 2011, 06:13:17 pm »
Thank you very much "user137", I think the problem was that I put it at the first of the procedure, but now it runs perfectly with this:

form1.Free;
Application.CreateForm(TForm1, Form1);

Thanks again.

arbelest

  • New Member
  • *
  • Posts: 15
Re: Reset a Form!!
« Reply #6 on: September 16, 2011, 08:37:15 am »
I have put:

for i := 0 to ComponentCount - 1 do
if Components[i ] is TCheckBox then TCheckBox(Components[ i]).free;

and the program failed, however if I put for example:
if Components[ i] is TEdit then TEdit(Components[ i]).Text := '';

the program doesn't failed, so I don't know what it's the problem, because I don't want to hide it the checkbox or the labels, I want to erase it

I think i know the problem.
Code: [Select]
  for i := 0 to ComponentCount - 1 do
  if Components[i ] is TCheckBox then TCheckBox(Components[ i]).free;
That code will produce accessviolation, because you using incremental for looping.
Say there is 5 components,  your checkbox at 3' index, so when i --> 3, your checkbox will free AND componentCount also decrease from 5 to 4.
Certainly there is nothing at index 4, so looping will fail.

Fixed:
Code: [Select]
  for i := ComponentCount - 1 downto 0 do
  if Components[i ] is TCheckBox then TCheckBox(Components[ i]).free;

 

TinyPortal © 2005-2018