<?xml version="1.0" encoding="UTF-8"?>        <rss version="2.0"
             xmlns:atom="http://www.w3.org/2005/Atom"
             xmlns:dc="http://purl.org/dc/elements/1.1/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									Lazarus Pascal - How to add a new function or property to an existing class - Delphi, Lazarus, Free Pascal				            </title>
            <link>https://www.tweaking4all.com/forum/delphi-lazarus-free-pascal/lazarus-pascal-how-to-add-a-new-function-or-property-to-an-existing-class/</link>
            <description>Tweaking4All.com Discussion Board</description>
            <language>en-US</language>
            <lastBuildDate>Thu, 11 Jun 2026 23:14:43 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>Lazarus Pascal - How to add a new function or property to an existing class</title>
                        <link>https://www.tweaking4all.com/forum/delphi-lazarus-free-pascal/lazarus-pascal-how-to-add-a-new-function-or-property-to-an-existing-class/#post-5835</link>
                        <pubDate>Wed, 30 Apr 2025 11:02:44 +0000</pubDate>
                        <description><![CDATA[Sometimes, a class or object is missing this one little property or function we need. So how do we add a property or function to an existing class or object?
In this example I add the prope...]]></description>
                        <content:encoded><![CDATA[<p>Sometimes, a class or object is missing this one little property or function we need. So how do we add a property or function to an existing class or object?</p>
<p>In this example I add the property "<strong>Selected</strong>" to a "<strong>TPanel</strong>".<br />This is a boolean property, but we could easily do with with a procedure or function as well.</p>
<p>In the beginning of your code (I usually place this either before the TForm definition, or in a separate unit):</p>
<pre contenteditable="false">  TPanel = class(ExtCtrls.TPanel)
  public
    Selected:boolean;
  end;          </pre>
<p>Now this property exists for every TPanel in this unit and can be read or assigned to.</p>
<p>Note: do not forget to add the phrase "<strong>public</strong>". </p>
<p>You can set the default value in the constructor like so:</p>
<pre contenteditable="false">TPanel = class(ExtCtrls.TPanel)
  public
    Selected:boolean;
    constructor Create(AOwner:TComponent); override;
  end;   

...

// Overriding Create Constructor - for example set some default values and event handlers
constructor TPanel.Create(AOwner:TComponent);
begin
  inherited;

  // Set initial value for example ...
  Selected := false;
end;</pre>
<p> Adding a function or procedure is easy as well:</p>
<pre contenteditable="false">TPanel = class(ExtCtrls.TPanel)
  public
    Selected:boolean;
    procedure DoSomething;
  end;   

...

procedure TPanel.DoSomething;
begin
   ...
end;</pre>
<p>This way we do not have to abuse existing properties (eg. Tag), or fumble with all kinds of linked lists, and add convenient functions as well.</p>]]></content:encoded>
						                            <category domain="https://www.tweaking4all.com/forum/delphi-lazarus-free-pascal/">Delphi, Lazarus, Free Pascal</category>                        <dc:creator>Hans</dc:creator>
                        <guid isPermaLink="true">https://www.tweaking4all.com/forum/delphi-lazarus-free-pascal/lazarus-pascal-how-to-add-a-new-function-or-property-to-an-existing-class/#post-5835</guid>
                    </item>
							        </channel>
        </rss>
		