...

How to Create a Custom Lazarus Component

edit

This guide walks you through creating a new visual Lazarus component, from creating the package to registering the component in the IDE. It also includes best practices for naming units and classes to avoid conflicts.

Contents

edit
  1. Step 1: Create the package
  2. Step 2: Create the unit
    1. Create a new unit
    2. Add an existing unit
  3. Step 3: Create icons for the package
    1. Using the Lazarus Image Editor
    2. LRS Explorer
    3. Using lazres
    4. Using glazres
  4. Recompiling Packages
  5. Removing Packages
  6. Enhancing mycomp.pas
  7. Using embedded (visual) components
  8. Using custom paint procedure
  9. Integrating the component with the IDE
    1. Property editors
    2. Component editors
  10. Design-time component debugging
  11. Adding contents for the Information box
    1. Simple
    2. FPDoc Editor
  12. See also

Step 1: Create the package

edit
  1. In Lazarus, go to Package → New Package.
  2. Choose a directory for the package. The folder name becomes the package name.
 Example: Tutorial folder → package name Tutorial.
  1. Save the package file (.lpk) in the package folder.

Step 2: Create the unit

edit

Each component lives in its own unit. The unit filename must match the unit name declared inside the file.

Create a new unit

edit
  1. In Lazarus, go to File → New Unit.
  2. Save the unit inside the package folder.
  3. The Pascal unit filename should match the component class name:
  • TSimpleLED → SimpleLED.pas (unit name = SimpleLED)
  • TOAISimpleLED → OAISimpleLED.pas (unit name = OAISimpleLED)

Add an existing unit

edit

If you already have a .pas file:

  1. In the Package Editor, click Add → Add Files.
  2. Select your existing unit file.
  3. Make sure the unit name in the file matches the filename (without extension).

⚠ Naming Conflicts and Prefixes

edit

When you create Lazarus components for public release (or even for use across multiple projects), you must avoid naming conflicts.

Lazarus and Free Pascal will not allow two units with the same name to exist in the same project. If your file is called SimpleLED.pas and another package also has a SimpleLED.pas, the compiler won’t know which one to use.

✅ The Solution: Add a Unique Prefix

edit

Use a short, project- or organization-specific prefix in both your unit name and your main component class name.

Element Without Prefix With Prefix (oai)
Unit filename SimpleLED.pas oaiSimpleLED.pas
unit declaration unit SimpleLED; unit oaiSimpleLED;
Component class TSimpleLED ToaiSimpleLED

💡 What could oai mean?

edit

OAI doesn’t have a universal meaning in Lazarus or Free Pascal — you pick something meaningful to you or your project.

  • OAI could mean OpenAI (if you’re releasing it under that brand).
  • It could mean Owen’s Application Interfaces (if your name was Owen).
  • It could even be something like Our Awesome Interface if you want it fun and memorable.

Tip:

  • Prefix should be short (2–5 letters)
  • Should be unique to you or your organization
  • Apply to unit names, file names, and class names
  • Especially important for published components that show up in the Lazarus Component Palette

Step 3: Create icons for the package

edit

Using the Lazarus Image Editor

edit
  1. Open Tools → Image Editor.
  2. Create a 24×24 icon and save it in the package folder.
  3. Assign the icon in the Package Options.

LRS Explorer

edit

Use the built-in LRS Explorer to embed icons into your unit.

Using lazres

edit

Compile resources into a .lrs file with the lazres tool.

Using glazres

edit

Graphical alternative to lazres.

Recompiling Packages

edit

After adding or modifying components, recompile your package so the IDE sees your changes.

Removing Packages

edit

To remove a package, open the Package Editor and click Remove.

Using embedded (visual) components

edit

Some custom controls use existing components internally for layout or functionality.

Using custom paint procedure

edit

Override the Paint method to custom draw your component.

Integrating the component with the IDE

edit

Property editors

edit

Special editors for selecting or editing complex property values.

Component editors

edit

Custom right-click menus and design-time behavior.

Design-time component debugging

edit

Use Lazarus’s debugger to trace component behavior in the IDE.

Adding contents for the Information box

edit

Simple

edit

Add a short description in the package editor.

FPDoc Editor

edit

Use FPDoc Editor to add detailed help for your component.

See also

edit