RAD Studio
ContentsIndex
PreviousUpNext
Modifying an existing component
Name 
Description 
You create every component the same way: you create a unit, derive a component class, register it, and install it on the Tool palette. This process is outlined in Creating a new component.
For this example, follow the general procedure for creating a component, with these specifics:
  • Call the component's unit Memos.
  • Derive a new component type called TWrapMemo, descended from TMemo.
  • Register TWrapMemo on the Samples page of the Tool palette.
  • The resulting unit should look like this:
 
The easiest way to create a component is to derive it from a component that does nearly everything you want, then make whatever changes you need. What follows is a simple example that modifies the standard memo component to create a memo that does not wrap words by default.
The value of the memo component's WordWrap property is initialized to True. If you frequently use non-wrapping memos, you can create a new memo component that does not wrap words by default.
Note: To modify published properties or save specific event handlers for an existing component, it is often easier... more 
Once you have created a new component class, you can modify it in almost any way. In this case, you will change only the initial value of one property in the memo component. This involves two small changes to the component class:
  • Overriding the constructor.
  • Specifying the new default property value.
The constructor actually sets the value of the property. The default tells Delphi what values to store in the form (.dfm for VCL applications ) file. Delphi stores only values that differ from the default, so it is important to perform both steps. 
When a component is placed on a form at design time, or when an application constructs a component at runtime, the component's constructor sets the property values. When a component is loaded from a form file, the application sets any properties changed at design time.
Note: When you override a constructor, the new constructor must call the inherited constructor before doing anything else. For more information, see Overriding methods.
For this example, your new component needs to override the constructor inherited from TMemo to set the WordWrap property to False. To achieve this, add the constructor override to... more 
When Delphi stores a description of a form in a form file, it stores the values only of properties that differ from their defaults. Storing only the differing values keeps the form files small and makes loading the form faster. If you create a property or change the default value, it is a good idea to update the property declaration to include the new default. Form files, loading, and default values are explained in more detail in Making components available at design time.
To change the default value of a property, redeclare the property name, followed by the directive... more 
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!