GPFs using multiple forms

Posted on 1998-07-02
Last Modified: 2010-04-04
I am having a problem that is related to component writing and I was hoping someone here could help me.

(Note:The program is a Delphi 1 program.  I am developing on a Win 95 machine.)

I have written a Delphi program with two Forms - the main form is Form1 and the second Form, ibpDlg, is called modally as in "rslt := ibpDlg.ShowModal"

I have both forms set to auto-create in the project file. I have also added some properties per Stewart McSporran's article "RAD Vs. Good Object-Oriented Practice" published in the July 1997 issue of Delphi Developer.

When I run the program, the first time I try to reference the ibpDlg form the program GPFs on me.  So I have played around with it and if I add the following statement to the TForm1.CreateFrom event handler:
ibpDlg := TibpDlg.Create(Self);
the modal form appears to work correctly BUT when I go to close the program with "Form1.close;" the program aborts with "Invalid Operation - can not change Visible on OnShow or OnHide".  I have NO idea where this is coming from - I am guessing that I am doing something so wrong the program is getting "lost" and throwing up a screw-ball error.
Anybody have any ideas?  Also and AS important, where can I find out how to encapsulate a Form into a component that I can place on the component taskbar and drop onto my Form?  The ibpDlg Form is a generic Dialog box that replaces ShowMessage and that ilk.  I have read Developing Custom Delphi Components by Ray Konopka but couldn't find an example of how to encapsulate a Form (the about box example looked close but I want a From to be able to have TStringList components (a memo) in addition to non-created variables like Integer and String.)

I hope this makes some sense and there is someone out there who can help.


Joe Donth

Question by:donth
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 1357003
Adjusted points to 150

Expert Comment

ID: 1357004
Ok, can show us some relevant code? And where exactly do you call form1.close?

Expert Comment

ID: 1357005
When you call ibpDlg.ShowModal GPF appears becouse ibpDlg not created yet.

But I cannot repeat your problem. Please write here your program code.

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1357006
Go to View | project source...look in there for lines of code like
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TibpDlg, ibpDlg);

no let me ask you this are you doing the showmodal of the ibpDlg form in the OnCreate event of the Form1...if so you will notice that the OnCreate of form1 is before the oncreate of the ibpDlg
you would have to do something like this in the OnCreate to make it work... (Delete the Application.CreateForm(TibpDlg, ibpDlg);
from the project source)

Application.CreateForm(TibpDlg, ibpDlg);

Call the Release method of the form instead of the Free...but you probably knew that.

can you post the code for the OnClose of Form1...your not setting the Visible to False or something crazy like that are you?...also look at your OnDestroy if you have one.

As for the Form thing do you mean a Form File | New -> template?


Author Comment

ID: 1357007
I have posted a zip file for download at: [21792 bytes]

It contains the following files:
Mkopy.dpr & MKopy.res
Unit1.dcu, Unit1.dfm & unit1.pas;
udialog.dcu, udialog.dfm & udialog.pas

I would appreciate that you would consider this as protected as an unpublished work - and that you would also be kind to the code as it is early in the development cycle and I never intended for anyone to see it in the shape it is in.  With that out of the way...

The place that the program GPFs with the ibpDlg := TibpDlg.Create(self)  (see line 5 after begin of Form1.FormCreate) is the 3rd line from the end of the TForm1.FormActivate event handler where it says "if AskForFile = true then Form1.close;

Note: I have commented out a unit called BinCopy1 used in the DoProcess routine to eliminate any other outside interactions.

I am increasing the points to 250.
LVL 10

Accepted Solution

Jacco earned 250 total points
ID: 1357008
The problem is that you can't close the form from within the OnActivate event. (The VCL is still in the process of fully showinbg your form. The problem is that your program runs without any user interaction. I have written forms like that, but had the same problem. I solved it by setting a timer to say one second and at the Timer.OnTimer event closed the form...

To encapsulate a form you can evaluate the source code of the VCL (although I am not sure it is given with Delphi 1) Look for the code some of the Dialogs (on the Dialogs page of the component palette). If you do not have the code I am willing to E-mail the code to you. It is not so hard to do it:

  TfrmDialog = class(TForm)

  TMyDialogComp = class(TComponent)
    FDialog : TfrmDialog;
    procedure Execute;


// form code

// component code

procedure TMyDialogComp.Execute;
    with FDialog do begin

This is called making a wrapper component. It wraps around your dialog.

Hope I helped you a bit.

Regards Jacco.

Expert Comment

ID: 1357009
I have another solution

Now when you create form1
it become visible before ipbDlg create and
even before setting Application.MainForm

Just set in desing time Form1.Visible = false and all.

After that at first form1 and ipbDlg created then form1 become visible.


Author Comment

ID: 1357010
Thank you! I changed the routine and it works!

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question