Using Visual Basic.NET Features to Code a Windows Forms Application

Welcome to the fourth part of a five-part series that shows you how to code and test a Windows Forms application. In this part you'll learn how to use the smart compile auto correction feature, the My feature, and more. This article is excerpted from chapter three of Murach's Visual Basic 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774456).

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
July 14, 2009
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

How to use the Smart Compile Auto Correction feature

As you learned in figure 3-9, Visual Studio puts a wavy line under any syntax errors that it detects while you’re entering code. In some cases, though, Visual Studio takes that one step further with its Smart Compile Auto Correction feature. In those cases, a bar appears at the right end of the wavy underline.

To use this feature, you place the mouse pointer over this bar to display a smart tag. Then, you can click the drop-down arrow that appears to display the Error Correction Options window shown in figure 3-13. This window includes a description of the error, suggestions for correcting the error, and a preview of how the code will look if you apply the corrections. If you like the suggested corrections, you just click on the suggestion to apply them.

For this example, I set the Option Strict option on, which you’ll learn how to do in the next chapter. Because that forces you to do some data conversion before comparisons or arithmetic calculations can be done, the suggested changes do those data conversions. This illustrates the power of this feature, so you’re going to want to use it whenever it’s available.

The Code Editor with the Error Correction Options window displayed

Description

  1. When Visual Studio detects a syntax error, it highlights the error with a wavy underline in the Code Editor.
  2. If a bar appears at the right end of a wavy underline, you can use the Error Correction Options window to view and apply suggested corrections. 
     
  3. To display the Error Correction Options window, place the mouse pointer over the bar, then over the smart tag that’s displayed, and click the drop-down arrow. 
     
  4. To apply a correction, click the appropriate “Replace…” link.

Note

  • To get the errors and the suggested corrections in the screen above, I turned the Option Strict option on. You’ll learn more about that in the next chapter.


Figure 3-13.   How to use the Smart Compile Auto Correction feature

How to use the My feature

The My feature that was introduced with Visual Basic 2005 can improve your productivity by making it easy to access .NET Framework classes and functions that would be otherwise difficult to find. If you’re new to programming, this feature may not make much sense to you, but it will as you learn more about Visual Basic. So for now, it’s enough to know that this feature is available and trust that you’ll learn more about it later.

Figure 3-14 illustrates how this feature works. As you can see, the My feature exposes a hierarchy of objects that you can use to access information. These objects are created automatically when you run an application. The three statements in this figure illustrate how you can use some of these objects. To get more information about any of these objects, you can use the Help feature as described later in this chapter.

The first statement in this figure shows how you can use the Name property of the My.User object to get the name of the user of an application. By default, this property returns both the domain name and the user name. To get this information without using this object, you would have to use the UserName and UserDomainName properties of the Windows.Forms.SystemInformation class. This illustrates how the My objects can make finding the information you need more intuitive.

The second statement shows how you can use the My.Computer.FileSystem object to check if a specified directory exists on the user’s computer. To do that, it uses the DirectoryExists method of this object. In chapter 21, you’ll learn about many of the properties and methods of this object that you can use to work with drives, directories, and files.

The third statement shows how you can use the My.Forms object to display a form. Note that when you use this technique, an instance of the form is created automatically if it doesn’t already exist. This wasn’t possible with Visual Basic 2002 or 2003. You’ll learn more about how to use the My.Forms object in chapter 24.

The main My objects for Windows Forms applications

A statement that gets the name of the current user of an application

  lblName.Text = My.User.Name

A statement that checks if a directory exists

  If My.Computer.FileSystem.DirectoryExists("C:\VB 2008\Files") Then ...

A statement that creates an instance of a form and displays it

  My.Forms.frmInvestment.Show()

Description

  • The My feature makes it easy to access frequently used .NET Framework classes and functions using objects that are grouped by the tasks they perform. These objects are created automatically when an application is run.

Figure 3-14.   How to use the My feature

How to get help information

As you develop applications in Visual Basic, it’s likely that you’ll need some additional information about the IDE, the Visual Basic language, an object, property, method, event, or some other aspect of Visual Basic programming. Figure 3-15 shows several ways you can get that information.

When you’re working in the Code Editor or the Form Designer, the quickest way to get help information is to press F1 while the insertion point is in a keyword or an object is selected. Then, Visual Studio opens a separate Help window like the one shown in this figure and displays the available information about the selected keyword or object. Another way to launch a Help window is to select a command from Visual Studio’s Help menu such as the Search, Contents, or Index command.

The Help window is split into two panes. The right pane shows the last help topic that you accessed. In this figure, for example, the right pane displays a help topic that provides information about working with the Code Editor.

The left pane, on the other hand, displays the Index, Contents, and Help Favorites tabs that help you locate help topics. In this figure, for example, the left pane displays the Index tab. At the top of this tab, the drop-down list has been used to filter help topics so they’re appropriate for Visual Basic programmers. In addition, “code e” has been entered to navigate to the index entries that begin with those letters, and the Code Editor entry has been selected.

