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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$IFDEF

Status
Not open for further replies.

JDSmith14

Programmer
Nov 11, 2005
11
ZA
Hi

I am trying to do the following in the uses clause of a form:

Uses
{$IFDEF X}
FormX,
{$ENDIF}

{$IFDEF Y}
FormY,
{$ENDIF}

other units.....;

Form X and FormY has the same name in Delphi - Let's say MyForm.

In other words, If I have a button on the main form of the project, I want to say MyForm.DoSomething on the button's onclick event.

Depending on what conditional directive I specify, I want either formX.DoSomething, or FormY.DoSomething to be executed.

I do however get a duplicate rescource compilation error when trying the above. The two forms are not allowed to have the same name.

I was under the impression that if X was not a conitional directive, Delphi would not even compile, or be aware of formX's existance. Is this so, and if not, how can I address this?

Any help would be appreciated.

Johan Smith

 
That was my impression too - make sure that neither form have been added to the project (View menu, Project Manager, expand your project .exe)
 
Well... may be Delphi will not compile the form... but the internal resource compiler is apparently trying to add both resources.

BBBBUUUUUUUUGGGGGGGGG!

Workaround: use an $IFDEF around the {$R *.dfm}.

Code:
The "ifdefable" forms are in the IFFPart1 and IFFPart2 units; both named TFIFPart.

The control directives are F1 and F2.

--------- Main Form ----
uses
  {$IFDEF F1} 
  IFFPart1,
  {$ENDIF}
  {$IFDEF F1}
  IFFPart2,
  {$ENDIF}
  .... 

---------- Part1 Form ----
{$IFDEF F1}
{$R *.dfm}
{$ENDIF}

---------- Part2 Form ----
{$IFDEF F2}
{$R *.dfm}
{$ENDIF}

buho (A).
 
Good thinking buho, but I wouldn't label this a bug just yet. I'm pretty certain that if a form has not been added to the project, it will not have it's resources added (why would it?). Similarly, units that have been added to the project should have it's resources loaded.

Also, if there are only two states that (ie, X is declared xor Y is declared, then consider using the $IFNDEF, or $ELSE as appropriate. That way you eliminate the possibility of both X and Y being declared, or neither X or Y being declared.
 
Both are added. I've checked it. I deleted both forms from the project and it did not compile.

Well... new check... it compiles if you unload Delphi, delete the project RES and let Delphi recreate it on load.

buho (A).
 
Not sure this is going to work.

The project compiled if it was not having any reference to the "ifdefable" forms. As soon I added a

FIFPart := TFIFPart.Create(Self);

the compiler claimed "Undeclared identifier TFIFPart", not matter what method I used (deleting the project RES or "ifdefing" the form resources).

Going to investigate a little more.

buho (A).


 
This worked for me. I have 3 forms. The first form's unit:
Code:
unit Unit1;

{$DEFINE X}

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
{$IFDEF X}
  unit2;
{$ELSE}
  unit3;
{$ENDIF}

procedure TForm1.Button1Click(Sender: TObject);
begin
  FormX := TFormX.Create(nil);
  try
    FormX.ShowModal;
  finally
    FormX.Release;
  end;
end;

end.

Both Unit2 and Unit3 contain exactly the same code:
Code:
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

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

var
  FormX: TFormX;

implementation

{$R *.dfm}

end.

Unit2's DFM file:
Code:
object FormX: TFormX
  Left = 191
  Top = 103
  Width = 696
  Height = 480
  Caption = 'Form888888'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
end
Unit3's DFM file: (note the only difference is the form caption, but an implementation could have a total different form)
Code:
object FormX: TFormX
  Left = 191
  Top = 103
  Width = 696
  Height = 480
  Caption = 'Form134298348'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
end

Change the $DEFINE to either define X or something else or nothing, and the project compiles every time without error, and clicking the button on Form1 properly displays the appropriate form.
 
Got it working with the ifdefing around the form resources. It compiles and runs and shows the form.

And forget what I've said about deleting the project resource, deleting it is useless, my check was flawed.

I'm very tired just now and making stupid mistakes. Will go to bed.

buho (A).
 
Thanks for the help and comments- Highly appreciated

Johan
 
What Delphi version are you using, Griffyn?

buho (A).

 
No matter, Griffyn, it works.

Was very tired last night, made a series of incredible stupid errors.

buho (A).
 
It's funny how easily we fall into the trap of thinking one problem is related to another buho - I saw a post of yours just earlier where you talked about Borland not fixing bugs from D4 that are still in D6, etc.

Sleep cures most problems. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top