P1: GIG WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 1 RI AL Welcome to Visual Basic .NET MA TE The goal of this third edition is to help you come up to speed with the Visual Basic .NET language even if you have never programmed anything before. You will start slowly, and build on what you learn. So take a deep breath, let it out slowly, and tell yourself you can do this. No sweat! No kidding! IG HT ED Programming a computer is a lot like teaching a child to tie their shoes.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Chapter 1 Windows Versus DOS Programming A Windows program is quite different from its ancient relative, the MS-DOS program. A DOS program follows a relatively strict path from beginning to end. Although this does not necessarily limit the functionality of the program, it does limit the road the user has to take to get to it.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Welcome to Visual Basic .NET be manipulated to make it usable to other language developers. Version 6.0 provided a new way to access databases with the integration of ActiveX Data Objects (ADO). ADO was developed by Microsoft to aid Web developers using Active Server Pages to access databases. With all of the improvements to Visual Basic over the years, it ensured its dominant place in the programming world.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Chapter 1 Figure 1-1 If you want to build Web applications locally you must install Internet Information Services (IIS) and Front Page Server Extensions. Clicking Setup Instructions takes you to a Web page with explicit instructions on how to install these components. You will have to restart the Visual Studio .NET / Visual Basic .NET installation after installing IIS.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Welcome to Visual Basic .NET Figure 1-2 ❑ Microsoft FrontPage 2000 Web Extensions Client ❑ Setup Runtime Files ❑ Microsoft Internet Explorer 6.0 with Service Pack 1 ❑ Microsoft Data Access Components 2.7 with Service Pack 1 ❑ Microsoft .NET Framework 1.1 ❑ Microsoft Visual J# .Net Redistributable Package 1.1 If you don’t know what some of those things are, don’t worry about it.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Chapter 1 Figure 1-3 There are three sections of information given for each feature: ❑ The Feature properties section outlines where the required files will be installed and how much space will be needed to do this. ❑ The Feature description box gives you an outline of each feature and its function.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Welcome to Visual Basic .NET 7. When installation is completed, you will see a dialog informing you that the installation has completed. Here you will see any problems that Setup encountered along the way. You are also given the chance to look at the installation log. This log provides a list of all actions taken during the installation process. Unless your installation reported errors, the installation log can safely be ignored.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Chapter 1 11. After clicking Next, you will be allowed to select the amount of the documentation you want to install. For example, if you did not install C++ there is probably little reason to install that documentation. If you have the spare hard drive space, it is a very good idea to install the full documentation. MSDN does not always include specific examples or documentation for Visual Basic .
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Welcome to Visual Basic .NET Figure 1-5 that you want and then click the Projects tab to be taken to that view. There you can create new projects and open existing projects. The Projects Tab By now, you may be a bit anxious to start writing some code. But first, take a look at the Projects tab on the Start Page and see what is there. Assuming that you have been following along while setting up Visual Studio .
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Chapter 1 Figure 1-6 File, Edit, View, Tools, Window, and Help menus. However, when you start working on a project, the full Visual Studio .NET menu appears as shown in Figure 1-7. Figure 1-7 At this point, there is no need to cover each menu topic in great detail. You will become familiar with each of them as you progress through the book.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Welcome to Visual Basic .NET ❑ Project: The Project menu allows you to add various files to your application such as forms and classes. ❑ Build: The Build menu becomes important when you have completed your application and want to run it without the use of the Visual Basic .NET environment (perhaps running it directly from your Windows Start menu as you would any other application such as Word or Access).
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Chapter 1 The toolbar is segmented into groups of related options, which are separated by a vertical bar. The first five icons provide access to the commonly used project and file manipulation options available through the File and Project menus, such as opening and saving files. The next group of icons is for editing (Cut, Copy, and Paste). The third group of icons is for editing and navigation.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Welcome to Visual Basic .NET Figure 1-9 3. Visual Basic .NET will then create an empty Windows application for you. So far, your HelloUser program consists of one blank window called a Windows Form (or sometimes just a form), with the default name of Form1.vb, as shown in Figure 1-10. Whenever Visual Studio .
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Chapter 1 Figure 1-10 20 ❑ Toolbox: The Toolbox contains reusable components that can be added to your application. These can range from buttons to data connectors to customized controls either purchased or developed by you. ❑ Design Window: The Design window is where a lot of the action takes place. This is where you will draw your user interface on your forms. This window is sometimes referred to as the Designer.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Welcome to Visual Basic .NET form in the Design window and then open Dynamic Help, you will see a list of help topics relating to forms. Try It Out Creating a HelloUser Project (cont.) 1. Change the name of your form to something more indicative of what your application is. Click on Form1.vb in the Solution Explorer window. Then, in the Properties window, change the File Name property from Form1.vb to HelloUser.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Chapter 1 Figure 1-12 property in the Appearance section of the Properties window and change its value to Hello from Visual Basic .NET and press Enter. Notice that the form’s title has been updated to reflect the change (see Figure 1-13). If you have trouble finding properties, click the little AZ button on the toolbar toward the top of the Properties window.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Welcome to Visual Basic .NET Figure 1-13 The Toolbox The Toolbox is accessed via the View ➪ Toolbox menu option, the Toolbox icon on the Standard menu bar, or by pressing Ctrl+Alt+X. Alternatively, the Toolbox tab is displayed on the left of the IDE and hovering your mouse over this tab will cause the Toolbox window to fly out, partially covering your form.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Chapter 1 Figure 1-14 drag the mouse to where you want the bottom right. (Placing controls on your form can also be accomplished by double-clicking on the required control in the Toolbox.) 24 3. If the Label control you have just drawn is not in the desired location or is too big or too small, it really isn’t a problem. Once the control is on the form you can resize it or move it around.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Welcome to Visual Basic .NET Figure 1-15 Figure 1-16 5. Now, directly beneath the label, you want to add a textbox, so that you can enter a name. You are going to repeat the procedure you followed for adding the label, but this time make sure you select the TextBox from the toolbar.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Chapter 1 Notice how, out of the eight sizing handles surrounding the control, only two are shown in white. By default, the TextBox control cannot be made any taller than the absolute height necessary to contain the font that it will use to draw the text. 6. In the bottom left corner of the form, add a Button control in exactly the same manner as you added the label and textbox.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Welcome to Visual Basic .NET Figure 1-18 Try It Out, you had named your label Name1 and your textbox Name2—you would very quickly become confused. How about if you left your application for a month or two and then came back to it to make some changes? When working with other developers, it is very important to keep the coding style consistent.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Chapter 1 Table 1-1 Common prefixes in Visual Basic .Net Control Prefix Button cmd or btn ComboBox cbo CheckBox chk Label lbl ListBox lst MainMenu mnu RadioButton rdb PictureBox pic TextBox txt functionality behind those on-screen elements is no more difficult. To add the code for a control, you just double-click on it.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Welcome to Visual Basic .NET or subroutines for the object selected in the left combo box. If this particular form had a lot of code behind it, these pull-downs would make navigating to the desired area very quick—jumping to the selected area. However, since all of the code fits in the window, there are not a lot of places to get lost. Now look at the code in the window. The code in Visual Studio .
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Chapter 1 Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click 'End the program and close the form Me.Close() End Sub You may be wondering what Me is. Me refers to the form. Just like the pronoun me, it is just a shorthand for referring to oneself. 4. Now that the code is finished, the moment of truth has arrived and you can see your creation.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Welcome to Visual Basic .NET 7. A window known as a message box appears, welcoming the person whose name was entered in the textbox on the form—in this case Thomas (see Figure 1-21). Figure 1-21 8. After you close the message box by clicking the OK button, click on the Exit button on your form. The application will close and you will be brought back to the Visual Basic .NET IDE.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Chapter 1 Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click 'End the program and close the form Me.Close() End Sub Using the Help System The Help system included in Visual Basic .NET is an improvement over Help systems in previous versions. As you begin to learn Visual Basic .NET, you will probably become very familiar with the Help system.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Welcome to Visual Basic .NET Figure 1-23 The other help commands in the Help menu (Contents, Index, and Search), function just as they would in any other Windows application. Summary Hopefully, you are beginning to see that developing basic applications with Visual Basic .NET is not that difficult. You have taken a look at the IDE and saw how it can help you put together software very quickly.
P1: GIG WY006-01 WY006-Sample WY006-Sample-v3.cls January 29, 2004 17:49 Chapter 1 Exercises At this point, you have not covered much about Visual Basic .NET, the language. So these exercises wouldn’t be too difficult. 1. 2. What Modified-Hungarian prefix should you use for a combo box? A label? A textbox? 3. When creating a button, how would you make the button respond to a keyboard hot key? (This assumes you set the Help Filter to Visual Basic and Related.