VB.NET Basics PDF
Document Details
Uploaded by PicturesqueSugilite5089
Tags
Summary
This document provides a tutorial on Visual Basic 2010, covering basic concepts like controls and how to create simple applications, such as a calculator program.
Full Transcript
Computer Programming 6 VB.Net Basics Visual Basic 2010 is the latest version of Visual Basic launched by Microsoft in 2010. Visual Basic 2010 is a full fledged Object-Oriented Programming (OOP) Language, so it has caught up with other OOP languages such as C++, Java,C# and others. However, you don’...
Computer Programming 6 VB.Net Basics Visual Basic 2010 is the latest version of Visual Basic launched by Microsoft in 2010. Visual Basic 2010 is a full fledged Object-Oriented Programming (OOP) Language, so it has caught up with other OOP languages such as C++, Java,C# and others. However, you don’t have to know OOP to learn VB2010. Visual Basic 2010 Express Edition is available free for download from the Microsoft site. Go to the link http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-basic-express The Integrated Development Environment when you launch VB2010 Express is shown in the diagram below. The IDE Start Page consists of a few sections, namely: -The New Project/Open Project section. -The Recent Projects section shows a list of projects that have been created by you recently. -The Getting Started Pane- It provides some helpful tips to quickly develop your applications. -The Latest News section- It provides latest online news about Visual Basic 2010 Express. It will announce new releases and updates The Properties section-let you defines the properties of each control To start creating your first application, you need to click on New Project. The following VB2010 New Project dialog box will appear. The dialog box offers you five types of projects that you can create. As we are going to learn to create windows Applications, we will select Windows Forms Application. At the bottom of this dialog box, you can change the default project name WindowsApplication1 to some other name you like, for exampe, myFirstProgram. After you have renamed the project, click OK to continue. The following IDE Windows will appear, it is almost similar to Visual Basic 6. It consists of an empty form, the toolbox tab and the properties. The layout is slightly different from vb2008 as the Toolbox is not shown until you click on the Toolbox tab. When you click on the Toolbox tab, the common controls Toolbox will appear. Now drag the button control into the form, and change its default Text Button1 to OK in the properties window, the word OK will appear on the button in the form, as shown below: Now click on the OK button and the code window appears. Enter the code as follows: When you run the the program and click on the OK button, a dialog box will appear and display the “WELCOME TO VISUAL BASIC 2010″ message,as shown below: There you are, you have created your first VB2010 program. Now let’s go know the Controls Controls in Visual Basic 2010 are tools that can be placed in the form to perform various tasks. We can use them to create all kinds of Windows applications. The diagram below shows the Toolbox that contains the controls of Visual Basic 2010. They are categorized into Common Controls, Containers, Menus, Toolbars, Data, Components, Printings and Dialogs. At the moment, we will focus on the common controls. Some of the most used common controls are Button, Label, ComboBox, ListBox, PictureBox, TextBox and more. To insert a control into your form, you just need to drag the control from the tool box and drop it into the form. You can reposition and resize it as you like. Let’s examine a few examples that made use of Button, Label, TextBox , ListBox and PictureBox. CODING When you click on the Toolbox tab, the common controls Toolbox will appear. Next I will show you how to create a simple calculator that adds two numbers using the TextBox control. In this program, you insert two text boxes , three labels and one button. The two text boxes are for the users to enter two numbers, one label is to display the addition operator and the other label is to display the equal sign. The last label is to display the answer. Now change the label on the button to Calculate,then click on this button and enter the following code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim num1, num2, product As Single num1 = TextBox1.Text num2 = TextBox2.Text product = num1 + num2 Label1.Text = product End Sub When you run the program and enter two numbers, pressing the calculate button will allows the program to add the two numbers. Basic ToolBox Function Labels - The Labels are used to display text information Button - It is made use of as a typical Windows Button. Most of the time, the Switch Control is utilized to generate a click event, its name, dimension as well as look are not altered in the runtime. Text Box - As you can guess, it is made use of to accept textual input from the individual. The individual can include strings, numerical worths, and a mix of those, but Images and also other multimedia material are not supported. ListBox - As the name recommends, this control works as a way to present a list of items on the application. Individuals can select any kind of choice from the list. Combo Box - It is similar to the checklist but it functions as a dropdown for the user. An individual can get both text in the box or he can click the downwards aero on the appropriate side and choose any kind of product. Checkbox - Checkboxes resemble radio switches in the manner in which they are also used in groups, however, a user can choose more than one item in the group. There are more things but we're here for the basics. Before writing an event procedure for a control in Visual Basic 2010 to response to a user’s input, you have to set certain properties for the control to determine its appearance and how it will work with the event procedure. You can set the properties of the controls in the properties window at design time or at runtime. This is a typical properties window for a form in Visual Basic 2010 IDE: The title of the form is defined by the Text property and its default name is Form 1. To change the form’s title to any name that you like, simple click in the box on the right of the Text property and type in the new name, in this example, the title is Addition Calculator. Notice that this title will appear on top of the window. In the properties window, the item appears at the top part is the object currently selected (in picture below, the object selected is Form1). At the bottom part, the items listed in the left column represent the names of various properties associated with the selected object while the items listed in the right column represent the states of the properties. Properties can be set by highlighting the items in the right column then change them by typing or selecting the options available. You may also alter other properties of the form such as font, location, size, foreground color, background color ,MaximizeBox, MinimizeBox etc. You can also change the properties of the object at runtime to give special effects such as change of color, shape, animation effect and so on. For example Here are some of the common colors and the corresponding RGB codes. You can always experiment with other combinations, but remember the maximum number for each color is 255 and the minimum number is 0.