In Visual Basic 2015, numeric data types are types of data comprises numbers that can be calculated mathematically. Some examples of numeric data types are examination marks, height, body weight, the number of students in a class, share values, the price of goods, monthly bills, fees, bus fares and more. Visual Basic Code Snippets and Utilities In the following section, you can find a variety of Visual Basic code samples. For every sample, you can download the entire project for opening in Visual Basic 6.0. Revealing the passwords behind asterisks in Internet Explorer.
[Lesson 3]<<[Contents]>>[Lesson 5]
In lesson 3, we have learned how to design the user interface by adding controls to the form. Besides that, we have also learned how to change their properties. However, the controls on the user interface alone will not work without adding code to them. In this lesson, we shall learn how to write Visual Basic 2015 code for all the controls. The code is necessary for a control to respond to events triggered by the users. Before learning how to write Visual Basic 2015 code, let us dwell into the concept of event-driven programming.
Visual Basic 2015 Community Download
4.1 The Concept of Event-Driven Programming
Visual Basic 2015 is an event-driven programming language, which means that the code is executed in response to events. We have learned how to design the interface by putting controls(objects) on the form, but they do nothing unless we write code for the objects to respond to events triggered by the user. Every control you place on the form has a set of events related to them.
Some of the events are load, click, double click, drag, and drop, pressing the keys and more.
To view the events, double-click the control (object) on the form to enter the code window. The default event will appear at the top part on the right side of the code window. You need to click on the default event to view other events associated with the control. The code appears on the left side is the event procedure associated with the load event. The event procedure load associated with the default form is shown in Figure 4.1.
Figure 4.1: Events associated with Form
The events associated with the button is displayed in Figure 4.2
Figure 4.2: Events associated with the button control
4.2 Writing the Code
To start writing code in Visual Basic 2015, click on any part of the form to go into the code window as shown in Figure 4.1. This is the structure of an event procedure. In this case, the event procedure is to load Form1 and it starts with Private Sub and ends with End Sub. This procedure includes the Form1 class and the event Load, and they are bound together with an underscore, i.e. Form_Load. It does nothing other than loading an empty form. To make the load event does something, insert the statement
MsgBox (“Welcome to Visual Basic 2015″)
Figure 4.3: The code window
When you run the program, a message box that displays the text “My First Visual Basic 2015 Program” will appear, as shown in Figure 4.4. MsgBox is a built-in function in Visual Basic 2015 that display a message in a pop-up message box.
Figure 4.4: The popup message
* You will notice that above Private Sub structure there is a preceding keyword Public Class Form1. This is the concept of an object-oriented programming language. When we start a windows application in Visual Basic 2015, we will see a default form with the name Form1 appears in the IDE, it is actually the Form1 Class that inherits from the Form class System.Windows.Forms.Form. A class has events as it creates an instance of a class or an object.
You can also write code to perform an arithmetic calculation. For example, you can use the MsgBox and the arithmetic operator plus to perform an addition of two numbers, as shown below:
Figure 4.5: Writing the code
*The symbol & (ampersand) is to perform string concatenation. The statement Me.Close() is to close the program after clicking the OK button.
The output is as shown in Figure 4.6
Figure 4.6: Output
We will learn more about code writing in coming lessons
[Lesson 3]<<[Contents]>>[Lesson 5]
PLEASE READ:
NOTE 1: Microsoft introduced a NASTY bug after release that is causing errors in sample code. Please read the Teach Yourself Visual Basic 2015 errata page – item #1 in particular – for details on how to work around this.
Note 2: I get a lot of questions from people looking for the sample database file referenced in hour 21. That database IS in the sample files listed below. Simply extract the appropriate zip file, and look in the Hour 21 folder that gets created.
Teach Yourself Visual Basic 2015
Click the title above to get sample files for Teach Yourself Visual Basic 2015
Visual Basic 2015 Download
Teach Yourself Visual Basic 2012
Get sample files for Teach Yourself Visual Basic 2012
Teach Yourself Visual Basic 2010
Get sample files for Teach Yourself Visual Basic 2010