Mega Code Archive

 
Categories / Delphi / VCL
 

5 Facts you Did Not Know about Delphi and Classes and the VCL and Inheritance and Custom Controls and

Title: 5 Facts you Did Not Know about Delphi and Classes and the VCL and Inheritance and Custom Controls and Have you ever created your own class in Delphi? Why not? It's easy. You did? Do you know that you can add your own properties and methods to a TEdit, or any class, without creating a new component? Do you always clear the caption of a panel when you drop it on a form? Why? You can use component templates for that, and much more. Do you know that you *can* change the height of the title/caption row in DBGrid by reaching for its "invisible" RowHeights property? Delphi, VCL, OOP, Classes and a Day in a Developer life Even the birds can sing that Delphi's power is in the VCL - a set of visual components for rapid development of Windows applications in the Delphi language. Without the VCL there's no Delphi. The VCL is a hierarchy of classes designed to help you code faster and with more joy. Here are some Delphi OOP specific tricks and goodies you should know of, to make your coding even more joyful :) 1. Accessing Protected Members of an Object Many Delphi components have useful properties and methods that are marked invisible ("protected") to the "user" of the component - that is you the Delphi developer. In Delphi OOP the protected data of a class is accessible to any method that appears in the same unit as the class. A technique sometimes called the "Protected Hack" means deriving (subclassing) a component from an existing Delphi component with the only purpose or exposing the protected properties and methods. Learn about the Protected Hack in Delphi 2. Fake Creating a New Delphi Control - IDE Component Templates The Delphi IDE lets you derive new components from existing components by *not* deriving :)) You can create templates that are made up of one or more components. After arranging components on a form, setting their properties, and writing code for them, save them as a component template. Later, by selecting the template from the Component palette, you can place the preconfigured components on a form in a single step; all associated properties and event-handling code are added to your project at the same time. Learn about Delphi Component Templates 3. Class Interceptors - Extend a Class - Don't Change the Name You *can* create your own custom controls derived from the existing VCL set by creating a, so called, interceptor class that has the same name as the class being extended. Find out about The Magic Behind type TButton = class(StdCtrls.TButton) 4. Class Helpers A class helper is a type that - when associated with another class - introduces additional method names and properties which may be used in the context of the associated class (or its descendants). Class helpers are a way to extend a class without using inheritance. Sounds lovely? It is. Unfortunately, class helpers can only add class/static methods to a Win32 Delphi class. If you need Help Delphi with Class Helpers 5. Don't use any of Delphi components No, I'm joking, use all. But. But, make sure you are prepared enough to be in a position to extend any class when you need it. Why you should Build your Own VCL. 6. - But I'm not counting this one ;) We can derive a new component from any existing component, but the following are the most common ways to create components: modifying existing controls, creating windowed controls, creating graphic controls, subclassing Windows controls and creating nonvisual components. Visual or not, with or without property editor, from scratch...you name it. It's never to late to start exploring Everything about creating custom components in Delphi. The ultimate source.