How do I import Units into a Delphi Project?


I would like to use some current codes from an existing project to create a new DLL for future use. How can I import these current codes (in Units) into my DLL project?

I have tried using Project -> Add to Project, and selecting the Units that I want, but upon compilation, error messages were returned specifying that the .DFM files cannot be found. Is it necessary to have .DFM files when importing Units into a DLL project?

Your assistance will be much appreciated. Thanks.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dagan HooverDeveloperCommented:
If your units are using forms then you need to have both the dfm file and the pas file.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Geert GOracle dbaCommented:
it looks like you will have common code between at least 2 projects

most of the time you start with common + project code in the same directory
you need to think about any other future projects too
and should try and mark the common units (.pas + .dfm) and place them in a separate directory

-- common code
-- project 1 code
-- project 2 code
-- project 3 code
-- big project 1
----subprojects specific common code
----subproject 1 code
----subproject 2 code

it will make your life a little harder to put the common units in the separate directories first
but for future projects it will make your life a lot easier

be aware that it takes some thinking to write common code
this common code should not need any units from the separate projects

you should be able to create a new project and use any units from the common code
without have to change anything to it for it to work in a new project
Look at the code, see what component was using. (The UNIT codes that you want to join with the others)

  TForm1 = class(TForm)
///this are the component used
//begin components
    Button1: TButton;
    Label1: TLabel;
    Memo1: TMemo;
    ListBox1: TListBox;
//end components
    { Private declarations }
    { Public declarations }

//So, 1button, 1label, 1memo, 1listbox

Open your Delphi
Add new form
Add that components to the form
Go back to the unit?.pas
Delete all the lines of unit?.pas
Paste the Unit codes that you want to use
Save it, without running if it works.
Close Delphi
Edit ?.DFM file (the one that youve saved.
Looks like this:

//////change Form1 or Form2 to the one that is specified in the UNIT code that you want to use
/////and TForm1 or TForm2 also change it to the UNIT code that you want to use
/////Add the object name to the ?.DPR
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 264
    Top = 88
    Width = 31
    Height = 13
    Caption = 'Label1'
  object Button1: TButton
    Left = 264
    Top = 40
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
  object Memo1: TMemo
    Left = 256
    Top = 128
    Width = 185
    Height = 89
    Lines.Strings = (
    TabOrder = 1
  object ListBox1: TListBox
    Left = 256
    Top = 232
    Width = 121
    Height = 97
    ItemHeight = 13
    TabOrder = 2

But it the component that was use in the UNIT was not there, then you must search it before running the whole code.

Hope that helps.
redsgAuthor Commented:
Upon further inspection of the codes (I had actually just inherited them, so had not taken a good long look at them yet), I understand what TheivingSix and systan are talking about i.e. to ensure that the DFM files are included where the form components are being used. Will make the necessary inclusions while importing the Units.

Understand the common code would be very useful for future projects, will keep it in mind.

Thanks guys!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.