Visual basic programming language. Introduction to Programming in Excel

1. Left-click on the image of the CommandButton
2. Place the mouse pointer on the created form, the appearance of which will change from an arrow to a cross and move it to the place in the form where it will be located created element.
3.Press left button mouse and move the pointer until the image becomes the required size, after which you should release the button - and the object will be created.

We also place the CommandButton2 and Text1 objects on the form. To move an object, you need to select it and move it with the left mouse button to the required position.

Now we need to connect the Microsoft Common Dialog Control 6.0 control component by turning it on in the menu Project--Components and in the list that opens, put a checkmark next to this component and click OK. After that, in the panel controls The CommonDialog button will appear. We also place it on the form. This button will not be visible when saving the application. A pre-created interface view is shown in the figure.

After everything necessary elements added to the form, you can begin setting their initial properties, which will determine the appearance and features of working with the interface. These actions are performed using the properties window, which can be displayed if it is not available in the development environment in several ways:
1. By selecting Properties from the View menu
2. Using the Properties Window button on the standard toolbars.
3. By selecting the Properties item in the context menu for the selected object on the form.

At the very top of the window is shown
An opening list of objects shows their properties below.
In our example, the list of objects contains
Form1
CommonDialog
Command1
Command2
Text1
.

Below, the left side of the column shows the properties of the objects, and the right side shows the property values. First, select the object, then set the value.

We set the property values ​​for the object according to the table. The property values ​​that we did not change remain the same by default.

An object Property Meaning
Form1 (Name) Border Style Caption StartUpPosition frmDialog 1 - Fixed Single Color and Font 2 - Center Screen
CommonDialog (Name)CancelError dlgStandard True
Text1 (Name) Alignment AutoSize Borderstyle Caption lblExample 2-Center True 1-Fixed Single Sample
CommandButton1 (Name) Caption ToolTipText cmdColor Color Select background color
CommandButton2 (Name) Caption ToolTipText CmdFont Font Select font options

Code. The final stage in creating an interface is writing program code to process it. Editing the program is carried out using the code window for created form, which can be opened in several ways:
1. Select the Code item in the View menu (in the same way, you can return to editing the form, select the Object item in the View menu);
2. In the Project Explorer, select the name of the form being created and select the View Code item for it in the context menu (you can return to the form in the same way - the View Object item in the form context menu):
3. Double-click on the corresponding form element for which you need to provide processing of any actions. A method for handling this event can be created by double-clicking on the cmdWork element, after which a template for a procedure will appear that processes the user pressing this button.
Open the code editor and enter the data.

Private Sub cmdColor_Click() With dlgStandard On Error GoTo ErrorLabel .Flags = cdlCCRGBInit + cdlCCPreventFullOpen .ShowColor lblExample.BackColor = .Color End With ErrorLabel: Exit Sub End Sub Private Sub cmdFont_Click() With dlgStandard On Error GoTo ErrorLabel .Flags = cdlCFBoth + cdlCFEffects + cdlCFApply .ShowFont lblExample.Font.Bold = .FontBold lblExample.Font.Italic = .FontItalic lblExample.Font.Name = .FontName lblExample.Font.Size = .FontSize lblExample.Font.Strikethrough = .FontStrikethru lblExample .Font.Underline = .FontUnderline lblExample.ForeColor = .Color End With ErrorLabel: Exit Sub End Sub

Save the project in the format frm And vbp.This will give you the opportunity to return to the project at any time to change or improve it. Now all that remains is to create executable file exe in other words, compile the project into a separate file that can work outside the Visual Basic development environment. In the Project menu, turn on the last line - Properties. In the window that opens, in the tab General indicate the project type - Standard EXE and the project name.
In the tab Make indicate the title and, if desired, you can change the icon. In the tab Compile check the box Compile to Native Code. For more fast work applications, you can enable Optimization for fast code. We don’t need the other tabs. Turn on OK and close the window.

Now let's compile. Open the File menu Do "project name.exe".IN different versions IDE this command may be different, mine is called this, but it can also be Make and Do.

We enable this command and you can check the application by clicking on the menu Start button Start.
It should be under the name and in the place with which you saved it with the extension EXE

See this app in action
Set text and background colors to different ones.



We create our own music player in Wave format.

After you have learned in the first program how to enter commands in the application, we will create your own player. You can save this player on your computer and also modify it for your needs.

Create new project Standard EXE.
Open the project properties window by selecting Projecti Properties from the Project menu and naming the project MyMultiMedia.

Name the project form Formplayer. In the caption property of the form, enter the title Multimedia Player.

Attach the Microsoft Multimedia Control 6.0 component library to your project using the Components dialog box, which is opened from the Project menu using the Components command.

We'll need a dialog box to search for playable files, so in the Components window, also check the box for the Microsoft Common Dialog Control 6.0 component library.

Add an MMControl control to the Formplayer form by double-clicking the MMControl button in the Controls panel and name it MMControlCDPlayer.

Add a commonDialog control to your form by double-clicking the CommonDialog button in the Controls panel, and name it cdplayer.

