Hardware and software setup

Application in c with a graphical interface. Application Development Methodology Using Forms

Hello everyone. In my previous lessons, I talked about creating console applications in the Borland C++ Builder environment. Starting from this lesson, we will learn C++ using graphical applications as an example. Who wants to learn how to create consoles. Can read the book "The Art of Building Console Applications in C++". Our first application will be a program that displays random number. We open borlandyayu, just do not create a console application. After launch, the following form will appear on the screen:

At the top there will be a toolbar:

On the right, the Object Inspector and the Form List:

Components (unlike BASIC) are already divided into tabs. By name, it is not difficult to guess what type of components are placed on the tab. Open the standart tab and place the components on the form like mine:

The button will be labeled Button1. She needs to be changed. In the lower left window of Borland, the properties of the components are shown; they must be changed to ours:

At the button Caption (Inscription) change to Generate

For label Label1, change the Caption property to Number

In Edit1, the Text property (the text in the edit itself) is simply erased.

After these manipulations, the form will look like mine:

We've finished with the visual style, by the way, most of the properties in Borlyad look like BASIC or Delphi. Experiment with them.

Now let's talk about events. Each component has events, they contain code that will be executed when certain conditions are reached. For example, for a button, the code in the Click event will be executed when we click on it, etc.

Today we will only use the Click event. Click twice on the button and get into the code window:

Automatically created click event for button. The code, like a console application, is placed between curly braces. We write the code:

Void __fastcall TForm1::Button1Click(TObject *Sender) ( double aaa; //Put the number generated by the processor here String count; //Write the same number, but reduced to a string randomize; //This is necessary so that the numbers do not repeat aaa=random (34)*43646; //Generate any number count=FloatToStr (aaa); //Translate the number into a string using the FloatToString function Edit1->Text=count; //Output a string variable in the text window)

The algorithm is simple, we declare a variable to store in it fractional number, and a variable for the number in string form. The fact is that the immediately generated number cannot be displayed in a text box (there will be an error. A text was expected and a number was received), so using the FloatToStr function we translate the number into a string and display it in a text box. For output, we turn (using the -> sign (similar to a dot in vb)) to the Edit1 text property and display the text there. That's all for now.

By the way, a backfilling question: who breeds faster computer viruses, Chinese, or rabbits?


Comments()

Vitay

artyomka

"randomize; //This is necessary so that the numbers do not repeat." I still keep repeating. what to do?

Andrey

There are 2 options 1-use "randomize();" or in line 6 complicate the function e.g. add more seconds

Andrey

"seconds" or the result of adding two pseudo-random numbers divided by seconds - the more factors, the more unpredictable the number is

artyomka Alexey(alex13sh)

randomize
this is so that the numbers do not repeat when the program is turned on
well, that is. turned on the program, press the button several times
1)5
2)47
3)86
this is me in whole numbers
well, if you turn off the program and turn it on again, when the button is pressed serially, there will be the same numbers with the same sequences
this is without randomize, but this will not be removed

And what is repeated in this way
1)3
2)69
3)1
4)3
5)8
6)1
THIS DOES NOT APPLY TO randomize
to avoid this Andrey already answered))

Begzod

I have visual c++.net on my computer. I can not find textbooks, source codes for it. Help pzhs.

Ali05

I saw a textbook on Visual C ++ .Net "Nikita Kultin Programming Basics in Microsoft Visual C ++ 2010" in the bookstore, it just shows how to create graphical applications under Windows (WinForms).

Coolhacker Nintendo

I wonder what is his "bad"?

Coolhacker

In the absence of the ability to properly present the material and accustoming you, gentlemen, to a bad programming style, a la transliterated variable/function names.

Edward Coolhacker

Ministry of Education and Science of the Russian Federation

Federal State Budgetary Educational Institution

higher professional education

"UFA STATE OIL

TECHNICAL UNIVERSITY"

Department of Computer Science and Engineering Cybernetics

Create application c GUI user in microsoft visual studio 2010 environment

Teaching aid

for laboratory and practical classes

with students of the direction

230100 (09.03.01) "Informatics and Computer Engineering"

The teaching aid provides theoretical information, tasks for practical and laboratory work of the course "Programming".

