19th Chapter-delphi Custom Parts Development (II.) (1)

Source: Internet
Author: User

19.2.2 Delphi Parts Programming

19.2.2.1 Create Properties

Properties are the most specific parts of a part, primarily because they can be seen and manipulated by the user at design time and can be returned immediately in the interactive process. Properties are also important, because if they are designed, they will make it easier for users to use and maintain themselves easily.

To enable you to better use attributes in your parts, this section describes the following:

Why to create a property

Types of properties

Advertisement (Publishing) inherited properties

Defining part Attributes

Writing the Property editor

1. Why to create a property

Attributes provide a very important benefit, the most obvious benefit being that properties can appear in the Object inspector window at design time, which simplifies programming because you only need to read the values assigned by the user, not the parameters of the constructed object.

From the point of view of the user of the part, the attribute image variable. A user can assign a value or read a value to a property, as if the property is the domain of the object.

From the viewpoint of the part writer, the attribute is more powerful than the domain of the object.

⑴ users can set properties at design time

This is very important because it is not like a method that can only be accessed at run time. Properties allow a user to customize a part before running it, and usually your parts should not contain many methods, and their functions can be implemented by attributes.

The ⑵ property can hide detailed implementation details

The ⑶ property can cause a response that is simply assigned, such as triggering an event

⑷ implementations of properties can be virtual methods, so seemingly simple attributes will implement different functions in different parts.

2. Type of property

A property can be any type that a function can return, because a property's implementation can use a function. All Pascal types, compatibility rules are applicable to attributes. The most important aspect of selecting a type for a property is that different types appear in the Object Inspector window in different ways. The Object inspector will determine how it appears in different types.

You can also describe different property editors when registering a part.

The following table lists how properties appear in the Object Inspector window

Table 19.3 The way properties appear in the Object Inspector window

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Property type handling method

───────────────────────────────────────

The simple type Numeric, character, and string attributes appear in Object Inspector, and users can

To edit directly

Enumeration Type Enumeration Type property display values are defined in the code. Drop-down appears when you select

list box to display all possible values.

Collection type collection types appear in the Object Inspector window as a collection that, when expanded, is

The user is selected by setting the collection element to true or false.

An object type has a property editor as the object's property itself, if the object has its own published

, users can edit them independently in object inspector by expanding their property columns.

property of an object type must inherit from Tpersistent.

Array-type array properties must have their own property editors, Object Inspector has no inline logarithm

Support for group property editing.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

3. Publish Inherited properties

All parts inherit properties from ancestor types. When you inherit from an existing part, the new part inherits all the attributes of the ancestor type. If you inherit an abstract class, the inherited property is protected or public, but not published. To enable a user to access the protected or public property, you can redefine the property to published. If you use Twincontrol inheritance, it inherits the Ctl3d attribute, but protected, so the user cannot access Ctl3d at design and run time, and changes the access level of Ctl3d by declaring published as Ctl3d in the new part. The following code shows how to declare Ctl3d as published so that it can be accessed at design time.

Type

Tsamplecomponent=class (Twincontrol)

Published

Property Ctl3d;

End

4. Define part attributes

Declaration of the ⑴ property

Declare the properties of the part you want to describe:

Property name

Type of property

Ways to read and set property values

At a minimum, the part attribute should be defined as the public part of the part object declaration, so that it can be easily accessed from the outside at run time, and in order to be able to edit the property at design time, the property should be declared in the published section so that the property can automatically appear in the Object Inspector window The following is a typical property declaration:

Type

Tyourcomponent=class (tcomponent)

...

Private

Fcount:integer {internal storage domain}

function Getcount:integer; {Read Method}

Procedure SetCount (Acount:integer); {Write Method}

Pubilic

Property Count:integer read GetCount write SetCount;

End

⑵ Internal data storage

on how to store the data value of the property, Delphi has no special requirements, usually Delphi components follow the following provisions:

Property data is stored in the object's data field

The identifier of an Attribute object field begins with F, such as a property defined in Tcontrol fwidth

The object field of the property data should be declared in the private part

The descendant part should only use the inherited property itself, and not directly access the internal data store.

⑶ Direct Access

The easiest way to make property data available is direct access. The Read and write sections of the property declaration describe how to assign a value to an internal data field without invoking the access method. But generally use read for direct access, and write for method access to change the state of the part.

The following part declaration demonstrates how to use direct access in the read and write sections of a property definition:

Type

Tyourcomponent=class (tcomponent)

...

Private {internal storage is proprietary}

Freadonly:boolean; {Declare the field that holds the property value}

Published {make properties available at design time}

Property Readonly:boolean read Freadonly write freadonly;

End

⑷ access method

The declaration syntax of a property allows the read and write portions of a property declaration to replace an object's private data field with an access method. Regardless of how the property implements its read and write sections, the method implementation should be private, and descendant parts can only be accessed using inherited properties.

① Reading method

The Read method of a property is a function with no parameters, and returns a value of the same type as the property. Usually the name of the read function is "get" plus the property name, for example, the Read method of the property count is GetCount. The only exception with no parameters is the array property. If you do not define the Read method, the property is write-only.

② Writing method

The process of writing a property always takes only one parameter. Parameter can be a reference or a value. Usually the procedure name is "Set" plus the property name. For example, the Write method name for the property count is SetCount. The value of the parameter takes the new value of the Set property, so the Write method needs to perform the operations written in the internal stored data.

If no write method is declared, the property is read-only.

You usually want to detect whether the new value is different from the current value before setting the new value.

The following is a simple write method for the integer attribute count:

Procedure Tmycomponent.setcount (Value:integer);

Begin

If value <>fcount then

Begin

Fcount: = Value;

Update

End

End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.