Delphi programming skills

Source: Internet
Author: User

This post adds some of my programming skills to my favorites, including those posted by netizens and collected by the Forum and summarized by my personal colleagues. Most of the data comes from http://www.aidelphi.com and csdn. Sort it here for future use.

1. value range of ordinal type (or variable)
"Ordinal types include integer, character, Boolean, enumerated, and subrange types." is mentioned in Delphi help. That is, the ordinal type includes integer, character, Boolean, enumeration, and subdomain type. To know its value range, you can use the low and high functions. However, the results returned by the low and high functions are generally the maximum value of the input parameter type. Therefore, you must use the Ord function to convert it to an integer. For example, showmessage (inttostr (ord (high (char); the result is 255.

2. override control attributes
When writing a control, if you want to define a new property with the name of an existing property of the parent class, that is, to redefine the existing property of the parent class so that it has different types, it has different functions. For example:
Type
Tmypanel = Class (tcustompanel)
Private
Falignment: integer; // change the alignment attribute of the parent class from the talignment type to the integer type.
Private setalignment (value: integer );
Public
Property alignment: integer read falignment write setalignment;
End;

Implementation

Procedure tmypanel. setalignment (value: integer );
Begin
If (value> = ord (low (falignment) and (value <= ord (high (falignment )))
And (value <> falignment) then
Begin
Falignment: = value;
Inherited alignment: = talignment (falignment); // call the alignment of the parent class.
End;
End;

{Thanks to AI Zhenbao for his enthusiastic answers}

3. Use carriage return instead of the tab key
Another method to move the focus by pressing the carriage return instead of the tab key:
Procedure tfrmeditbase. formkeydown (Sender: tobject; var key: word;
Shift: tshiftstate );
Begin
If key = 13 then
Begin
If shift = [ssshift] Then
Perform (wm_nextdlgctl, 1, 0) // move back
Else
Perform (wm_nextdlgctl, 0, 0); // move back
End;
End;

4. Run other programs in the program
In many cases, such as online update, you need to run additional programs to complete some tasks, and most of the time these tasks operate on the program itself directly or indirectly. In this case, you must exit the program correctly and timely after running the additional program. The "exit" here will not be able to use mainform normally. close, because in this case, the program will wait for the additional program to run and return before exiting. If it is online update, the program itself will fail to update due to sharing conflicts. Therefore, you need to terminate the program incorrectly during the halt process. If necessary, you need to use the chdir () function to change the current directory, so that the program still occupies the directory, leading to the failure of some tasks.

5. Directory and Path
Directory is the directory, and path is the path. We can see from the following two examples:
Procedure tform1.button1click (Sender: tobject );
Begin
Showmessage (extractfilepath (application. exename ));
End;

Procedure tform1.button2click (Sender: tobject );
Begin
Showmessage (extractfiledir (application. exename ));
End;
Set up a default Windows Application in DELPHI6 and run the above Code, as shown in the following order:
C:/program files/Borland/DELPHI6/projects/
C:/program files/Borland/DELPHI6/Projects
Obviously, path has one more "/" than directory.

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.