Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open and close vcl form

Tags:

forms

delphi

Right now i have 2 forms. On Form1 i open up Form2 like this:

procedure TForm1.Action1Execute(Sender: TObject);
var
  Form2: TForm2;
begin
  Form2 := TForm2.Create(Form2);
  Form2.ShowModal;
  Form2.Free;
end;

Now i want to close the Form2 with a button on it. So i tried

procedure TForm2.cancelBtnClick(Sender: TObject);
begin
  Form2.Close;
end;`

But that only gives me Access violation error when i click that button. What i am doing wrong?

like image 374
hs2d Avatar asked May 07 '11 20:05

hs2d


People also ask

What is VCL form?

The Visual Component Library (VCL) is a visual component-based object-oriented framework for developing the user interface of Microsoft Windows applications. It is written in Object Pascal.

How do I open another form in Delphi?

Start a new project, and add one additional form (Delphi IDE Main menu: File -> New -> Form). This new form will have a 'Form2' name. Next add a TButton (Name: 'Button1') to the main form (Form1), double-click the new button and enter the following code: procedure TForm1.


2 Answers

The normal way to do this is to do

procedure TForm1.Action1Execute(Sender: TObject);
begin
  with TForm2.Create(nil) do
    try
      ShowModal;
    finally
      Free;
    end;
end;

and, if TForm2 contains a OK button, this should have the ModalResult property set to mrOK at design-time. Use the object inspector to set this. You probably also want to set Default to True. Now you can 'click' the OK button by pressing the Enter key on your keyboard!

In addition, if there is a Cancel button in the dialog, this should have ModalResult set to mrCancel and Cancel set to True. Now you can 'click' the Cancel button by pressing the Escape key on your keyboard!

A button with a ModalResult value will automatically close a modal dialog box.

like image 85
Andreas Rejbrand Avatar answered Oct 29 '22 03:10

Andreas Rejbrand


Since the form is showing modally the correct solution is to set ModalResult := mrCancel in your button click handler. The shortcut is to set the ModalResult property of the button to mrCancel and then you don't even need the event handler.


Note that your form is being created incorrectly. You are passing the unassigned variable Form2 as the Owner parameter to the constructor. I expect this is the cause of the access violation.

You should pass another form, Application or nil, for example. In fact in this case you may as well pass nil so that the code should read:

Form2 := TForm2.Create(nil);
try
  Form2.ShowModal;
finally
  Form2.Free;
end;

If you pass an owner then the form will be destroyed when the owner is destroyed. Since you are destroying it yourself, you don't need to pass an owner.

That said, it is sometimes useful to set the owner, for example if you are using one of the Position property values that sets the form's position based on the owner's position. If so then I recommend passing Self in this instance which is a TForm1 object reference.

like image 22
David Heffernan Avatar answered Oct 29 '22 02:10

David Heffernan