C++. Development of the program for problem solving use the operator conditional jump “if”. Overview of the main properties of the form




Development of the program for problem solving use the operator conditional jump “if“. Overview of the main properties of the form

In this paper is given an example of using the operator of conditional jump “if” to solve the task. Also, the basic properties of the main form of application are considered, that allow you to modify its appearance and behavior.

 

The task

A real number a is given. Create a program for calculating the f(a), if:

 

Progress

1. Creating a project

Run MS Visual Studio. Create a project by template “Visual C++ – Windows Forms Application“.

 

2. Create a the main form of application

According to problem condition, create the form as shown in Figure 1. Examples of forms creating are shown in details here and here.

Add to the form the controls of the following types: Label, Button, TextBox.

Figure 1. The main form of the program after the placement of the controls

 

3. Configuring of the application form

On Windows-based applications, the main form is associated with the program. The main form of the program corresponds to the object-variable that is of type:

System.Windows.Forms.Form

By default, the main form of application is named Form1. The control Form1 serves as a basis for placing other controls (buttons, labels, text boxes etc.).

For more convenient display the Form1 control, you can set the following properties.

 

3.1. Property Text

Yo need set the “Text” property into value: “Conditional statement if“. This property specifies the name of the form or the name of the program (application).

 

3.2. Placement of the form against the screen window

To configure the position of the form relative to the monitor window is intended StartPosition property.

By default, property StartPosition = WindowsDefaultLocation. This means that after the start the form is placed in a random place. Position is determined by the Windows operating system.

In this case, you must set the property StartPosition = CenterScreen. This means that the window form (program) will be centered relative to the monitor screen.

If you want to specify your own layout position of the form, then

StartPosition = Manual

In this case, the coordinates of the upper-left corner of the form are specified in the Location structure:

Location.X
Location.Y

If you want to specify the coordinates of the position, the code snippet is as follows:

this->Location = System::Drawing::Point(100, 100);
this->StartPosition = System::Windows::Forms::FormStartPosition::Manual;

 

3.3. Property ControlBox

ControlBox property can take two values: true or false.

If ControlBox = true, then the window control buttons (minimize, maximize) are displayed on the form. If ControlBox = false, then the form is the dialog box without control buttons.

A fragment of code where the programmatically is changed the value of the ControlBox:

this->ControlBox = false;

 

3.4. Sizes of the form. Property Size

Property (structure) Size specifies the size in pixels of the form with the help of two fields:

Width
Height





 

3.5. Setting up the font. The Font property

The Font property allows you to set a font of the text on the form. By default, all controls that are placed on the form, will take the same font as the font of the form.

 

3.6. Borders of the form. Property FormBorderStyle

With the property FormBorderStyle you can set the view and behavior of the form boundaries.

If

FormBorderStyle = Sizealbe

then you can change the size of the form.

If

FormBorderStyle = None

then the form has no frame and header.

If

FormBorderStyle = FixedDialog

then form is a dialog window.

 

4. Setting the properties of form controls

You need set the following properties of controls:

  • in the label1 property Text = “x = “;
  • in the label2 property Text = “Result = “;
  • in the button1 property Text = “Calculate“.

After setting, the form view is as shown in Figure 2.

Figure 2. Main form of application

 

5. Programming the event of click on the button “Calculate

Code snippet of event handler of clicking on the button “Calculate” is following:

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
    float x, f;

    x = x.Parse(this->textBox1->Text);

    if ((-2<=x) && (x<=2))
        f = x*x + 5;
    else
        f = 10 - x;

    label2->Text = "Result = " + f.ToString();
}

In the above code to solve the problem is used the operator “if … else“.