Engineering

Introduction to Visual Basic.NET

Description
24785_CH02_BRONSON.qrk 11/10/04 12:44 PM Page 45 Introduction to Visual Basic.NET 2.1 Elements of a Visual Basic Application 2.2 Getting Started in Visual Basic 2.3 Adding an Event Procedure 2.4 Adding
Categories
Published
of 80
340
Categories
Published
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Similar Documents
Share
Transcript
24785_CH02_BRONSON.qrk 11/10/04 12:44 PM Page 45 Introduction to Visual Basic.NET 2.1 Elements of a Visual Basic Application 2.2 Getting Started in Visual Basic 2.3 Adding an Event Procedure 2.4 Adding Controls 2.5 Adding Additional Event Procedures 2.6 Focus on Program Design and Implementation: Creating a Main Menu 2.7 Knowing About: The Help Facility 2.8 Common Programming Errors and Problems 2.9 Chapter Review Goals 24785_CH02_BRONSON.qrk 11/10/04 12:44 PM Page Chapter 2: Introduction to Visual Basic.NET In this chapter we begin learning about the fundamentals of programming and Visual Basic.NET. First we examine the two elements that are required by every practical Visual Basic program: the screens and instructions seen by the user, and the behind the scenes processing that is done by the program. We then present the basic design windows that you must be familiar with to produce such programs. Finally, we show you how to use these design windows to create the visual user interface, or GUI, and then add processing instructions. 2.1 Elements of a Visual Basic Application Visual Basic was initially introduced in 1991 as the first programming language that directly supported programmable graphical user interfaces using language-supplied objects. From that time until 2002, there were five other versions released, each version having features that increased the power of the language. In 2001, Microsoft released the.net (pronounced dot net ) platform. Visual Basic.NET, or VB.NET, is an upgrade to the last version of VB (version 6.0) that conforms to the.net platform. As you will see in subsequent chapters, the changes in VB.NET allow programmers to write Web or desk-top applications within the same language. In addition, VB.NET is fully object-oriented as opposed to prior versions that had many, but not all, of the elements of an object-oriented language. This book is based on VB.NET. In the balance of the book we will sometimes refer to Visual Basic as VB, omitting.net. From a programming viewpoint, Visual Basic is an object-oriented language that consists of two fundamental parts: a visual part and a language part. The visual part of the language consists of a set of objects, while the language part consists of a high-level procedural programming language. These two elements of the language are used together to create applications. An application is simply a Visual Basic program that can be run under the Windows operating system. The term application is preferred to the term program for two reasons: one, it is the term selected by Microsoft to designate any program that can be run under its Windows Operating System (all versions) and two, it is used to avoid confusion with older procedural programs that consisted entirely of only a language element. Thus, for our purposes we can express the elements of a Visual Basic application as: Visual Basic Application = Object-Based Visual Part + Procedural-Based Language Part Thus, learning to create Visual Basic applications requires being very familiar with both elements, visual and language. The Visual Element From a user s standpoint, the visual part of an application is provided within a window. This is the graphical interface that allows the user to see the input and output provided 24785_CH02_BRONSON.qrk 11/10/04 12:44 PM Page Elements of a Visual Basic Application 47 by the application. This user interface is referred to as the graphical user interface (GUI). From a programmer s perspective the GUI is constructed by placing a set of visual objects on a blank window, or form, when the program is being developed. For example, consider Figure 2 1, which shows how a particular application would look to the user. From a programmer s viewpoint, the application shown in Figure 2 1 is based on the design form shown in Figure 2 2. The points displayed on the form are a design grid used to arrange objects on the form and are only displayed during design time. Figure 2 1 A User s View of an Application Design Form (Initial Form Window) Design Window Figure 2 2 The Design Form on which Figure 2 1 is Based 24785_CH02_BRONSON.qrk 11/10/04 12:44 PM Page Chapter 2: Introduction to Visual Basic.NET The programmer can place various objects on this form, which is itself a Visual Basic object. When an application is run, the form becomes a window that provides the background for the various objects placed on the form by the programmer. The objects on the window become the controls used to direct program events. Let s take a moment to look at the objects provided in the Visual Basic Toolbox. The standard object Toolbox, which is illustrated in Figure 2 3, contains the objects we will use in constructing each graphical user interface. Figure 2 3 The Standard Visual Basic Toolbox 24785_CH02_BRONSON.qrk 11/10/04 12:44 PM Page Elements of a Visual Basic Application 49 Forms and Controls Programmer Notes When an application is being designed, a form is a container upon which controls are placed. When an application is executed, the form becomes either a window or a dialog box. Forms can be of two types: SDI or MDI. The acronym SDI stands for Single Document Interface, which means that only one window at a time can be displayed by an application. SDI applications can have multiple windows, but a user can only view one window at a time. The acronym MDI refers to Multiple Document Interface, which means the application consists of a single parent or main window that can contain multiple child or internal windows. For example, the Notepad application supplied with the Windows operating system is an SDI application, while Excel and Access are both MDI applications. A control is an object that can be placed on a form, and has its own set of recognized properties, methods, and events. Controls are used to receive user input, display output, and trigger event procedures. A majority of applications can be constructed using a minimal set of objects provided by the standard object Toolbox. This minimal set consists of the Label, TextBox, and Button objects. The next set of objects that are more frequently found in applications include the CheckBox, RadioButton, ListBox, and ComboBox. Finally, the Timer and PictureBox can be used for constructing interesting moving images across the window. Table 2 1 lists these object types and describes what each object is used for. The remaining sections of the text will describe the use of objects in the toolbox, with special emphasis on the four objects (Label, TextBox, Button, and ListBox) that you will use in almost every application that you develop. In addition to the basic set of controls provided in VB, a great number of objects can be purchased either for special purpose applications or to enhance standard applications. Table 2-1 Object Type Label TextBox Button CheckBox RadioButton ListBox ComboBox Timer PictureBox Fundamental Object Types and Their Uses Use Create text that a user cannot directly change. Enter or display data. Initiate an action, such as a display or calculation. Select one option from two mutually exclusive options. Select one option from a group of mutually exclusive options. Display a list of items from which one can be selected. Display a list of items from which one can be selected, as well as permit users to type the value of the desired item. Create a timer to automatically initiate program actions. Display text or graphics. 24785_CH02_BRONSON.qrk 11/10/04 12:44 PM Page Chapter 2: Introduction to Visual Basic.NET Don t be overwhelmed by all of the available controls. At a minimum, you will always have the objects provided by the standard Toolbox available to you, and these are the ones we will be working with. Once you learn how to place the basic control objects on a form, you will also understand how to place the additional objects, because every object used in a Visual Basic application, whether it is selected from a standard or purchased control, is placed on a form in the same simple manner. Similarly, each and every object contains two basic characteristics: properties and methods. An object s properties define particular characteristics of the object. For example, the properties of a text box include the location of the text box on the form, the color of the box (the background color), the color of text that will be displayed in the box (the foreground color), and whether it is read-only or can also be written to by the user. Methods are predefined procedures that are supplied with the object for performing specific tasks. For example, you can use a method to move an object to a different location or change its size. Additionally, each object from the Toolbox recognizes certain actions. For example, a button recognizes when the mouse pointer is pointing to it and the left mouse button is clicked. These types of actions are referred to as events. In our example, we would say that the button recognizes the mouse-click event. However, once an event is activated, we must write our own procedures to do something in response to the event. This is where the language element of Visual Basic comes into play. The Language Element Before the advent of GUIs, computer programs consisted entirely of a sequence of instructions. Programming was the process of writing these instructions in a language to which the computer could respond. The set of instructions and rules that could be used to construct a program were called a programming language. Frequently, the word code was used to designate the instructions contained within a program. With the advent of graphical user interfaces the need for code (program instructions) has not gone away rather, it forms the basis for responding to the events taking place on the GUI. Figure 2 4 illustrates the interaction between an event and a program code. As illustrated in Figure 2 4, an event, such as clicking the mouse on a button, sets in motion a sequence of actions. If code has been written for the event, the code is exe- An event, such as clicking on this button Figure causes this code to execute An Event Triggers the Initiation of a Procedure 24785_CH02_BRONSON.qrk 11/10/04 12:44 PM Page Getting Started in Visual Basic 51 cuted; otherwise the event is ignored. This is the essence of GUIs and event-driven applications the selection of executed code depends on what events occur, which ultimately depends on what the user does. The programmer must still write the code that performs the desired action. Visual Basic is a high-level programming language that supports all of the procedural programming features found in other modern languages. These include statements to perform calculations, permit repetitive instruction execution, and allow selection between two or more alternatives. With these basics in mind, it is now time to create our first Visual Basic application. In the next section, we introduce the Visual Basic programming environment and create an application that uses only a single object: the form itself. We will then add additional objects and code to create a more complete Visual Basic application. Exercises List the two elements of a Visual Basic Application. 2. What is the purpose of a GUI and what elements does a user see in a GUI? 3. What does a Visual Basic toolbox provide? 4. Name and describe the four most commonly used Toolbox objects. 5. When an application is run, what does a design form become? 6. What is executed when an event occurs? 2.2 Getting Started in Visual Basic It s now time to begin designing and developing Visual Basic programs. To do this, you will have to bring up the opening Visual Basic screen and understand the basic elements of the Visual Basic development environment. Visual Studio is the integrated development environment (IDE, pronounced as both I-D-E, and IDEE) used to create, test, and debug projects. Developers can also use Visual Studio to create applications using languages other than Visual Basic, such as C# and Visual C++. To bring up the opening Visual Basic screen, either click the Microsoft Visual Studio.NET icon (see Figure 2 5), which is located within the Microsoft Visual Studio.NET Group, or, if you have a shortcut to Visual Basic.NET on the desktop, double-click this icon. When you first launch Visual Basic.NET, the Start Page similar to the one shown in Figure 2 6 will appear. While this page provides links to Web pages to help developers find useful information, we will be concerned only with the following three areas: the central rectangle displaying recent programs, the Open Project button, and the New Project button. Clicking on any of the recent programs causes VB.NET to retrieve the program and load it into the IDE. Clicking the Open Project button opens a standard Windows file dialog box permiting you to retrieve a previously saved Visual Basic program and load it into the IDE. Clicking the New Project button opens the dialog box shown in Figure 2 7. This dialog box provides a choice of eleven project types, shown 24785_CH02_BRONSON.qrk 11/10/04 12:44 PM Page Chapter 2: Introduction to Visual Basic.NET Figure 2 5 The Microsoft Visual Studio.NET Icon within the Visual Studio.NET Group in Table 2 2. In this text, we will be concerned with Windows Applications and ASP.NET Web Applications. Click the New project button to open the New Project Dialog box displayed in Figure 2 7. Click the OK button to create a new project. Don t be concerned with the Name and Location, as the goal here is to display the IDE screen as shown in Figure 2 8 The four windows shown in Figure 2 8 are, as marked, the Toolbox window, the Initial Form window, the Solution window, and the Properties window. Additionally, directly under the Title bar at the top of the screen sits a Menu bar and a Toolbar, which should not be confused with the Toolbox window. Table 2 3 lists a description of each of these components. Before examining each of these components in depth, it will be useful to consider the IDE as a whole and how it uses standard Windows keyboard and mouse techniques. 24785_CH02_BRONSON.qrk 11/10/04 12:44 PM Page Getting Started in Visual Basic 53 Figure 2 6 The Visual Basic.NET Start Page Figure 2 7 New Project Dialog The IDE as a Windows Workspace The IDE consists of three main components: a GUI designer, a code editor, and a debugger. In the normal course of developing a Visual Basic program, you will use each of these components. Initially, we will work with GUI designer, which is the screen shown in Figure 2 8. The screen is actually composed of a main parent window containing multiple child windows. 24785_CH02_BRONSON.qrk 11/10/04 12:44 PM Page Chapter 2: Introduction to Visual Basic.NET Table 2 2 Eleven Project Types Windows Application Windows Control Library ASP.NET Web Service Console Application Empty Project New Project in Existing Folder Class Library ASP.NET Web Application Web Control Library Windows Service Empty Web Project Title Bar Menu Bar Tool Bar Project Window Initial Form Window Toolbox Sizing Handle Design Window Properties Window Debugging Window Dynamic Help Window Figure 2 8 The Integrated Development Environment s Initial Screen As a Windows-based application, each child window within the overall parent window, as well as the parent window itself, can be resized and closed in the same manner as all windows. To close a window you can double-click the X in the upper right-hand corner of each window. Windows can be resized by first moving the mouse pointer to a window s border. Then, when the pointer changes to a double-headed arrow, click and drag the border in the desired direction. You can move each window by clicking the mouse within the window s Title bar, and then dragging the window to the desired position on the screen. 24785_CH02_BRONSON.qrk 11/10/04 12:44 PM Page Getting Started in Visual Basic 55 Table 2 3 Component Initial Development Screen Components Description Title Bar Menu Bar Toolbar Layout Toolbar Toolbox Initial Form Window Properties Window Solution Window Form Layout Window The colored bar at the top edge of a window that contains the window name. Contains the names of the menus that can be used with the currently active window. The menu bar can be modified, but cannot be deleted from the screen. Contains icons that provide quick access to commonly used Menu Bar commands. Clicking an icon, which is referred to as a button, carries out the designated action represented by that button. Contains buttons that enable you to format the layout of controls on a form. These buttons enable you to control aligning, sizing, spacing, centering, and ordering controls. Contains a set of controls that can be placed on a Form window to produce a graphical user interface (GUI). The form upon which controls are placed to produce a graphical user interface (GUI). By default, this form becomes the first window that is displayed when a program is executed. Lists the property settings for the selected Form or control and permits changes to each setting to be made. Properties such as size, name, and color, which are characteristics of an object, can be viewed and altered either from an alphabetical or category listing. Displays a hierarchical list of projects and all of the items contained in a project. Also referred to as both the Solution Resource Window and the Solution Explorer. Provides a visual means of setting the Initial Form window s position on the screen when a program is executed. As with any other Windows application, Visual Basic makes use of a menu bar to provide an interface to the programmer. For example, if you wish to save a program you have been working on and start a new one, you would choose the File item from the menu bar, which will bring up the File submenu shown in Figure 2 9. From this menu you can save the current project by using the Save All option, then click the New option and click Project (Figure 2 10). The New Project dialog box appears. To access an existing program, you can also use the menu bar File item, except you would then click Open and click Project to reopen a previously saved program. Similarly, these two options can also be activated by clicking the appropriate icons on the Toolbar located immediately under the Menu bar. Once a program has been opened, you can always use the View item on the menu bar to display any windows that you need. For example, if either the Properties or Toolbox windows are not visible on the development screen, select the View item from the 24785_CH02_BRONSON.qrk 11/10/04 12:44 PM Page Chapter 2: Introduction to Visual Basic.NET Figure 2 9 The File SubMenu Figure 2 10 The New Project Dialog Box menu bar. This will open the View submenu illustrated in Figure From this submenu, click the Properties Window or click Toolbox and then click a Toolbox item to open the desired window. Note in Figure 2 11 that all Visual Basic s windows are listed in the View submenu. Having examined the Menu bar and how it is used to configure the development screen, make sure that you go back to the initial development screen shown in Figure 2 8. If any additional windows appear on the screen, close them by clicking each win- 24785_CH02_BRONSON.qrk 11/10/04 12:44 PM Page Getting Started in Visual Basic 57 Figure 2 11 The View SubMenu dow s close button (the box with the X in the upper right corner). The window does not have to be active to do this. Note that the caption within the top title bar of the screen shown in Figure 2 8 contains the words Microsoft Visual Basic [design]. The word [design] in the top Title bar caption is important because it indicates that we are in the design phase of a Visual Basic program. At any point within our development, we can run the program and see how it will look to the user. Once the design windows are visible, creating a Visual Basic application requires the following three steps: 1. Create the graphical user interface (GUI). 2. Set the properties o
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks
SAVE OUR EARTH

We need your sign to support Project to invent "SMART AND CONTROLLABLE REFLECTIVE BALLOONS" to cover the Sun and Save Our Earth.

More details...

Sign Now!

We are very appreciated for your Prompt Action!

x