Add a CommandButton control to your Formplayer form by double-clicking the CommandButton in the Controls panel. Name this button cbFindFile and set the caption property to Find File. Upon the click event of the cbFindFile button created in the form, a dialog box will open to search for playable files.

Open the code editor and enter code to assign the MMControlCDPlayer control the properties it needs to work with WAV files. Private Sub Form_Load() MMControlCDPlayer.Notify = False MMControlCDPlayer.Wait = True MMControlCDPlayer.Shareable = False MMControlCDPlayer.DeviceType = "WaveAudio" End Sub Private Sub cbFindFile_Click() cdPlayer.ShowOpen MMControlCDPlayer.FileName = cdPlayer.FileName MMContro lCDPlayer.Command = "Open " End Sub Private Sub Form (Cancel As Integer) MMControlCDPlayer.Command = "Close" End Sub

Launch the application by clicking the Start button in the menu. Click the Find file button, a window will open allowing you to search, and then select music file Wave for playback. Using the player's control buttons, you can listen to the selected file.
Save the project as in the first application and create an executable file exe

Basic programming language dialect

Definition 1

Visual Basic (VB) refers to the 3rd generation programming languages ​​and is a development environment from Microsoft for the COM programming model. This language was derived from BASIC and supports rapid application development (RAD) graphical user interface (GUI), database access using DAO, RDO, ADO, creation of ActiveX controls and objects. Scripting languages ​​(VBA, VBScript) have a syntax similar to VB.

The VB language is a modern programming language that combines procedures and elements of object-oriented and component-oriented programming languages. The VB development environment includes tools for visually designing a user interface. A programmer can create an application using the components provided in VB. Applications built in VB can also use the Windows API (external function declarations are required).

Features of the language

VB is often used to teach programming and is used for application development. Allows you to develop simple and complex applications with a graphical interface. Programming in VB combines visual components and controls, defining attributes and events for components, and writing additional code to expand functionality. Default values ​​and actions for components allow you to create simple programs without writing any code.

Program compilation was introduced in VB version 5, but executable programs still require certain libraries to run. Such libraries have been included in Windows since version 2000.

A form in VB is created using drag and drop technology. Controls ( text fields, buttons, etc.) are placed on the form and have their own properties, methods and event handling. Many control attributes can be changed during program execution, resulting in programs that respond dynamically to user input.

The VB language allows you to create executable programs (files with the extension .exe), ActiveX controls, DLLs, but is most often used for developing Windows applications. Dialog boxes are used to provide hints. A set of controls allows you to provide basic functionality application, and with the help of event handlers you can expand the program logic. For example, when you use a drop-down list, a list is automatically displayed that allows you to select the desired item.

An event handler is used to write additional code depending on the selected element. The language has a large library of service objects and object-oriented development. VB is case-insensitive, unlike many other programming languages. String comparisons are case-sensitive, but you can also do them case-insensitively.

The VB compiler ships with other Visual Studio languages ​​(C, C++), but IDE limitations prevent the creation of some types of applications.

Visual Basic Features

VB has the following features:

  • The true value is determined by one, and the false value by zero, because The Boolean type is stored as a 16-bit signed integer. These two meanings are related logical operation Not, i.e. True = Not False.
  • Logical and bit operators are combined, which distinguishes VB from other C-like languages ​​(Java, Perl).
  • When declaring arrays, upper and lower bounds are specified, as in Pascal and Fortran. Moreover, the lower bound can have a value lower than 1 or 0, unlike Visual Basic .NET or VBScript, in which the lower bound is fixed.
  • Strong integration with Windows operating system and COM.
  • Integers are automatically converted to real numbers when division (/) is performed. The () operator is used for division by truncating the fractional part.
  • For variables that are declared without specifying a type, the default type is variant. Using the Deftype operator, you can change this setting and set a different default type (for example, DefInt, DefVar, etc.). The default type can be overridden for variable by using a special suffix in her name.

Evolution of Visual Basic

The first version of the VB 1.0 language was developed in 1991. Alan Cooper developed the principle of communication between the language and the graphical interface, which is used in the language and is implemented in its Tripod prototype (known as Ruby).

In May 1991, VB 1.0 for MS Windows appeared.

VB 1.0 was released for DOS in 1992. It was not fully compatible with the Windows version of VB because it ran in text mode screen.

Figure 1. Visual Basic for operating system MS-DOS. Author24 - online exchange of student works

In the same year it appeared a new version VB 2.0, which was easier to use and faster.

In 1993, Standard and Professional language VB 3.0, which included an engine for working with databases Access. VB 4.0 (1995) allowed the creation of 32-bit and 16-bit Windows programs. It also became possible to write classes in VB and the ability to compile into machine code “native” for the processor, which significantly increased the speed of program execution.

1997 – VB 5.0 – a version in which it became possible to create your own components.

In 1998, Russian version 6.0–6.3 appeared, which supported the ability to use variable and procedure names in Cyrillic. VB.NET, which appeared in 2001, came with the .NET Framework, and in 2003 - VB.NET 2003 with the .NET Framework 1.1.

