How to Create a Custom Lazarus Component
editThis 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- Step 1: Create the package
- Step 2: Create the unit
- Step 3: Create icons for the package
- Recompiling Packages
- Removing Packages
- Enhancing mycomp.pas
- Using embedded (visual) components
- Using custom paint procedure
- Integrating the component with the IDE
- Design-time component debugging
- Adding contents for the Information box
- See also
Step 1: Create the package
edit- In Lazarus, go to Package → New Package.
- Choose a directory for the package. The folder name becomes the package name.
Example: Tutorial folder → package name Tutorial.
- Save the package file (
.lpk) in the package folder.
Step 2: Create the unit
editEach component lives in its own unit. The unit filename must match the unit name declared inside the file.
Create a new unit
edit- In Lazarus, go to File → New Unit.
- Save the unit inside the package folder.
- 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
editIf you already have a .pas file:
- In the Package Editor, click Add → Add Files.
- Select your existing unit file.
- Make sure the unit name in the file matches the filename (without extension).
⚠ Naming Conflicts and Prefixes
editWhen 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
editUse 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?
editOAI 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
editUsing the Lazarus Image Editor
edit- Open Tools → Image Editor.
- Create a 24×24 icon and save it in the package folder.
- Assign the icon in the Package Options.
LRS Explorer
editUse the built-in LRS Explorer to embed icons into your unit.
Using lazres
editCompile resources into a .lrs file with the lazres tool.
Using glazres
editGraphical alternative to lazres.
Recompiling Packages
editAfter adding or modifying components, recompile your package so the IDE sees your changes.
Removing Packages
editTo remove a package, open the Package Editor and click Remove.
Using embedded (visual) components
editSome custom controls use existing components internally for layout or functionality.
Using custom paint procedure
editOverride the Paint method to custom draw your component.
Integrating the component with the IDE
editProperty editors
editSpecial editors for selecting or editing complex property values.
Component editors
editCustom right-click menus and design-time behavior.
Design-time component debugging
editUse Lazarus’s debugger to trace component behavior in the IDE.
Adding contents for the Information box
editSimple
editAdd a short description in the package editor.
FPDoc Editor
editUse FPDoc Editor to add detailed help for your component.