Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reloading a form as designed by using code 1

Status
Not open for further replies.

D3lphiN00b

Technical User
Jul 1, 2012
3
DE
Hey There,

I would appreciate help or advice. Please try to keep it as simple as possible, as I am not above average experienced with delphi. I'm using delphi 7 Professional and I would like to reload a form that I'm closing in the way I designed it, meaning clearing all the checkboxes etc to their designed state without having to type everything out. I have three forms that needs to be 'reloaded' every time one of the three in question is closed.

I am unable to find my answer via google for the past two hours.

Thank you in advance.
 
Are you wanting to do this when you're running your application? For example, the user opens form1 and fills in some information and then closes the form. They then open form1 again but all of the checkboxes and information are still showing from the first time they used the form - but you wanted to have the form to not show what the user typed the first time. Is that correct?
 
That is correct sir.

This is my scenario.
I'm writing a simple database program for the church to add members into the dbase. The user clicks on bkAdd which sets frmAddMember to Visible. This form contains a number of edits and checkboxes and bkNext. BorderStyle is single thus the user can close frmAddMember or click bkNext to continue to frmAddMember2 which has the same properties as frmAddMember, the last form frmAddMember3 has the same properties except for the buttons btnCancel and btnSave. All 3 forms have an OnClose procedure that sets the frmMainScreen to visible.

Now what I'm trying to do is the following:
If the user closes any of the frmAddMembers, I want it to clear all data back to their default state on all the forms. I would like to do the same after the user has clicked on btnSave, after my code has assigned the entered data to my record base.

Hope my explanation makes sense
 
Yes. Here is what I do:
Code:
procedure TForm1.Button1Click(Sender: TButton);
var
  fAddMember: TfrmAddMember;
begin
  try try
    fAddMember := TfrmAddMember.Create(Application);
    fAddMember.ShowModal;
  Except
    on e:exception do begin
      showmessage('Exception:'+e.message);
    end;
  end;
  finally
    fAddMember.Free;
  end;

What you end up doing is creating a new instance of the form which will start up just as you saved it when you designed it.
 
I can't think of a quick and dirty way to do what you ask. Recreating the form from scratch is going to mean taking a performance hit and possibly annoying flicker. Your best bet would be to write a procedure in the form that resets all the control's values to their desired state. That option will also allow for the possibility of wanting a different reset value for a control than the initial value on down the line.
 
Thank you very much Djangman. May I continue in asking for a bit of a breakdown of the above code?

I see you are declaring a variable of type TfrmAddMember. Could you please explain your code? Sorry to be a bother, but I believe in understanding the code before implementing it, so that I actually learn the technique in stead of just have myself spoonfed.
 
When you create a form in Delphi the form's unit automatically creates a variable whose type is that form. It will auto automatically create an instance of that form unless you tell the program not to (Project, Options, Forms).

Code:
fAddMember := TfrmAddMember.Create(Application);
This creates a new instance of TfrmAddMember and makes that form's owner the Application.

Code:
fAddMember.ShowModal;
This makes that form appear to the user. Then the code within the form works as normal. The user fills out the form, clicks some sort of 'Save' button and the information is posted back to the database. When the user clicks Close the form will be closed and then:

Code:
fAddMember.Free;
This will destroy the form.

Search the web for dynamically created form and you'll get lots of examples.
 
one small remark here:

Djangman you got the exception handling wrong.
In your code sample the outer try finally has no sense since it will alwayes execute since you swallow the exception in the inner block, the correct order should be:

Code:
procedure test;

var obj : TObject;

begin
 Obj := TObject.Create;
 try
  try
   DoSomethingWithObject(Obj);
  finally
   Obj.Free;
  end
 except
  on E: Exception do
   HandleTheException(E);
 end;
end;

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thank you, Daddy. Was coding from the hip, so to speak.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top