6

I have a form called MyForm stored in a unit called Unit UnitMyFrom. Of course Delphi automatically added this code:

TYPE
  TMyForm = class(TForm)
  private
  public
 end;

var MyForm: TMyForm;

but I removed the var declaration from that unit. Somebody said that this may cause problems with the IDE. Is it true? Does the IDE require that variable?


EDIT:

The MyForm IS NOT automatically created. The user creates that form at runtime.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Gabriel
  • 20,797
  • 27
  • 159
  • 293

6 Answers6

13

I don't use auto-create and I never use global variables containing form references. It works just fine.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks for confirmation. I do the same. +1 – Gabriel Feb 01 '11 at 13:49
  • 1
    +1. I wish there was an option to **not** add new forms to the auto-create list automatically. I wish there was an option to **not** create that troublesome "var". Oh well... – Cosmin Prund Feb 01 '11 at 14:23
  • @Cosmin There is an option. It's called "Auto create forms & data modules". It just doesn't seem to have any effect!! Obviously I'm not doing it right, or misunderstanding the documentation. – David Heffernan Feb 01 '11 at 14:34
  • the option you refer to prevents the form from being auto created but does not remove the global variable for the form. – X-Ray Feb 01 '11 at 20:12
  • How do you create the main form then and make it behave as such? Calling `Application.Terminate` in the `OnClose` event? What about the taskbar icon? Other things? – Thijs van Dien Aug 01 '15 at 23:33
  • There's nothing much to do. Call Application.CreateForm just once, for the main form. When it closes, so does the app. – David Heffernan Aug 02 '15 at 04:46
7

Remove the line Application.CreateForm(TMyForm, MyForm); in project .dpr file and you wont have any problems.

If you dont create the form automatically, then you wont have any problems

Bharat
  • 6,828
  • 5
  • 35
  • 56
3

I would say yes when you want to create form automatically, because you need to pass this variable to the TApplication.CreateForm method.

  • 3
    Or more precisly, that's what the _IDE_ wants to do when you add a form to the autocreated forms. If you want to do it yourself, you can put the variable anywhere you like. You shouldn't overuse autocreation anyway. – Paul-Jan Feb 01 '11 at 13:45
3

Go to Project Options, and make sure the form is not automatically created. Then you can safely remove the form instance variable. (Otherwise, you'll just get a compiler error.)

Frederik Slijkerman
  • 6,471
  • 28
  • 39
2

You may want to maintain this line in the uses or contains clause in the dpr-file:

  Unit1 in 'Unit1.pas' {Form1};

If you (or the IDE) deletes the {Form1}-comment, then your form will not appear in the Shift+F12 list - only in the Ctrl+F12-list.

Jørn E. Angeltveit
  • 3,029
  • 3
  • 22
  • 53
  • +1; I have seen so many projects screw this up (because people think they own the .dpr; you don't: the IDE owns the DPR, you own the units. People stick all their special code and conventions in a "MainUnit"). – Jeroen Wiert Pluimers Feb 01 '11 at 14:49
  • 4
    @Jeroen Well, I disagree. The IDE screws it up. I want conditional inclusion of units in my project (e.g. switch between different memory managers, support different versions of Delphi). IDE screws this up. I need better control over form creation and destruction than IDE made dpr allows, and also need to work around some VCL bugs relating to that. – David Heffernan Feb 01 '11 at 15:45
  • Yes, I still have that line in the DPR file. – Gabriel Feb 01 '11 at 18:04
  • @David it is very easy to defer that decision to another unit and still keep the Delphi IDE happy. You can hate it that the IDE owns the .dpr, but it has been a fact for over 15 years, so by now you should have learned to live with it. – Jeroen Wiert Pluimers Feb 01 '11 at 18:47
  • @jeroen are you familiar with installing custom memory managers? – David Heffernan Feb 01 '11 at 19:26
  • @david Yes, the method above is exactly how I choose if I want to use FastMM or not. [This answer](http://stackoverflow.com/questions/4463979/my-program-never-releases-the-memory-back-why/4464907#4464907) explains how. It is easy to extend to other memory managers. Note that back in the 16-bit days I wrote Korsakov to track memory leaks. I need to dig that up and publish somewhere. – Jeroen Wiert Pluimers Feb 01 '11 at 19:40
  • @Jeroen I'm choosing between multiple memory managers. I only want 1 of them to be referenced by my .dpr file. Thus I need conditional compilation in my .dpr uses. The IDE can't handle this. I really don't mind too much because I write all the rest of the code myself, handling the .dpr file is trivial. It would be nicer if the IDE would understand better, but since it doesn't, I just don't let it interfere. – David Heffernan Feb 01 '11 at 22:48
  • @David: you can do that the way I do it. I designed the way to do that. Worst case (when you use the default memory manager) is that you get an empty "MemoryManagerBootsTrapUnit" unit in your project (because that unit does not contain code, as all of the conditional compilation paths are excluded). Drop me an example project by email, and I'll convert it for you. If you want to, I can write a nice blog post on it. – Jeroen Wiert Pluimers Feb 01 '11 at 22:58
  • @Jeroen Actually I like my way just fine. I don't want the other ones compiled in to my project. I can't see what problem you are solving. But blog about it and I'll try to haul in the detail. – David Heffernan Feb 01 '11 at 23:05
1

i remove them & have worked that way for quite a while (i don't autocreate or use the variable for most forms).

X-Ray
  • 2,816
  • 1
  • 37
  • 66