The manual is addressed to teachers of the discipline, as well as students of the direction: 230100 "Informatics and Computer Engineering".

Compiled by: Gabdullina A.A., Art. lecturer at the department VTIK

Druzhinskaya E.V., Art. lecturer at the department VTIK

Reviewer: Filippov V.N., Ph.D., Associate Professor of the Department. VTIK.

1. Theoretical information 4

1.1. Basic concepts 4

1.2. Acquaintance with Windows application Form in Microsoft Visual Studio 2010 4

1.3. Form 7 control

1.5. MessageBox 9 function

1.6. TextBox Control 10

2.Practical task. Introduction to Windows Form Application in Microsoft Visual Studio 2010 12

2.1. Assessment structure for completed work 12

2.2. Procedure for performing practical work 12

3. Laboratory work. Application Development in Microsoft Visual Studio 2010 16

3.1. Lab order 16

3.2. Task 1. Tabulation of a function and calculation of its values ​​in the specified interval with a given step 16

3.3. Individual tasks 19

3.4. Task 2. Processing two-dimensional arrays 21

3.5. Individual tasks 27

  1. Theoretical information

  1. 1.1. Basic concepts

Automation of information processes is currently represented, first of all, by the development of a software application with a graphical user interface (GUI) that manages data flows.

Graphical User Interface (GUI) is a system of means for user interaction with a device, based on the presentation of all system objects and functions available to the user in the form of graphical screen components (windows, buttons, scroll bars, etc.).

Most often, the interface elements in the GUI are implemented on the basis of metaphors and display their purpose and properties, which makes it easier for unprepared users to understand and master programs. Thus, the user's work is carried out with screen forms containing control objects and toolbars with action buttons for processing.

A standard graphical user interface must meet a number of requirements:

Maintain the information technology of the user's work with the software product;

Focus on the end user who communicates with the program at the external level of interaction;

Satisfy the “six” principle, when no more than 6 concepts are included in one menu bar, each of which contains no more than 6 options;

    maintain a standardized purpose of graphic objects and, if possible, their location on the screen.

In object-oriented programming, we deal with classes and objects. Objects are composite data types: they combine multiple values ​​into a single unit and allow us to write and store those values ​​by name. In other words, an object is an unordered collection of properties, each with a name and a value. The named values ​​contained in an object can be either primitive types, such as numbers or strings, or other objects.

When developing non-console applications, the main concept is the Form.

A form is a container for hosting design environment controls.

Properties - the ability to access information stored in this element.

Methods are a set of actions that an object can perform.

An event is an action recognized by an object (for example, a mouse click, a key press), for which a response can be programmed, i.e. object's response to the event.

In this article, we will talk about creating simple applications using forms in C ++. I want to make it clear right away: the development of "form" C ++ applications will be carried out in the Microsoft Visual Studio environment (we will bypass the main competitor from Borland Software). It is worth noting that in Visual Studio there are two ways to make an application with forms, the decision is made at the time of creating a new project.

The first of these is to use Windows Forms, which implements a graphical user interface and is part of the Microsoft.NET Framework. This approach simplifies access to interface elements. Microsoft Windows by wrapping the Win32 API in managed code. If you put your thoughts in a more understandable form, then this approach is very similar to building a console application, but a little more complicated. uses forms.

The second method is based on using Microsoft Foundation Classes (MFC), a library that takes care of building the application framework. Unlike the first, MFC out of the box uses the MVC (Model-View-Cont roller) pattern. This approach is more complicated than the first, but relying on it, you can easily create a framework for a very interesting applications, eg, text editor or use the Ribbon component and make a menu like in the notorious MS Office 2010.

Creating an application in MS Visual Studio

Let's create a new application: File->New->Project. In the window that appears, as in the figure above, find and select Windows Forms Application, then specify the name (app1) and location of the new project and confirm its creation by clicking the "OK" button.

Before proceeding directly to programming, you should understand what an event is. An event is an action that occurs under certain conditions. The simplest (and most common and) can be considered Load, Click ... let's look at some of them in more detail:

  • Activated - an event that occurs when the element is activated.
  • Click - occurs when the element is clicked once.
  • DoubleClick - double click on the element.
  • HelpRequested - fired when a key is pressed .
  • Paint - fired when an element is being redrawn.
  • MouseLeave - The event fires when the cursor leaves the bounds of the element.

