By Dr. HANY ELSALAMONY 1by Dr. HANY ELSALAMONY
Microsoft released Visual Basic in It was the first visual development tool from Microsoft, and it was to compete with C, C++, Pascal and other well-known programming languages. From the start, Visual Basic wasn't a hit. It wasn't until release 2.0 in 1991 that people really discovered the potential of the language, and with release 3.0 it had become the fastest-growing programming language on the market. 2by Dr. HANY ELSALAMONY
Programmers have undergone a major change in many years of programming various machines. For example what could be created in minutes with Visual Basic could take days in other languages such: as "C" or "Pascal". Visual Basic provides many interesting sets of tools to aid you in building exciting applications. Visual Basic provides these tools to make your life far more easier because all the real hard code is already written for you. 3by Dr. HANY ELSALAMONY
With controls like these you can create many applications which use certain parts of windows. For example, one of the controls could be a button, which we have demonstrated in the "Hello World" program below. First create the control on the screen, then write the code which would be executed once the control button is pressed. With this sort of operation in mind, simple programs would take very little code. Why do it like the poor old "C" programmer who would have to write code to even display a window on the screen, when Visual Basic already has this part written for you. 4by Dr. HANY ELSALAMONY
Even though people tend to say Visual Basic's compiler is far behind the compilers of Pascal and C, it has earned itself the status of a professional programming language, and has almost freed BASIC of the reputation of a children's language. Overall you would class Visual Basic as a Graphics User Interface(GUI). Because as you draw, you write for the program. This must always be remembered in any kind of creation of a Visual Basic program. All in all, VB is the preferred language of many future program means. If you want to start programming Windows, and don't know how to start, give Visual Basic a shot. 5by Dr. HANY ELSALAMONY
A programming language is a set of commands and command options, called arguments, that you use to give instructions to the computer. Computers cannot (yet) understand human languages because people deal well with ambiguous commands, and a computer cannot understand such ambiguity. A programming language must be more precise than a spoken language. Visual Basic is not only a programming language, but also a complete graphical development environment. This environment allows users with little programming experience to quickly develop useful Microsoft Windows applications which have the ability to use OLE ( Object Linking and Embedding ) objects, such as an Excel spreadsheet. 6by Dr. HANY ELSALAMONY
Visual Basic also has the ability to develop programs that can be used as a front end application to a database system, serving as the user interface which collects user input and displays formatted output in a more appealing and useful form than many SQL versions are capable of. Programs are often comprised of several files that interact with one another, so you’ll often see the term application used as a synonym for all of a program’s files. The program, or application written in a programming language, is a set of instructions that directs the computer. 7by Dr. HANY ELSALAMONY
Visual Basic's main selling point is the ease with which it allows the user to create nice looking, graphical programs with little coding by the programmer, unlike many other languages that may take hundreds of lines of programmer keyed code. As the programmer works in the graphical environment, much of the program code is automatically generated by the Visual Basic program. In order to understand how this happens it is necessary to understand the major concepts, objects and tools used by Visual Basic. 8by Dr. HANY ELSALAMONY
Listing 1.1. Early BASIC programs had line numbers and were somewhat cryptic. 10 REM This program computes and prints the first ten squares 20 CLS 30 PRINT “Squares from 1 to 10” 40 PRINT “Value”, “Squared” 50 FOR N = 1 TO 10 60 PRINT N, (N*N) 70 NEXT N 80 PRINT 90 END 9by Dr. HANY ELSALAMONY
Squares from 1 to 10 Value Squared 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 by Dr. HANY ELSALAMONY
An interpreted language, such as BASIC, lets you run programs as you write them. Interpreted languages make good learning platforms because of their quick feedback. A compiled language requires extra steps, called compilation and linking, before the programmer can run the program. The compiled program resides in the computer’s own native language and not in the programming language that the programmer originally used. 11by Dr. HANY ELSALAMONY
The term " Personal Programming " refers to the idea that, wherever you work, whatever you do, you can expand your computer's usefulness by writing applications to use in your own job. Personal Programming is what Visual Basic is all about. Using Visual Basic's tools, you quickly translate an abstract idea into a program design you can actually see on the screen. VB encourages you to experiment, revise, correct, and network your design until the new project meets your requirements. However, most of all, it inspires your imagination and creativity. 12by Dr. HANY ELSALAMONY
Over time you’ll find your own way of writing programs that works best for you. Nevertheless, you’ll generally follow these standard set of steps when creating your Visual Basic programs: 1. Decide what your application is to do by creating an overall design. 2. Create the visual portion of your application (the screens and menus that your users will interact with). 13by Dr. HANY ELSALAMONY
3. Add Visual Basic programming language code to tie the visual elements together and to automate the program. 4. Test your application to locate and remove any bugs you find. 5. Compile your tested application and distribute the compiled application to your users. 14by Dr. HANY ELSALAMONY
Visual Basic can and is used in a number of different areas, for example: Education Research Medicine Business Commerce Marketing and Sales Accounting Consulting Law Science 15by Dr. HANY ELSALAMONY
16by Dr. HANY ELSALAMONY
17by Dr. HANY ELSALAMONY
Changing the form’s BackColor property by Dr. HANY ELSALAMONY18
19by Dr. HANY ELSALAMONY
20by Dr. HANY ELSALAMONY
21by Dr. HANY ELSALAMONY
Once all your objects are created, you can combine them to form a single executable program that can be run outside of the Visual Basic environment, in Microsoft Windows. 22by Dr. HANY ELSALAMONY
First thing to do is to create a Directory where you will store all your VB Projects. Call it VB Apps, for example. Then start VB. The first screen will ask whether you want to open a new project or an existing one - it's obviously a new one and it will be a Standard EXE. Then, maximize all the windows Now, save your project. It will first ask you to save the form - call it Score.frm - and then the Project - call it Scorebrd.vbp. From now on, do File-->Save Project very, very frequently. 23by Dr. HANY ELSALAMONY
24by Dr. HANY ELSALAMONY
Visual Basic Express Edition enables you to create several types of applications. This includes Windows Forms applications, Windows Presentation Foundation applications, console applications, and class libraries. Each application that you create is contained in its own project, and project templates are provided to help you get started. When you create a new project, icons in the New Project dialog box and Add Project dialog box represent the available project types and their templates. by Dr. HANY ELSALAMONY25
The following project templates are available when you open a new project in Visual Basic Express Edition. Although there is no project template for Windows control libraries, you can still create your own controls by using the Class Library template. by Dr. HANY ELSALAMONY26
Project TemplateUsed to create Windows Application Template Used to create Windows-based applications that run locally on users' computers. You can build anything from a simple single-window tool like Windows Calculator to a complete application that has multiple windows and advanced capabilities. Class Library Template Used to create reusable classes or components that can be shared with multiple projects. Console Application Template Used to create command-line applications, programs that run from a Windows command prompt and have no visual interface. WPF ApplicationUsed to create stand-alone Windows Presentation Foundation applications. WPF Browser Application Used to create browser-hosted Windows Presentation Foundation applications. by Dr. HANY ELSALAMONY27
28by Dr. HANY ELSALAMONY
Before you start to build-up the form, it will make it easier if you change the color of the form. Otherwise you will be working with grey controls on a grey background. To change the color, just click anywhere on the form, go to the properties window, find the property called BackColor and change it to the standard Window background (teal) or to any color you want in the palette. 29by Dr. HANY ELSALAMONY
In our first example we will need 6 labels and 2 command buttons. Each one of these objects that you put on a Form is called a control. To get a control you go to the Toolbox, click on the control you want, come back to the Form and click and drag the control to the size and position you want. Position the controls somewhat like in the diagram below. 30by Dr. HANY ELSALAMONY
31by Dr. HANY ELSALAMONY
Now that we have a bunch of controls on the form, we have to jazz them up a bit. We do this by changing the Properties of the controls in the Properties window. Each control has a whole series of properties, most of which we won't need right now. The ones we do need are: Alignment = how text aligns in the control BackColor = choose the color of the background Caption = the text that will appear in the control Font = choose the font type and size ForeColor = choose the color of the text (foreground) 32by Dr. HANY ELSALAMONY
As with all Windows applications, you can select multiple controls with (Ctrl)+(Click) and change a property for all of them at once. For example, if all backgrounds are white, select all controls, change ForeColor to white and all of them are modified. Change your form to look like the one below. Note that you do not have to change the Caption for Label4, Label5 and Label6 and that you can't change the color of the buttons. They insist on being what was called in the old days "IBM grey". Don't forget to save your project often as you go along! 33by Dr. HANY ELSALAMONY
34by Dr. HANY ELSALAMONY
35by Dr. HANY ELSALAMONY
If you Run the application at this point, you should see your Form appear, just the way you created it. However if you click on any of the controls, absolutely nothing happens! There are events that occur; the form opens, a button is clicked, etc. But, there is nothing that tells the form what to do when it sees an event. That is why we have to write code, also called script. 36by Dr. HANY ELSALAMONY
37by Dr. HANY ELSALAMONY
To switch between the Code window and the Form window, use the buttons just over the Project Explorer window (diagram on the left). Once in the Code window, you have the option of seeing all the code for the Project or the code for one event at a time. Use the buttons in the lower left-hand corner (diagram on the right). To select the object and the event you wish to code, use the two Listboxes at the top of the Code window. The one on the left for the object and the one on the right for the event. Start with General... Declarations and then Form... Load, etc. 38by Dr. HANY ELSALAMONY
At this point you might want to download the sample program and study it. In the following lessons we'll add functionality to the exercice and we'll explain what the code means. But for the moment, a good exercice would be to write part of the code and then try to figure out how to improve certain aspects of the program. 39by Dr. HANY ELSALAMONY
40by Dr. HANY ELSALAMONY
41by Dr. HANY ELSALAMONY
Now we can Run it and see something happen. When the Form loads, it will initialize the fields that we specified in the code. Now code the Command1 button and Run it to see the result. 42by Dr. HANY ELSALAMONY
The language you write and speak has structure: for example, a book has chapters with paragraphs that contain sentences consisting of words. Programs written in Visual Basic also have a structure: modules are like chapters, procedures are like paragraphs, and lines of code are like sentences. by Dr. HANY ELSALAMONY43
When you speak or write, you use different categories of words, such as nouns or verbs. Each category is used according to a defined set of rules. In many ways, Visual Basic is much like the language that you use every day. Visual Basic also has rules that define how categories of words, known as programming elements, are used to write programs. by Dr. HANY ELSALAMONY44
Programming elements in Visual Basic include statements, declarations, methods, operators, and keywords. As you complete the following lessons, you will learn more about these elements and how to use them. Written and spoken language also has rules, or syntax, that defines the order of words in a sentence. by Dr. HANY ELSALAMONY45
Visual Basic also has syntax—at first it may look strange, but it is actually very simple. For example, to state "The maximum speed of my car is 55", you would write: Car.Speed.Maximum = 55 You will learn more about syntax later, and tools in Visual Basic such as IntelliSense provide you with guidance in using the correct syntax when you write programs. by Dr. HANY ELSALAMONY46
Hello World! Example Program Description This program demonstrates the text output and button control functions of the Visual Basic programming language. By clicking on the button "Hello World", the message "Hello world" is displayed in the upper left hand corner. 47by Dr. HANY ELSALAMONY
48by Dr. HANY ELSALAMONY
The first program that students traditionally write when they learn a new language is a program that simply writes a message to the computer screen which reads “Hello, world!” You can create a ‘Hello World’ program by following these instructions: Open Visual Basic Express Edition – on the Start menu, point to All Programs, and then click Microsoft Visual Basic 2005 Express Edition. On the File menu, click New Project. In the Visual Studio installed templates list, click Console Application, enter My First Application in the Name box, and then click OK. Visual Basic will create an outline program for you, and display the code for that program in the code window. Insert the following line of code (underlined below) into the outline program in the code window: 49by Dr. HANY ELSALAMONY
Module Module1 Sub Main() Console.WriteLine("Hello, world!") End Sub End Module 50by Dr. HANY ELSALAMONY
Before you run the program, you should customize the Visual Basic 2005 Express Edition menu so that the option that we use will be available. Take the following steps to customize the menu: On the Tools menu, click Customize. Click to show the Commands tab. In the Categories list, click Debug, then scroll the Commands list until you can see the Start Without Debugging command. Drag the Start Without Debugging command over the Debug menu, move the insertion point below the Start / Continue command (as shown below), and then release. 51by Dr. HANY ELSALAMONY
52by Dr. HANY ELSALAMONY
Click the Close button to dismiss the Customize dialog. On the Debug menu, click Start Without Debugging (or just press Ctrl-F5 ). A command prompt window appears that looks like this: 53by Dr. HANY ELSALAMONY
Note The reason that you should use Start Without Debugging is so that the Press any key to continue message is displayed after your program has finished running. You can also use the Start Debugging menu option (or press F5 ) – as shown on the video that accompanies this article – but then the console window is closed as soon as your program has finished running, and you don’t get the chance to read what’s displayed on the console. 54by Dr. HANY ELSALAMONY
Visual Studio detected that it needed to ‘build’ the program, because this is the first time the program has been run. To build the program, Visual Studio invoked a special kind of program called a compiler, which understands the Visual Basic language and knows how to convert it into machine code. The compiler converted the ‘source code’ for your program (which is just a plain text file containing the Visual Basic code you wrote) into machine code which the computer can understand. 55by Dr. HANY ELSALAMONY
Once the compiler finished, Visual Studio took the compiler’s output – which is your program – and ran that program. Because your program is a console application, Visual Studio actually launched a console window and ran the program inside that window. Once your program had written “Hello, world!” on the screen and finished, Visual Studio displayed the “Press any key” message so that you could see what your program had displayed before the window closed. You could do all these steps yourself, of course, but it’s much easier to use the Visual Studio Integrated Development Environment (IDE) to do them on your behalf. However, it’s important that you understand what’s going on behind the scenes. 56by Dr. HANY ELSALAMONY
All the code in the file is contained within two statements (shown below) that indicate that we are creating a code module. A module is a general term for a file containing code or information that you add to your project. Usually, a module contains program code which you write. Along with classes (which you will learn more about shortly), modules are the basic units of code in a Visual Basic program. 57by Dr. HANY ELSALAMONY
Modules and classes are containers for your code. You must have at least one module or class in a program, but most programs have several. In the example below, everything between the two statements is inside a module called Module1. Module Module1 ... End Module 58by Dr. HANY ELSALAMONY
Inside the module there is a block of code called a subroutine. Modules and classes can contain subroutines, but subroutines are the blocks of code that do much of the actual work in a program. You use the Sub keyword to start a subroutine, and the End Sub keyword to end it. Everything in between is part of the subroutine. 59by Dr. HANY ELSALAMONY
Sub Main() ... End Sub In this case, the program declares a subroutine called Main. We’ll take a deeper look at subroutines later, but for now you need to know that the Main subroutine has a special place in a program, because it’s usually where the program starts running. You can write your subroutines in any order in the source code, but the Main method will be where your program starts. 60by Dr. HANY ELSALAMONY
Inside the Main subroutine, we have the code which actually writes the message to the screen: Console.WriteLine("Hello, world!") In this case, we use a class called System.Console, which is part of the.NET Framework. Inside the System.Console class there is a subroutine called WriteLine, which writes a message to the console screen (“Write”) and skips to the next line (“Line”). 61by Dr. HANY ELSALAMONY
we create a program that displays the text “ Welcome to Visual Basic!” The program consists of a single form that uses a label control (i.e., a control that displays text which the user cannot modify) and a picture box to display the image. 62by Dr. HANY ELSALAMONY
63
by Dr. HANY ELSALAMONY64
by Dr. HANY ELSALAMONY65
Setting the form’s Text property by Dr. HANY ELSALAMONY66
by Dr. HANY ELSALAMONY67 Adding a label to the form
by Dr. HANY ELSALAMONY68 GUI after the form and label have been customized
by Dr. HANY ELSALAMONY69 Properties window displaying the label’s properties
by Dr. HANY ELSALAMONY70 Font dialog for selecting fonts, styles and sizes.
by Dr. HANY ELSALAMONY71 Centering the label’s text.
Add a picture box to the form. by Dr. HANY ELSALAMONY72 Inserting and aligning the picture box.
by Dr. HANY ELSALAMONY73 Image property of the picture box
by Dr. HANY ELSALAMONY74 Selecting an image for the picture box.
by Dr. HANY ELSALAMONY75 Picture box displaying an image
by Dr. HANY ELSALAMONY76
As an aside – how is it that the class is called System.Console but we just call it Console in our program? Well, Visual Basic enables you to use abbreviations if you use certain classes regularly. In this case, the Console class is part of a group of classes in a namespace called System. A namespace is a way of grouping several classes or modules together. 77by Dr. HANY ELSALAMONY
The diagram shows the structure of the System namespace, and how it contains many classes. Because the System namespace contains commonly used classes, Visual Basic automatically creates the abbreviation for you, so that you only need to type the name of the class and not the full name including the namespace. 78by Dr. HANY ELSALAMONY