Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Cource_work_theory.doc
Скачиваний:
3
Добавлен:
31.07.2019
Размер:
90.11 Кб
Скачать

Conditional statements

A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose, C++ provides control structures that serve to specify what and how our program has to perform.

If statement

The "If statement allows your program to make a decision. Its syntax is: if (condition)

{action 1

{ else

{action2

}

If the condition in parenthesis is true, all code inside the first braces is executed, if it is not, the code in­side the first braces is ignored and the code inside the second braces after else is executed.

Condition is a value or an expression that is used to determine which code block is executed, and the curly braces act as "begin" and "end" markers. Note that conditions always are written in parenthesis.

If you have only one statement in action 1 or action2, then the correspondent braces are not required.

Pay your attention, that commands in action 1 are written a little more to the right of the IF statement and commands in action2 are written a little more to the right of the else statement. It is done for visual selection of these commands. The program becomes clearer, it is easier to find and correct errors in it.

Nesting if - else - if statement

There may be any statements instead of Action 1 or Action2, including another if statement. Example: define the sign of variable x (it may be positive, negative or zero).

if (x>0) ShowMessage ("positive");

else

if (x<0) ShowMessage("negative");

else ShowMessage ("zero");

First else means that x is a negative number or zero. Therefore we should check up both of these variants.

Visual components

The Form

The form is the primary object of a visual application. It is rectangular window whose main purpose is to carry, host, or hold other controls to allow the user to interact with the computer.

When C++ Builder starts, it creates a starting form and initializes an application. You can use such as form as you see fit. If you need additional forms, you can create them visually by clicking File -> New -> Form on the main menu.

The top section of a form or a dialog box is called a title bar:

To customize the text displayed on the title bar, on the Object Inspector, change the text of the Caption property. At design time, you can only set the caption as text. The caption can be any type of string.

When the user has finished using a form, he or she must be able to close it. Closing a form is made possible by a simple call to the Close() method. You can type:

Close();

to close the form.

The Button

The most popular button used in Windows applications is a rectangular control that displays a word or a short sentence that directs the user to access, dismiss, or initiate an action or a suite of actions. To add a button to your form, click the Button control from the Standard tab of the Component Palette and click on the form.

From the user's point of view, the only things important about a burton are the message it displays and the action it performs. The Caption of a button is the word or group of words that display(s) on top of the con­trol, showing the message that would direct the user to what the button is used for. To change the caption on a control, on the Object Inspector, click the word Caption and type the desired caption.

To the programmer, one of the most important properties of any control is the Name. This allows you and the compiler to know what control you are referring to when the program is running. By default, the first button you add to a form is named Buttonl, the second would be Button2. Since a program usually consists of many buttons, it would be a good idea to rename your buttons and give them meaningful names. The name should help you identify what the button is used for. For example, OKButton, ExitButton.

Depending on what you are trying to do, sometimes you will not want the user to see a control until an­other action has occurred. To hide a control, use the Visible property. It toggles the appearance and the disap­pearance of a control. Choose false if you do not want to see a button or true if you want to see it.

The most regular action the user performs on a button is to click it. To initiate the OnClick event on a button, double-click it.

Labels

A label is a control that serves as a guide to the user. It provides static text that the user cannot change but can read to get information on the form. The programmer can also use it to display simple information to the user. Most controls on the form are not explicit at first glance and the user would not know what they are. Therefore, you can assign a label to a control as a help to the user.

To add a label to a container, click the Label button rom the Standard section of the Tool Palette and click on the object that would host it.

The most important characteristic of a label control is the text it displays. This is what the user would read. The text of a label is its Caption property and is its default. To set a label's caption, after adding the control to a container, click Caption in the Object Inspector and type the desired value.

When you type the caption of a label, it is continually resized to accommodate its string. If you edit the label, as you delete or add characters, the label resizes itself. If you want to fix the size of the label regardless of its caption, set the Boolean property AutoResize to false. By default, this property is set to false on most controls that use it; but on a label, it is set to true. Once you have set AutoResize to true, if you change the text of the label, only the portion that fits in the allocated space would be seen. Of course, you can resize it manually.

Before or after typing the caption of a label, you can resize its allocated space to your liking. This is because a string occupies a rectangular area. Here is an example:

One of the fancy characteristics you can apply to a label includes its Font and Color. Here is an example:

Memo

Like the Edit control, the Memo control is used to display or receive text. Unlike the Edit control, the memo can be used to display multiple lines of text.

To add a memo to a form or another container, from the Standard tab of the Component Palette, click the Memo button and click on the desired position on the form.

The user mostly reads and/or enters text in the memo when interacting with the control. At design time, you can set the text that would display when the memo comes up. To enter this text, on the Object Inspector, click the Lines field to reveal its ellipsis button that allows you to open the String List Editor dialog box. If you want the control to be empty at startup, delete the content of the String List Editor and click OK. Otherwise, type the desired text and click OK.

To add line with text "Something you want" to Memol, wtrite : Memol->Lines->Add("Something you want");

If you want to clear all the lines in Memol, write: Memol->Clear();

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]