New method for setting component properties 2-setting object property values using message mechanism

Source: Internet
Author: User

 

 

We often need to set the same attributes of multiple controls on the interface. These controls may be the same class controls such as tedit, or they may not be the same class controls such as tedit, tmemo, tcombobox, ttoolbar, tbutton, etc. Attributes that we often use, such as visible, readonly, tcolor, parentcolor, and showhint. Take the readonly attribute as an example.

We know that the following code cannot be used to modify the readonly attribute of controls in batches. tedit and tmemo can only be processed separately.

VaR

I: integer;

Begin

For I: = ComponentCount-1 downto 0 do

If components [I] Is tcustomedit then

(Components [I]
Tcustomedit). readonly: = true;

End;

However, if we want to be lazy and do not want to write a piece of code for each type of control, we can do it only by writing a loop. We also need to be able to process the readonly attribute of other controls inherited from tcustomedit, is there a solution? Since I write it here, there is definitely a way to handle it. The method is to use the message mechanism of VCL.

Using the message mechanism of VCL is indeed an alternative method. I also thought of it inadvertently when I read the source code of VCL. This method has its own particularity and cannot be widely used. However, you can try it in some places.

Let's take a look at the source code of the tcustomedit class.

Tcustomedit = Class (twincontrol)

 
Private


Fmaxlength: integer;


Fborderstyle: tborderstyle;


Fpasswordchar: Char;


Freadonly: Boolean;

Fautosize: Boolean;


Fautoselect: Boolean;


Fhideselection: Boolean;


Foemconvert: Boolean;


Fcharcase: teditcharcase;


Fcreating: Boolean;


Fmodified: Boolean;

Fonchange:
Tpolicyevent;

...........

Procedure
Setreadonly (value: Boolean );

Protected

Property
Readonly: Boolean read freadonly write setreadonly default false;

...........

Procedure tcustomedit. setreadonly (value:
Boolean );

Begin

If
Freadonly <> value then

 
Begin


Freadonly: = value;


If handleallocated then


Sendmessage (handle,
Em_setreadonly, ord (value), 0 );

 
End;

End;

You should understand the source code above. Our code should be written like this

VaR

 
I: integer;

Begin

For
ComponentCount-1 downto 0 do

If
Components [I] Is tcustomedit then

 
// (Components [I] As tcustomedit). readonly: = true;

 
Sendmessage (components [I] As tcustomedit). Handle, em_setreadonly,
Ord (true), 0 );

End;

Handle is the control handle, and all window controls are handled. Therefore, we can set the read-only attribute of all window controls at one time, if it has this attribute.

If
Components [I] Is twincontrol then

 
Sendmessage (components [I] As twincontrol). Handle, em_setreadonly,
Ord (true), 0 );

This method sends an em_setreadonly message to all controls on the form so that the control can modify its Read-Only status. If the control does not have a read-only status, it will not respond to the message. Note that the status of the control is changed, and no attribute is used. Therefore, the value of its readonly attribute is not changed. If you use

If edit1.readonly then

 
Edit1.text: = 'readonly = true'

 
Else

 
Edit1.text: = 'readonly = flase ';

Test, it will show readonly = flase, but tedit can no longer accept the keyboard input, just like setting its readonly attribute to true. To put it simply, we do not change the status of the control by changing the attribute value, but directly send a message through the code to change its status.

At this point, some people may want to track where the em_setreadonly message was processed. I also wanted to see how VCL responded to the message. Later I thought about it and found it unnecessary, because tedit and tmemo controls are one of the basic controls provided by windows, and their functions have been implemented in windows, Delphi only encapsulates them using its own VCL class, use the attribute of the class to close the basic status for your convenience.

Other basic Windows controls encapsulated by VCL can be used to modify the status.

 

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.