Do not forget that the events specific to an element depend on its type. To view all events available for the form object, select it and select the lightning bolt icon in the properties window. Below is a part of the events for the elementForm1. As you can see, the Form1_Load function is selected for the Load event, so the code contained in this function will be called when the form is loaded.

Open the file Form1.h, there will be the following code:

Private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)( )

This is the same Form1_Load function that fires when the form is loaded. Let's verify this by adding a TextBox component to the form.

To do this, open the form resource and select it. Next, select the Toolbox toolbar and drag the TextBox component onto the form. Modify the Form1_Load function as follows:

Private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) ( textBox1->Text = "Hello, World!"; //textBox1 is the name of the text box you added)

Run the project, as a result, the following message should appear:

That's all for now, continued in the following lessons.

Last update: 05/26/2019

In Xamarin.Forms, the visual interface is made up of pages. The page is an object of class Page , it occupies the entire screen space. That is what we see on the screen mobile device is a page. An application can have one or more pages.

The page accepts one of the layout containers as content, which in turn contains standard visual elements such as buttons and text fields, as well as other layout elements.

Let's take the HelloApp project created in the previous topic (or create a new one). By default, the entire interface is created in the App class, which is located in the App.xaml.cs file and represents the current application:

Its default code is:

Using System; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace HelloApp ( public partial class App: Application ( public App() ( InitializeComponent(); MainPage = new MainPage(); ) protected override void OnStart() ( // Handle when your app starts ) protected override void OnSleep() ( / / Handle when your app sleeps ) protected override void OnResume() ( // Handle when your app resumes ) ) )

The App class starts with the constructor, where the InitializeComponent() method is first called, which initializes the object, and then the MainPage property is set. Through this property, the App class sets home page applications. In this case, it is defined by the HelloApp.MainPage class, which is the class defined in the MainPage.xaml and MainPage.xaml.cs files.

But given way not the only one. Xamarin.Forms allows you to create a visual interface either using C# code or declaratively using the xaml language, similar to html, or a combination of these approaches.

Creating an interface from C# code

Let's add a regular C# class to the HelloApp project, which we'll call StartPage .

And define the following content in this class:

Using Xamarin.Forms; namespace HelloApp ( class StartPage: ContentPage ( public StartPage() ( Label header = new Label() ( Text = "Hello from Xamarin Forms" ); this.Content = header; ) ) )

This class represents a page, so it inherits from the ContentPage class. The constructor creates a label with text, which is set as the content of the page (this.Content = header).

To designate MainPage as start page, change the App class:

Using Xamarin.Forms; namespace HelloApp ( public partial class App: Application ( public App() ( InitializeComponent(); MainPage = new StartPage(); ) protected override void OnStart() ( // Handle when your app starts ) protected override void OnSleep() ( / / Handle when your app sleeps ) protected override void OnResume() ( // Handle when your app resumes ) ) )

The MainPage property now points to the newly created StartPage.

It's also worth noting that Visual Studio has ready template to add new page classes with the simplest code. So, to add a new page, you need to select the Content Page (C#) template when adding a new element:

This class is added to main project solutions (in this case, HelloApp).

The added page class will have the following code:

Using System; using System.Collections.Generic; using System.Linq; using System.Reflection.Emit; using System.Text; using Xamarin.Forms; namespace HelloApp ( public class Page1: ContentPage ( public Page1() ( Content = new StackLayout ( Children = ( new Label ( Text = "Hello Page" ) ) ); ) ) )

This class will also inherit from the base class ContentPage and will have almost the same organization as the MainPage class created above.

And also in the application class, we can set this page as the start page:

Using Xamarin.Forms; namespace HelloApp ( public partial class App: Application ( public App() ( InitializeComponent(); MainPage = new Page1(); ) //........... ) )

Liked the article? Share with friends!
Was this article helpful?
Yes
Not
Thanks for your feedback!
Something went wrong and your vote was not counted.
Thank you. Your message has been sent
Did you find an error in the text?
Select it, click Ctrl+Enter and we'll fix it!