Chapter II-delphi Object-oriented programming method (i) (2)

Source: Internet
Author: User
Tags constant numeric value first row

2.1.2.3 Constants

Constants are given a value at the time of the description and are immutable during program execution. The following example illustrates three constants:

Const

Pi = 3.14159;

Answer = 342;

ProductName = "Delphi";

Like variables, constants also have types. The difference is that a constant assumes that its type is the type of the value it represents in the constant description. The types of the three constants above are real, shaped, and string-shaped. Constants Use "=" to indicate that the values on both sides are equal.

2.1.3 Process and function

Procedures and functions are modular parts of a program that perform specific tasks. The Delphi runtime contains a number of procedures and functions for your application to invoke. You don't have to understand the logic of processes and functions, but you know the purpose of procedures and functions. The procedures and functions described in an object are called methods (method). All event-handling procedures are procedures that begin with a reserved character procedure. Each event-handling process contains only the program code that needs to be executed when this event occurs. Use the procedures and functions that Delphi already exists in event handling, just call them in program code.

2.1.3.1 a simple routine that calls the Delphi method

The following will be compiled by editing the text of a memo part, such as cutting, copying, pasting, and erasing, and introducing the method of invoking Delphi process and function.

The Memo (memo) widget has a Cuttoclipboard method that enables you to move text selected in Memo to the Clipboard. Since this feature has been built into this method, you only need to know what this method does and how it can be used.

The following statement shows how to invoke the Cuttoclipboard method of a memo part named Memo1:

Memo1.cuttoclipboard;

Describes the Cuttoclipboard method of which part is invoked by specifying the name of the Memo1. If you do not specify an object name, Delphi displays a unknown identifier error. When the event-handling process is triggered, the program executes the statements in Cuttoclipboard to clip text from the Memo1 to the Clipboard.

The routines below show you how to call Delphi to cut and copy the text information of the memo part to the Clipboard, paste the tagged text from the Clipboard into your notes, and clear the four features such as all the text in the notes part.

Open a new empty form, add a Memo widget and four buttons, and arrange them neatly. Change the Name property of the button part, named Cut,copy,paste,clear. You will notice that when the Name property changes, the Caption property changes accordingly. Add "&" to the Caption property before setting up the accelerator key

Set the ScrollBars property of the memo part to scvertical so that you can add a roll bar. Set the WordWrap property to True so that when user input text reaches the right edge of the memo part, it automatically returns to the line. Deletes the Memo1 text in the first row of the line property so that the memo part is empty when it is initially displayed.

Set up the following event handling procedure for each button:

Procedure Tform1.cutclick (Sender:tobject);

Begin

Memo1.cuttoclipboard;

End

Procedure Tform1.copyclick (Sender:tobject);

Begin

Memo1.copytoclipboard;

End

Procedure Tform1.pasteclick (Sender:tobject);

Begin

Memo1.pastefromclipboard;

End

Procedure Tform1.clearclick (Sender:tobject);

Begin

Memo1.clear;

End

Execute this program. You can enter text in the notes part, and you can cut, copy, paste, and clear it arbitrarily after the text has been marked. When the button is pressed, the corresponding procedure is invoked to process it. Users can consult the online Help for the topic Search of the memo part and refer to method in memo component to get detailed instructions for the above process.

2.1.3.2 calls Delphi's containing parameter process

Some procedures require the user to indicate a parameter. The invoked procedure uses the passed-in parameter values at execution time, and these values are considered to be variables that have been described in the procedure. For example, the LoadFromFile method is described in the Tstring object as:

Procedure loadfromfile (const filename:string);

When you invoke this procedure, you should indicate that the filename parameter is the name of the file to be mounted. The following program opens the Open dialog box, and when you select a file, Delphi reads the file into a memo part:

Begin

Opendialog.execute;

Memo1.lines.LoadFromFile (Opendialog.filename);

End

2.1.3.3 use Delphi function

As with procedures, the program code of a function performs a specific task. The difference between this and the procedure is that the function returns a value when it executes, and the procedure does not return a value. Functions can be assigned to a property or variable, or you can use the return value to determine the process of the program.

We have actually contacted the function in the previous article. When you tell a variable, you have used the following program segment: Edit1.text: = IntToStr (X + Y), where INTTOSTR (value) converts a numeric value of a longint type to a string, and value is the inttostr unique argument. It can be an integer value, a variable, a property, or an expression that produces an integer value. To call a function, you must assign the return value to a variable or property that is compatible with this return value type.

Some functions return a boolean amount of true or false, and the user's program can decide to jump based on the return value. The following routines describe the judgment usage of the function return value Boolean:

Add a ColorDialog object to the form and a button with a Name property of ChangeColor. The event-handling process for the button's onclick event is set up as follows:

Procedure Tform1.changecolorclick (Sender:tobject);

Begin

If Colordialog1.execute Then

Form1.color: = colorDialog1.Color

Else

Form1.color: = clred;

End

This event-handling procedure uses an Execute method that returns a Boolean value. Press the button and select a color in the Color dialog box. If you press the OK button, The Colordialog.execute method returns True, the Form1.color is assigned the colorDialog1.Color, the form shows the color you choose, and if you press the Cancel button in the Color dialog box, the method returns a value of false, and the form changes to red.

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.