A Guide to Working in Visual Basic .NET

A Guide to Working in Visual Basic .NET

Microsoft Visual Studio .NET has a somewhat different development experience than Visual Basic 6 developers are used to. In fact, the new IDE combines features from Visual Basic 6, Microsoft Visual C++, and Microsoft Visual InterDev. The single most powerful aspect of the new IDE is that it is shared across all development languages. The Visual Basic editing experience is not all that different from the C# or C++ editing experience. The menus change slightly between project types, but the core interface, illustrated in Figure 6-1, remains the same.

In this section, you ll develop a simple Windows application to get a feel for the new IDE. The application will contain two buttons, a TextBox control and a ListBox control. The first button will add text from the TextBox control to the ListBox control. The second button will remove the selected item from the ListBox control. Nice and simple. Let s rock.

Figure 6-1

New Visual Basic .NET IDE.

Creating a Visual Basic .NET Project

Creating a Visual Basic .NET Windows Forms project is simple: under the File menu, select New, and then select Project. In the New Project dialog box, shown in Figure 6-2, select Visual Basic Projects as the project type and Windows Application as the project template. Leave all settings at their defaults and click OK to finish creating your project. Figure 6-3 shows the new project.

Figure 6-2

New Project dialog box.

Visual Basic .NET Project Types

Visual Basic .NET offers a sizable list of projects that you can create by using the New Project dialog box. The notion of a multilanguage solution is evident to anyone who examines this list. The dialog box does not change, whether you re creating a new project or adding a new project to an existing solution. Here are the Visual Basic .NET project types that are supported out of the box:

  • Windows application

  • Class library

  • Windows control library

  • ASP.NET Web application

  • ASP.NET Web service

  • Web control library

  • Console application

  • Windows service

  • Empty project

  • Empty Web project

  • New project in existing folder

Figure 6-3

Your new project.

Getting to Know the Visual Studio .NET IDE

In Visual Basic .NET you still have projects, but instead of project groups (as you had in Visual Basic 6) you now have solutions. Each solution can contain multiple projects, even projects created in different programming languages (for example, Visual C# and C++). Visual Basic .NET now supports additional project types, including console applications, Web applications, Web services, and Windows services, all of which can be included in your solutions. You use the Solution Explorer to organize and manipulate your projects and build settings. By default, you will find the Solution Explorer in the upper right corner of the screen.

Figure 6-3 demonstrates several differences between Visual Basic .NET and Visual Basic 6. In the Solution Explorer you can see that references are now accessible from the project tree, and Visual Basic no longer uses file extensions to identify forms, classes, and code modules. All Visual Basic files now have the same extension, .vb.

On the left side of the IDE is the Toolbox. Moving your mouse over the Toolbox tab causes the drawer to slide out from the side of the screen. The Toolbox view defaults to the Windows Forms tab, which lists all the controls available to your application.

Now we are going to walk through creating a simple Windows application. To create a Windows Forms project, do the following:

  1. Drag a TextBox control from the Toolbox and position it in the upper left corner of the form.

  2. Drag a Button control from the Toolbox and position it to the right of the TextBox.

  3. Drag another Button from the Toolbox and position it to the right of the first button.

  4. Drag a ListBox control to the form and position it below the TextBox and buttons.

  5. Position the controls so that the form looks roughly like Figure 6-4.

Now let s set some properties for the controls we just added to the form.

  1. Click the Button1 control. The property page should be visible on the right. If it s not, right-click the control and select Properties from the shortcut menu.

    Figure 6-4

    Newly added controls.

  2. In the property page set the Text property to &Add and the (Name) property to AddButton.

  3. Click the second button and set the Text property to &Remove and the Name property to RemoveButton.

  4. Click the ListBox1 control and find the Anchor property. (It is in the group of properties that specify layout, toward the bottom of the properties list.) Click the down arrow and make sure the top, left, right, and bottom sides of the control are selected. Setting these options will ensure the correct resizing behavior of the control.

You now have a Microsoft .NET application that doesn t do much but look pretty and resize nicely. Let s add some functionality. Double-click the Add Button control. This takes you to the Code Editor in the AddButton_Click method. Add the following line of code:

ListBox1.Items.Add(TextBox1.Text)

From the left drop-down menu directly above the editor, select RemoveButton. In the drop-down menu on the right, select the Click event. This creates a RemoveButton_Click event and positions the cursor inside the event. Now you should add the following line of code:

ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)