The 2005 version - Visual Studio 2005 - included VB.NET 2005. The .NET Framework 2.0 was released and Microsoft SQL Server 2005. A little later, a version of VB Express appeared - a free edition of VB.NET 2005

Main Varieties of Visual Basic

Note 1

Microsoft has developed variations of VB for scripting. Over time, the original VB was replaced by the .NET version.

  • Classic VB (versions 5-6) is distinguished by its strong attachment to its development environment and to the Windows OS, intended for writing exclusively Windows applications. Includes a large number of tools for assistance and ease of programming: built-in debugger, viewing variables and data structures on the fly, debugging window, tooltip when typing the program.
  • VB for Applications (VBA) is a programming tool for writing macros and other application programs for specific applications. It became popular due to its use in the MS Office software package. Due to low attention to security issues, the widespread use of VBA has led to the spread of macro viruses.
  • VB Scripting Edition (VBScript) is a scripting language that is a stripped-down version of classic VB. Used primarily for administration automation Windows systems as well as for creating ASP pages and scripts for Internet browser Explorer.
  • VB.NET is the successor to VB 6.0 and part of Microsoft platforms.NET.

Programming system this is the package software, which in addition to the translator also includes an editor for entering code, tools for automating the creation and debugging of programs, libraries with ready-made blocks code, a convenient reference book and other special tools.

For the BASIC programming language, there is the Microsoft Visual Basic programming system - VB for short. For the Pascal programming language - Borland Delphi. For the SI++ programming language, these are Microsoft Visual C++ and Borland C++ Builder. From any application Microsoft Office In the Windows operating environment, you can run the Visual Basic for Applications (VBA) programming system in short.

The listed programming systems are visual programming systems, that is, objects in this system are created using the mouse.

Windows OS has a lot of standard elements: windows, menus, buttons, etc. For them, the system has prepared standard blocks machine code - they are stored in dynamic libraries - files with the extension .DLL. Therefore, a Windows program no longer looks like a set of sequential commands, but like countless calls to procedures stored in Windows. Considering the above, for programming in the Windows operating system environment, it is necessary to purchase programming system programs.

Visual Basic is a general purpose programming language for beginners. Microsoft Visual Basic is a software development tool developed by Microsoft Corporation and includes a programming language and development environment.

The program in which new programs are created is called the Visual Basic programming environment. Since this environment runs on a computer along with the Windows operating environment, you will create programs called Windows applications or simply: applications. To create an application, you need to create a project. This is what you will do in the Visual Basic programming environment.

Advantages of Visual Basic

    Visual Basic compares favorably with other programming languages ​​due to its simplicity and clarity.

    Visual Basic is a dynamically developing language.

    Visual Basic is built into programs such as Word, Excel, etc. It can be used to control these programs from other programs.

    Visual Basic is an object-oriented language. The basis of the language is objects. For example: a window, a button, a combo box with which the program works.

Basics of programming in visual basic

When you start the visual programming system (in this system, objects are created using the mouse), you will have access to: a window of standard object classes; properties window for these objects; code window.

STANDARD OBJECT CLASSES, for example, such as WINDOWS (a window is called a FORM) and CONTROLS (buttons, lists, fields, radio buttons, check boxes, command buttons and others), are presented in the visual programming system as icons on the toolbar.

Using the mouse, the programmer drags control elements from the ToolBox toolbar onto the FORM.

Next, the programmer uses the mouse to call up the OBJECT PROPERTIES window and configures the properties of each object (selects with the mouse from the OBJECT PROPERTIES window), for example, such as: name, size, color characteristics, position on the form and on the screen, font, labels on objects, and so on.

Now these objects, with the properties we have specified, need to be assigned a certain EVENT PROCEDURE (or they say - it is necessary to configure the object’s methods).

Assigning an EVENT PROCEDURE to objects means writing a program that starts executing after the occurrence of a certain event. For example, after clicking on a command button, text appears in a text field, or, for example, when opening a window, a sound is heard, and before closing, a prompt appears asking for confirmation to close the window.

To assign an event procedure to an object, the programmer usually double-left-clicks on the object, and in the PROGRAM CODE window that appears, writes a program in a programming language (this can be Basic, Pascal or C++, depending on the programming system).

In programming systems, writing a program is as easy as possible, since after double-clicking on an object, a program code window appears with a ready-made empty template of the event procedure; all that remains is to use the mouse or keyboard to make some changes in the template (if necessary) and add the program in the process of compiling the program The auto hint system can be of great help - the editor automatically offers the programmer a list of operators, functions, and object properties as needed - all that remains is to select from the list and click the mouse.

After placing all the necessary controls on the form, setting their properties and creating event procedures, the program is ready. All that remains is to give a command to translate it (for VB, click Start in the toolbar, or the RUN – Start menu, or press F5): the Delphi and C++ Builder systems perform the compilation, and the Visual Basic system performs the interpretation.

Starting the Visual Basic Programming Environment

Starting the Visual Basic programming environment:

StartPrograms→ Microsoft Visual Basic 6.0.→ Microsoft Visual Basic 6.0.

A window appears with three tabs: New, Past, Existing.

Rice. 1. Window for adding an object.

Using the window, you can create a new project or open an existing one.

Creating a new project: New → Standard EXE → Open.

A project for the future application is created, which may contain forms, modules and other components.

Open an existing project: Existing → Open the desired folder→ Select the desired project → Open.

Open an object that was previously opened on this computer: Past → Select the desired project → Open.

Visual Basic Programming Environment Window

Rice. 2. Visual Basic programming environment window.

    control panel.

    project explorer window.

    The properties window for the current control or form.

    window for placing the form on the monitor screen.

    window for entering commands. Commands are executed immediately after input.

If any of the listed elements are not visible, they can be displayed (or hidden) using the View menu.

View → Toolbar.

View → Project Explorer.

View → Properties window.

View → Form Placement Window.

View → Emergency Window.

The form window can be displayed by double-clicking on the icon or name of the form in the project explorer window.

For self-study Visual language Basic allows you to understand the basics of programming from scratch without the help of a teacher and learn how to create full-fledged applications for Windows. The large amount of information about Visual Basic also makes it possible to use the book as a reference.

* * *

The given introductory fragment of the book Visual Basic for Beginners. Step by step. Tutorial/reference book (Evgeniy Matveev) provided by our book partner - the company liters.

Chapter 2: Introducing Visual Basic

In this chapter you will learn:

Launch Visual Basic,

Find what you need menu items,

Use the toolbar buttons

Understand all the main interface objects,

Create application projects,

Choose the right one application type,

Save and open recorded projects,

Write your first program

Use the help system correctly.

2.1. Programming Environment Overview

Mainly Windows menu select team Start – Programs – Microsoft Visual Studio 6.0 – Microsoft Visual Basic 6.0(Start – Programs – Microsoft Visual Studio 6.0 – Microsoft Visual Basic 6.0) Visual Basic will start and a dialog will appear on the screen New Project(New Project) expanded in the tab New(New) (Fig.2.1).


Fig.2.1. New Tab


Dialogue New Project(New Project) offers the user the option of creating a new one or opening an existing project. Project is an application under development. It consists of several components stored on disk in the form separate files, each of which has a corresponding extension:

Form files with source code ( *.frm),

Form files with executable code ( *.frx),

Module files ( *.bas),

Class module files ( *.cls),

Additional control files ( *.ocx),

Project file ( *.vbp),

Resource file ( *.res),

Other files.

By selecting one of the icons on the tab New(New), you can create a new empty project the right type or use the services of one of the application generation wizards.

At the end of the wizard, a draft version of the application is obtained with ready-made forms, menus, toolbars, etc. Then the programmer has to finalize many small details, but thanks to the use of the wizard, time is saved on building the main framework of the application.

Now let’s list the options for created applications or components that are selected on the tab New(New) dialogue New Project(New project):

Standard EXE– standard application,

ActiveX EXE– ActiveX component as an application,

ActiveX DLL– ActiveX component in the form of a dynamically loaded library,

ActiveX Control– an ActiveX component in the form of a Visual Basic tool for creating controls,

VB Application Wizard– Visual Basic Application Wizard,

VB Wizard Managercontrol program on creating custom wizards,

Data Project– application for working with databases,

IIS Application– application for Microsoft Internet Information Server,

Addin– add-on for Visual Basic,

ActiveX Document Dll– ActiveX document in the form of a dynamically loaded library,

ActiveX Document Exe– ActiveX document as an application,

DHTML Application– application in the form dynamic web page,

VB Enterprise Edition Controls– a standard Windows application with the ability to use additional controls.

To view the next dialog tab New Project(New Project) click on the shortcut Existing(Existing) (Fig.2.2).


Fig.2.2. Existing Tab

New Project dialog


Tab Existing(Existing) allows you to open a saved project from any permanent or removable drive, or network device, for which first select the folder where it is located in the drop-down list Folder(Folder), then the project file name in the main window and click Open(Open).

Now let's go to the tab Recent(Recent) dialogue New Project(New project) (Fig.2.3).


Fig.2.3. Recent Tab

New Project dialog


Here you can quickly return to one of the most recently edited Visual Basic projects by simply clicking on the project file name and clicking Open(Open).

To continue to familiarize yourself with the Visual Basic programming environment interface, you should create a standard application project. In this case, most menu items and toolbar buttons will become available.

Return to tab New(New) (Fig.2.1) dialogue New Project(New Project), click on the icon Standard EXE (Standard application) and press the button Open(Open). After this, a new project will be created with the name Project1, which is assigned by default, and we will see the Visual Basic programming environment in the mode design(design) (Fig. 2.4).

Fig.2.4. Visual Basic programming environment

in design mode


We list the objects of the interface of the Visual Basic programming environment that are currently visible:

window title(Window Header)

menu bar(Menu Bar)

toolbar(Tool Bar)

control panel(Tool Box)

project guide(Project Explorer)

properties window(Properties Window)

form placement window(Form Layout Window).

Window title(Window Header) displays the name of the project being edited and the current operating mode of Visual Basic, as discussed above.

Menu bar(Menu Bar) provides the programmer with the opportunity to use a powerful arsenal of functions of the Visual Basic programming environment (Fig. 2.5).


Fig.2.5. Menu Bar Functions


Right below the menu bar, like many Windows applications, located toolbar(Tool Bar), the buttons of which duplicate the most frequently used Visual Basic menu items. The toolbar also displays reference Information about the position and size of the selected object, or the location of the cursor in the program text (Fig. 2.6).


Fig.2.6. Tool Bar Functions


On the left side of the Visual Basic window is control panel(Tool Box) (Fig.2.7).


Fig.2.7. Tool Box


Most of the controls are used for decoration appearance applications and user interactions. They will be discussed in detail in the corresponding chapter.

There is a window in the upper right corner project guide(Project Explorer) entitled as Project - Project1(Project – Project1). This window has a tree structure with a list of components that are part of the application being designed. Now the list contains one single component - a form named Form1 (Fig. 2.8).


Fig.2.8. Project Explorer


If you right-click on the name of an object in the project explorer, a context menu will appear from which you can select the desired action on this object, for example:

Print on a printer,

Because Visual Basic applications are built in a modular fashion, each project component is stored in a separate file.

Below is properties window(Properties Window), which has a title Properties – Form1(Properties – Form1) (Fig.2.9).


Fig.2.9. Properties Window


Using this window you can view or change property values various elements controls that are on the form, as well as the form itself. The properties can be very different: name, size, color, etc.

Under the window title there is a drop-down list containing a list of controls for the active form. Even lower, you can see a list of properties of the selected element in the form of a pair: the name of the property – its value. For example, property Caption(Title) matters Form1.

To make it more convenient to navigate a large amount of information, the list of properties has two tabs that allow you to select the sort order: Alphabetic(Alphabetical) or Categorized(By category). Finally, below the list of properties is a tooltip about the purpose of the property highlighted by the mouse click.

The next window we'll look at is form placement window(Form Layout Window) (Fig.2.10).


Fig.2.10. Form placement window

(Form Layout Window)


It displays a small copy of the worker Windows desktop with the form located on it exactly as it will be while the program is running. Particularly valuable is the ability to check the location of a form on the desktop at different resolutions.

All subwindows are docked to the edges of the main Visual Basic window. This type of interface is called (Multi-Document Interface). Early versions of Visual Basic used another variation called Single Document Interface, where each child window was independent.

If you want to install a different interface type, select the menu command Tools – Options…(Tools - Options...). A dialog will appear Options(Options), where you need to go to the tab Advanced(Optional) (Fig.2.11).


Fig.2.11. Advanced Tab

Options dialog


After checking the box SDI Development Environment(Single Document Development Environment) and button clicks OK, the interface type will change to SDI – Single Document Interface(Single Document Interface).

You can adjust the docking behavior of individual child windows when using MDI – Multiple Document Interface(Multi-Document Interface), for which you should go to the tab Docking(Attaching) the same dialogue (Fig. 2.12).


Fig.2.12. Docking Tab

Options dialog


Selecting the checkbox to the left of the name of the corresponding subwindow enables Visual Basic to snap to the edge of the main window, and clearing the checkbox disables it.

In addition to the interface objects of the Visual Basic programming environment, approximately in the middle of the screen (Fig. 2.4) there is a window for the created project, which has a title Project1 – Form1 (Form)(Project1 – Form1 (Form)), inside which is the displayed form of the future application (Fig. 2.13).


Fig.2.13. Form Form1

in the Project1 project window


The form is separate window future application. Elements can later be placed on its surface Windows management: buttons, text input fields, lists, checkboxes, etc.

The grid of small dots is visible only during application development and helps the programmer to proportionally place controls on the form.

The same form in runtime will look slightly different, without a guide grid for placing elements and a project window (Fig. 2.14).


Fig.2.14. Form Form1

in runtime


The form plays a key role in the vast majority of applications created with Visual Basic. In addition to the visible interface, it contains event-handling routines that are executed when form buttons are clicked, keystroked, and other user interactions with elements located on the form.

Most other components of the Visual Basic programming environment are also, to one degree or another, related to the form and contain its name in their name, so it is advisable not to rename the form to avoid the need to manually rename other program components.

Some Visual Basic interface windows are not visible by default. You can control their display on the screen using menu commands View(View). One of the most important is Code Window(Code window), in which the source code of the subroutines included in the application is entered.

Execute menu command View - Code(View – Code), after which you will see the code window (Fig. 2.15).


Fig.2.15. Code Window

(Code Window)


Another commonly used window is Object Browser(View objects), where you can not only view a hierarchical list of available objects, but also see brief information about their use in the program (Fig. 2.16).


Fig.2.16. Object Browser

(View objects)


The remaining windows of the Visual Basic programming environment will be discussed in the corresponding chapters.

2.2. Help about objects and their properties

There are quite a lot of objects in Visual Basic, and they have even more properties. Remembering everything is difficult, if not almost impossible, so you need to constantly use the built-in help.

The same goes for instructions and built-in functions of the Visual Basic language. Of course, they will be memorized gradually, but don’t worry if you don’t remember exactly the name or syntax of some instruction.

The context-sensitive help of the Visual Basic programming environment can be easily accessed on the screen using the key .

Help is called context-sensitive because the Help topic that is displayed depends on the current context, which is determined by the selection of any object and the name of the Visual Basic property, method, or statement within which the text cursor is positioned.

Let's try to use Visual Basic context help in practice. For example, we want to get detailed information about the control ProgressBar(Progress bar).

Select the button with the mouse ProgressBar(Progress bar) in ToolBox(Toolbox) and press the key . The Visual Basic Help window opens with information about this object(Fig.2.17).


Fig.2.17. Visual Basic Help - ProgressBar


At the top of the description text, just below the current section title, there are several menu options you can select to get more information:

See Also(See also) – information on related topics,

Example(Example) – text code examples,

Properties(Properties) – list of properties,

Methods(Methods) – list of methods,

Events(Events) – list of events.

Let's say that we need to familiarize ourselves with examples of programming a control ProgressBar(Progress bar).

In theory, we should have selected the item Example(Example). But in this case, this item is not available, since the code examples are located below. Therefore, move down the text using the vertical scroll bar until you see an example procedure using the control ProgressBar(Progress bar) (Fig.2.18).


Fig.2.18. Visual Basic Help - Example procedure text using ProgressBar

(Progress bar)


All examples that are available in the Visual Basic help system can be used as a sample, for which you can copy them into your program and edit them at your discretion.

Select the desired part of the code with the mouse and copy it to the Windows clipboard using the keyboard shortcut +[C] or + .

Go to your program and press the key combination +[V] or + to insert a code snippet.

While in the Visual Basic Help window, you can browse the contents of other topics, or use the index or search to find the Help topic you're looking for.

To find the information you need, click on the label Search(Search) located on the left side of the Help window. The tab will open Search(Search) Visual Basic help system (Fig.2.19).


Fig.2.19. Search Tab

Visual Basic Help


Click on the input field Search for following words(Search the following words) and enter keywords to search, using if necessary logical operators: And(AND), Or(Or), Near(Close), Not(Not). Entering these operators is possible not only manually, but also by selecting from a list called up by clicking on the button to the right of this field.

To start searching for help topics that contain the keyword combination you entered, click Topics(Sections) and wait for the search process to complete. Upon completion, a list of found partitions will be displayed on the left side of the window.

The help options described above are available from the Visual Basic menu. The help content is called by the command HelpContents…(Help - Contents), alphabetical index - the next paragraph HelpIndex…(Help - Index), and search - HelpSearch…(Help – Search). Using the menu Help You can also go to the Microsoft Web site for technical support.

2.3. First application in Visual Basic language

Now let's try out in practice the entire process of developing an application using Visual Basic using an example the simplest program Hello, World! (Hello World!). This application will only have two buttons: Greetings And Exit.

After pressing the button Greetings a dialog with a message should appear Hello World!, and after pressing the button Exit– the program exits.

The well-known Hello, World! (Hello, world!), usually written by all novice programmers taking their first steps in mastering a new programming language.

To continue the development process of this program, we will use the already created project with the default name Project1, just rename it first.

From the Visual Basic menu, select Project-Project1 Properties…(Project – Project1 Properties…). A settings dialog will appear on the screen (Project1 – Project Properties), opened on the tab General(General) (Fig.2.20).


Fig.2.20. General Tab

Settings dialog Project1 – Project Properties

(Project1 – Project properties)


Double click on the input field Project Name(Project name) and replace the existing text Project1 on new - HelloWorld. After that, click the button OK to close the settings dialog Project1 – Project Properties(Project1 – Project properties).

Let's rename the only form of our application. To do this, click anywhere on the form Form1, after which in the window Properties – Form1(Properties – Form1) a complete list of its properties will be displayed (Fig.2.21).


Fig.2.21. Properties Window – Form1


In the left column of the properties window, find the property called Name(Name) having the current value Form1. Property name Name(Name) is enclosed in parentheses because it is used slightly differently in Visual Basic code than all other properties.

Name(Name) in the right column and enter Hello World, then press the key .

Change the property value in the same way Caption(Headline). Instead of the old value Form1 enter new - Hello World! Please note that you can already use spaces, commas and other characters here. The form will take the following form (Fig.2.22).


Fig.2.22. HelloWorld form in the HelloWorld project window


How are these two properties different? Property Name(Name) is required for any Visual Basic object. It uniquely identifies or, in other words, points to this object so that it can be accessed from the program text. Form name Hello World can be seen in the title bar of the current project window: HelloWorld – HelloWorld (Form). This window is for informational purposes only and is only displayed in application development mode. The project window contains the form itself inside Hello World.

Property Caption(Title) is only available for those objects that can have a caption visible on the monitor screen, for example, for objects Form(Form), CommandButton(Command button) or Frame(Frame). Our form has the inscription Hello World! can now be seen in the title bar, where it will also be visible when the application runs.

