Your First Windows Forms Application

 Download CD Content

In the previous chapters, we built Console applications in VB .NET. In this chapter, we look at building a Windows Forms application, a much more common type of program for VB developers (see Figure 11.1) especially for applications related to a Tablet PC.

click to expand
Figure 11.1: A Windows Forms application.

Project Overview

We begin this chapter by looking at the creation of a user interface for our application. The user interface is probably the first step you'll take when developing most typical VB .NET applications. Start the Visual Basic IDE and select Windows Forms Application from the templates that appear.

The next step is to place controls on the default form that appears. There are two separate approaches you can use to do this. First, you can simply double-click on one of the intrinsic Visual Basic controls that appear in the toolbar, which places a single instance of the control on the form. Another way you can place controls on the form begins by clicking the tool in the Toolbox. You then move the mouse pointer to the form window, and the cursor changes to a crosshair. Place the crosshair at the upper-left corner of where you want the control to be, press the left mouse button, and hold it down while dragging the cursor toward the lower-right corner. As you can see in Figure 11.2, when you release the mouse button, the control is drawn.

click to expand
Figure 11.2: You can place controls on a form in several different ways.

You don't have to place controls precisely where you want them because you can move them; Visual Basic provides the necessary tool to reposition them at any time during the development process. To move a control you have created with either process, you click the object in the form window and drag it, releasing the mouse button when you have it in the correct location. You can resize a control very easily as well, by clicking the object so that it is in focus and the sizing handles appear. These handles, which can be seen in Figure 11.3, can then be clicked and dragged to resize the object.

click to expand
Figure 11.3: Handles are useful for positioning and resizing objects.

For a first project, you can begin by placing a text box and a command button on the form and position them so that they look similar to Figure 11.4.

click to expand
Figure 11.4: Beginnings of a GUI.

The command button needs its Text property changed in the Properties window. It can be changed to 'Click.' See Figure 11.5.

click to expand
Figure 11.5: The layout with the changes to the button.

The next step is to double-click the command button, which brings up the Code window and leaves you something that looks similar to Figure 11.6.

click to expand
Figure 11.6: When you double-click an object on a Visual Basic form, it opens an event in the Code Editor.

Your cursor should be flashing beneath the Private Sub Command1_Click() line. Type the following lines into your application:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strInfo As String
strInfo = "My First Windows Application"
MessageBox.Show("Hello World!")
 End Sub

Click on the tab at the top of the screen that says  Form1.vb [Design]. Double-click the form to open the Form_Load event. Enter the following code and continue reading for an explanation:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 TextBox1.Text = "Form Load"

End Sub
  Note 

The CD-ROM that is included with the book contains all of the sample code for each of the projects we create throughout the book. This saves you time and programming mistakes, which allows you to focus only on the task at hand-learning VB .NET. It also contains several additional applications. Please see the CD-ROM for a complete list of projects and applications.

The Code Explanation

That's all we need for this application. Although this project is very simple, we did use variables for storing text information in order to point out several features of VB .NET. First, there are only a few simple rules you should keep in mind when using variables. They should be less than 40 characters, they can include letters, numbers, and underscores (_), they cannot use one of the Visual Basic reserved words (i.e., you cannot name a variable Text), and they have to begin with a letter. Let's look carefully at what the code does.

The line that begins with Private Sub Button1_Click tells Visual Basic to run this line of code when someone clicks the command button you created called Button1. When they do, the lines of code you typed in are executed. The End Sub simply informs Visual Basic that it's time to stop running the code. This event and subsequent code was created automatically for you when you double-clicked on Button1 in a previous step. The Form_Load event was created automatically for you when you double-clicked the form. Inside the event, you added a line that sets the text box equal to 'Hello World!'

Similar to the Button1_Click event, the code is run only when the form is loaded when the program runs. The first line of code that you entered dimensions the variable strInfo as a string. The next line assigns the text string 'Form Load' to the variable and the final line uses the MessageBox.Show method to display the message box.

You may have noticed some additional code in your Code Editor that we haven't looked at or mentioned in this example. Specifically, you will see something that says 'Windows Form Designer Generated Code' that contains a '+' sign next to it. If you click the '+', you will see an enormous amount of code that was created for you automatically. This code is the underlying code that is created when you create a form. We'll ignore this section of code for the examples in this book. Because the code is created by VB .NET automatically, we don't really have a need to list it.

Running the Program

You can execute the program from within Visual Basic by pressing Ctrl+F5. You should see a window that appears similar to Figure 11.7.

click to expand
Figure 11.7: Your first program running inside the IDE.

You can close it like any Windows-based application or select the Stop button from within the Visual Basic IDE. You've created your first Windows Forms application. You can save your changes, if you want, by selecting Save from the File menu. When saving a project, it's best to create a new directory in which you can store all the files necessary for the project. In this way, you keep the files in one easy to manage area without the risk of another project corrupting the source code or data.

Complete Code Listing

The following code is the complete listing for this chapter:

Public Class Form1
 Inherits System.Windows.Forms.Form

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 Dim strInfo As String
 strInfo = "My First Windows Application"
 TextBox1.Text = strInfo
 MessageBox.Show("Hello World!")
 End Sub

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 TextBox1.Text = "Form Load"
 End Sub
End Class

Summary

During the first chapter, we looked at numerous concepts, many of which might be new. We built our first Windows Forms application after dealing with Console applications in the previous chapters. We developed a simple application by using a few intrinsic controls and some basic code, and then proceeded to run it inside the Visual Basic IDE.

Now that you have some of the basics out of the way, let's move to the next chapter-where the real fun begins!



Developing Tablet PC Applications
Developing Tablet PC Applications (Charles River Media Programming)
ISBN: 1584502525
EAN: 2147483647
Year: 2003
Pages: 191

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