Not only have we implemented the desired functionality, but we ve also explored two ways of associating event handlers with controls in Visual Basic .NET. Now that we have the features we want, let s run the application.

Running Your Project

Compiling in Visual Basic .NET is different from compiling in Visual Basic 6. In Visual Basic 6, you could run your application with coding errors. The IDE would break in and notify you of problems before it would execute problematic code. In Visual Basic .NET, your entire application has to be compiled before you can test anything at run time.

To run your application, press F5. This step will cause Visual Basic .NET to compile the application before launching it under the debugger. At this point we shouldn t have any errors. Everything should work just fine and look something like Figure 6-5 (after the user has added a few listbox entries).

Figure 6-5

Your new Visual Basic .NET application.

It turns out that everything is not working just fine. Try selecting an item in the listbox (add one if necessary), and then click the Remove button twice. You will get the error shown in Figure 6-6.

Figure 6-6

The application is broken.

So it s broken. Click Break and let s dive into the debugger.

A Quick Introduction to Debugging

Debugging in Visual Basic .NET is not exactly what traditional Visual Basic developers are used to. Probably the most surprising missing feature is Edit And Continue. However, Visual Basic .NET offers a host of new features that greatly enhance the debugging experience, including service debugging, cross language debugging, XCopy deployment, structured exception handling, and a more sophisticated debugging API.

Where Is Edit And Continue?

The developers of the initial version of Visual Basic .NET decided not to include Edit And Continue. Dropping a feature that Visual Basic developers have come to rely on was not done lightly, but it had to be done. Visual Basic .NET and the .NET Framework are totally new platforms developed from the ground up. The challenge was to provide all the features of previous platforms that had come about during a decade of refinements. Unfortunately, including Edit And Continue would have meant delaying the initial release of Visual Basic .NET. Although it is a sadly missed feature, don t let this omission detract from the other powerful debugging features available in Visual Basic .NET.

In the preceding section, we learned that the application is throwing an exception on the following line of code, and we need to find out why. The exception dialog box has already told us that the error was an ArgumentOutOfRange exception. Looking at the code, we can see that the problem is located in the SelectedIndex property of the ListBox1 control:

ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)

The Autos window in the lower left side of the IDE displays the value for SelectedIndex. When no item is selected, the SelectedIndex property is 1. You can also use the Command window to inspect the SelectedIndex property. Just type ?ListBox1.SelectedIndexto print out the value in the Command window.

One possible solution is to add code that will check for this condition before trying to remove an item from the ListBox control. You must end the debugging session before you can make any changes to the source file. The current version of Visual Studio .NET locks the source files during debug sessions. Moving to the Code Editor, replace the body of the RemoveButton_Click event with the following code:

If ListBox1.SelectedIndex <> -1 Then    ListBox1.Items.RemoveAt(ListBox1.SelectedIndex) End If

This code prevents the application from attempting to remove any items from the ListBox unless an item is selected. It will avoid the exception previously encountered at this point. There is, however, another way to do this possibly a more proper way from a user-interface perspective. It makes sense that the user should not be able to click the Remove button unless it will actually perform a valid action. To specify this behavior, do the following:

  1. In the Form Designer, select the Remove button and change its Enabled property to False.

  2. In the Code Editor, select the ListBox1 control from the left drop-down menu. Select the SelectedIndexChanged event from the right drop-down menu. Add the following code to the new event handler:

    If ListBox1.SelectedIndex = -1 Then    RemoveButton.Enabled = False Else    RemoveButton.Enabled = True End If

The end result is that by default, the Remove button is disabled at startup. As soon as an item is selected in the listbox, the Remove button is enabled. If the user clicks the button, the selected item is removed, and the button is again disabled.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net