user1405338
user1405338

Reputation: 189

How to change the Form1 to FormMain in Delphi

I'm starting with Delphi and I just find annoying that I get default varuables of Form1. I want to give it other name like FormMain or something like that. So let's see my code:

unit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TForm9 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form9: TForm9;

implementation

{$R *.dfm}

end.

Code above works fine when I compile. Now I change next code to:

unit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TFormMain = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form9: TFormMain;

implementation

{$R *.dfm}

end.

And in other file .dpr I also change it to:

program mainProject;

uses
  Vcl.Forms,
  main in 'main.pas' {FormMain};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TFormMain, FormMain);
  Application.Run;
end.

And this gives me errors, what ever I change. I just want to change default form name.

Upvotes: 2

Views: 617

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595897

The correct way to rename a Form is to use the Object Inspector at design-time. Simply change the Form's Name property to the desired value (ie, change 'Form9' to 'FormMain'), and then the IDE will rename the Form's class type for you, as well as its associated global variable and DFM resource.

In your case, you did not rename the global Form9 variable to FormMain to match what the DPR is looking for in the call to Application.CreateForm(). And I'm guessing you did not update the DFM resource, either.

Upvotes: 9

Related Questions