Now everything is ready to continue designing the interface of our application, which consists of one form on which two buttons should be located. We already have the form. All that remains is to place buttons on it and write a subroutine for displaying a dialog with a message on the screen Hello World!

Placing a button on the surface of a form is similar to drawing a rectangle in standard Paint program. Click the tool icon CommandButton(Command button) of the control panel (Tool Box) and position the mouse pointer over the form, which will change to a cross:

Press the left mouse button and, while holding it in this position, move the mouse pointer a couple of centimeters to the right and down from this point, and then release the mouse button. A command button with the inscription will appear in the specified place on the form Command1.

Place the second button on the form in the same way. After this, it will take approximately this form (Fig. 2.23).


Fig.2.23. HelloWorld form with two command buttons


Of course, it is difficult to immediately establish the required sizes and location of buttons. But Visual Basic then allows you to change these settings in several ways that are common to all controls:

With a mouse control selected, you can drag it across the form's surface or resize it by grabbing one of the square handles around its perimeter.

To more accurately set the geometric dimensions of the control element, select it and install numeric values properties Left(Left), Top(Upper), Width(Width) and Height(Height) in Properties Window(Properties window). The first two properties indicate the coordinates of the control's upper left corner, and the last two properties indicate the width and height of the object.

Now let's change the labels on the buttons that should not be Command1 And Command2, A Greetings And Exit accordingly, and also give more informative names to these objects.

Click to highlight the button Command1, after which in Properties Window(Properties window) a list of its properties will be displayed.

Double click on the property value Caption(Heading) and instead Command1 enter Greetings. After this, the text on the button will immediately change.

Change the property in the same way Name(Name), setting it to a new value – cmdHello.

Perform similar actions with the button Command2, which has properties Name(Name) and Caption(Header) assign values cmdExit And Exit respectively.

At this point, the interface design stage ends, and we move on to actually writing the program text.

Double-click the button labeled Greetings. A code window will open with the already designed subroutine template cmdHello_Click, which will be called when you click on this button (Fig.2.24).


Fig.2.24. Code window with cmdHello_Click procedure


The Visual Basic code window, in addition to the main part intended for entering the text of individual subroutines and global form settings, contains two drop-down lists.

In the left list you can select a control element, and in the right list - the type of event to respond to which you plan to write a subroutine in Visual Basic.

After selecting both values ​​in the drop-down lists, an empty procedure for handling this event will be automatically created in the main part of the code window.

In this case, the desired control and event type are already selected. This is a button with the name cmdHello and event Click(Click). The text of an empty event procedure looks like this:



Keyword Sub, abbreviation for Subroutine(subroutine), serves for ads(Declaration) procedures cmdHello_Click, which handles the button click event cmdHello.

The name of the procedure consists of the name of the object, the underscore character " _ " and the type of the event. It is this name that indicates to Visual Basic that this procedure is associated with the event Click(Click) buttons cmdHello. Empty parentheses tell us that no parameters are passed to the procedure.

Modifier Private(Private) indicates that this procedure is visible only within its container, in this case the form. The instructions complete the procedure. End Sub.

Of course, an empty procedure does absolutely nothing - in order for it to do anything, you need to write at least one line of program inside the body of the procedure.

From this moment the process begins coding(Coding), which is the writing of the text of a program that performs the desired functionality.

Coding is the central link of any programming system. Visual Basic is no exception, although it allows you to minimize your coding time.

All we have to do is enter a single Visual Basic language instruction inside the procedure, which displays a dialog with a message Hello World!:


MsgBox("Hello world!")

The finished procedure will then look like this:


Private Sub cmdHello_Click()

MsgBox("Hello world!")


Then double click on the second button that says Exit. The procedure for processing the event of clicking on this button will open.

In the body of the procedure cmdExit_Click enter instructions End, which performs the function of shutting down the application when an event occurs Click(Click).

The text of the second procedure will now look like this:


Private Sub cmdExit_Click()


That's all! All that remains is to admire the work done, checking the functionality of the written code.

To check the viability of the created program, click on the button Start(Launch) toolbar or press the key .

Visual Basic goes into run mode and a running Hello, World! application appears on the screen. (Hello, world!) (Fig. 2.25).


Fig.2.25. Working application

Hello, World! (Hello World!)


Make a few clicks on the button labeled Greetings. A dialogue will appear with the required text message(Fig.2.26).


Fig.2.26. Dialogue with text message

Hello, World! programs (Hello World!)


Admire it with the pride of the creator of your first computer program and press the button OK to close the dialog.

Now click on the button Exit. The program exits and Visual Basic returns to design mode.

2.4. Saving a project to disk and loading it

to continue programming

The author has no doubt that creating applications using Visual Basic will captivate you and there will be a need to save the project on disk, as well as then load it to continue working. This is very easy to do, similar to saving Microsoft document Office.

Just click the button Save Project(Save Project) toolbar (Tool Bar), or select the menu command FileSave Project(File – Save Project) to save all project components to disk. Visual Basic will then prompt you to save the individual components one by one. The dialog will appear first Save File As(Save file as) with a request to save the form (Fig.2.27).


You can edit the suggested form name in the input field File Name(File name) or leave it unchanged. Then click the button Save(Save). After saving the form, a dialog will appear Save Project As(Save project as) with a request to save the project (Fig.2.28).


This will complete saving the project. When you save again, you don’t have to do all the steps again - just press the button Save Project(Save Project) toolbar.

There is another more flexible option for saving individual project components, which may be necessary, for example, if changes to other components do not need to be saved.

Expand the thread Forms(Forms) by clicking on the icon. Here we will see the name of the form Hello World(Fig.2.29).


Fig.2.29. HelloWorld Project Components Overview

in the Project Explorer


Right-click on the form name. A context menu will appear, with which you can perform all valid actions on this object (Fig. 2.30).


Fig.2.30. Context menu

in the Project Explorer


Select menu item Save HelloWorld.frm(Save HelloWorld.frm) to save the form to disk.

To configure the automatic saving mode of the project, use the menu item Tools – Options(Tools - Options). A dialogue will open Options(Options). Click on the tab Environment(Wednesday) (Fig.2.31).


Fig.2.31. Environment Tab

Options dialog


Switch When a program starts(When the program starts) set to Save Changes(Save changes) and click the button OK.

That's it for saving the project. Now let's try loading the saved project from disk.

To open a project, select the menu command File – Open Project…(File – Open project...). A dialogue will appear on the screen Open Project(Open Project) expanded in the tab Existing(Existing) (Fig.2.32).


Fig.2.32. Existing Tab


First in the dropdown list Folder(Folder) select the drive and directory where the project is located, then click on the file name and click the button Open(Open).

If you have already worked with a saved project before, then it is more convenient to open it a little differently. Click on the shortcut to go to the tab Recent(Recent) dialogue Open Project(Open project) (Fig.2.33).


Fig.2.33. Recent Tab

Open Project dialog


On this tab you don't need to select a folder, just click on the project name and click the button Open(Open).

One way or another, we have opened the desired project, but Visual Basic itself will not display the forms on the screen for editing; for this they need to be displayed deliberately.

To display it, double-click on the form name Hello World in the Project Explorer window (Fig. 2.29), or first right-click on it, and then select the item from the context menu that appears (Fig. 2.30) View Object(View object).

In 1964, the BASIC language was born. It was developed for educational purposes so that university students could learn programming using simple algorithms. BASIC was a real breakthrough in education, and in real life, programmers competed in creating dialects. Among the geeks fascinated by the language was Bill Gates. At the age of 13, he wrote his first program on it, and at 20, he made money by implementing a version of Altair BASIC, which he developed together with Paul Allen.

In the 80s, the popularity of BASIC began to decline. New programming languages ​​have gotten rid of complex syntax, while their capabilities have increased. And only Microsoft continued to stick to its line, releasing first another bestseller QBasic, and then the hero of today's text - Visual Basic.

Brief information

The first versions of Visual Basic were not successful among developers. The graphical interface, and then syntax highlighting, were perhaps the only differences from QBasic. Microsoft only started making significant changes with version 3.0. In the Professional configuration, developers were able to work with the database Access data, graphic capabilities allowed even non-programmers to work with the language. In addition, performance has increased and software installation has been simplified.

In 1995, Windows 95 was released, along with which Visual Basic 4.0 set off to seize world domination. A full-fledged compiler appeared here, the ability to create 32 and 16-bit applications, and the speed of operation increased. Released in 1998 latest version traditional Visual Basic. Next, Microsoft changed the concept, launching the development of the language in two directions: VB on the .NET platform and adapted versions for solving local problems (VBA, VBScript, eVB).

Visual Basic today

VB is a tool for quickly creating Windows applications. After changing the concept, the language acquired:

  • OOP support with constructors, destructors and inheritance;
  • free multithreading;
  • compilation into bytecode executed using the CLR;
  • structured exception handling;

But the main convenience is the .NET platform. This means that developers now have access to a huge number of platform libraries for working with the database, graphics, forms, security and web.

Another popular successor is Visual Basic for Applications (VBA). This is a stripped-down VB 6.0 for processing macros, creating scripts, which simplifies work within the OS.

In AutoCAD, CorelDraw, SolidWorks and, of course, Microsoft Office, you can export data, structure information, and connect a database. Even at the everyday level, you will need the ability to create auto-filled forms and extract information in one click from Excel. For work that doesn't necessarily involve programming, VBA makes it easier to create technical documents, drawings, and plans.

Literature

It is better to start learning the language with VBA. Firstly, it is simpler, secondly, the target products are well known to you, and thirdly, the restrictions will allow you to concentrate on the language. It is estimated that you will need 30 hours to learn VBA. In fact, no more than 15. You can use one of the following books as a basis:

  • Excel 2013. Professional VBA Programming, John Walkenbach.
  • Solving scientific and engineering problems using Excel, VBA and C C++, Igor Gaidyshev.