In addition to the topic that’s displayed in the right pane, all the topics that are available for a selected entry are listed in the Index Results window that’s displayed across the bottom of the screen. When the Code Editor entry was selected in this figure, for example, four topics were listed in the Index Results window and the first topic was displayed by default. To display another topic, you simply click on it.

In the left pane, you can click on the Contents tab to display a list of help topics that are grouped by category. Or, you can click on the Help Favorites tab to view a list of your favorite help topics. At first, the Help Favorites tab won’t contain any help topics. However, you can add topics to this tab by displaying a topic and clicking on the Add To Help Favorites button that’s available from the toolbar.

You can display a Search tab in the right pane by clicking on the Search button in the toolbar. From this tab, you can enter a word or phrase to search for and select the languages, technologies, and content you want to search. Then, when you click the Search button, the results are displayed in the tab and you can click a topic to display it.

When you display information in the Help window, you should realize that the Help window uses a built-in web browser to display help topics that are available from your computer and from the Internet. In addition, the Help window works much like a web browser. To jump to a related topic, you can click on a hyperlink. To move forward and backward through previously displayed topics, you can use the Forward and Back buttons. As a result, with a little practice, you shouldn’t have much trouble using this window.

The Help window

Description

  1. You can display a Help window by selecting an object in the Form Designer or positioning the insertion point in a keyword in the Code Editor and pressing F1.
  2. You can also display a Help window by selecting a command (such as Index, Contents, or Search) from Visual Studio’s Help menu. 
     
  3. The Help window works like a web browser and can display help topics that are available from your computer or from the Internet. You can use the buttons in its toolbar to navigate between help topics or to add topics to your list of favorite topics. 
     
  4. The Help window is divided into two panes. The left pane displays the Index, Content, and Help Favorites tabs that let you locate the help topics you want to display. The right pane displays the last help topic that you accessed. 
     
  5. If you click on the Search button, the right pane will display a Search tab that lets you search for help topics by entering a word or phrase. 
     
  6. If you click on the How Do I button, the right pane will display a How Do I tab that lets you go to a topic by clicking on a link. 
     
  7. To close a tab, click on the Close button when the tab is active. To display a tab, click the tab or select it from the Active Files drop-down list that’s next to the Close button.

 

Figure 3-15.   How to get help information

How to run, test, and debug a project

After you enter the code for a project and correct any syntax errors that are detected as you enter this code, you can run the project. When the project runs, you can test it to make sure it works the way you want it to, and you can debug it to remove any programming errors you find.

How to run a project

As you learned in chapter 1, you can run a project by clicking the Start Debugging button in the Standard toolbar, selecting the Start Debugging command from the Debug menu, or pressing the F5 key. This builds the project if it hasn’t been built already and causes the project’s form to be displayed, as shown in figure 3-16. When you close this form, the application ends. Then, you’re returned to Visual Studio where you can continue working on your program.

You can also build a project without running it as described in this figure. In most cases, though, you’ll run the project so you can test and debug it.

If build errors are detected when you run a project, the errors are displayed in the Error List window, and you can use this window to identify and correct the errors. If it isn’t already displayed, you can display this window by clicking on the Error List tab that’s usually displayed at the bottom of the window or by using the View->Error List command.

The form that’s displayed when you run the Invoice Total project

Description

  • To run a project, click the Start Debugging button in the Standard toolbar, select the Debug->Start Debugging menu command, or press the F5 key. This causes Visual Studio to build the project and create an assembly. Then, if there are no build errors, the assembly is run so the project’s form is displayed as shown above.
  • If syntax errors are detected when a project is built, they’re listed in the Error List window and the project does not run. 
     
  • You can build a project without running it by selecting the Build->Build Solution command. 
     
  • When you build a project for the first time, all of the components of the project are built. After that, only the components that have changed are rebuilt. To rebuild all components whether or not they’ve changed, use the Build->Rebuild Solution command.

     

 Figure 3-16.   How to run a project

Please check back tomorrow for the conclusion to this article.

blog comments powered by Disqus
VISUAL BASIC.NET ARTICLES

- Basic Form Properties and Modality in VB.NET
- Multiple Document Interfaces in Visual Basic
- Visual Basic for Beginners
- ASP.NET Image to PDF with VB.Net
- MySQL in ASP.NET: Mono using VB.NET
- AsyncFileUpload File Type and File Size Vali...
- Visual Studio: Adding Functionality and Style
- Clocks and Countdowns
- User-defined Functions using Visual Basic Ap...
- Understanding Object Binding in VBA
- Mastering the Message Box
- Testing a Windows Forms Application
- Using Visual Basic.NET Features to Code a Wi...
- Correcting Code in a Windows Forms Applicati...
- Write Readable Code and Comments for Windows...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials