Windows Applications Using the .NET SDK

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 7.  Windows Forms


To gain insight into how Windows Forms works, it will be helpful to build a simple application using only the .NET Framework SDK. See the program ByHand and its progressive steps. None of these steps use Visual Studio. The file build.bat is a simple batch file that you should run at the command prompt to build the executable.

Step 1: A Simple Form

The following code illustrates a bare-bones Windows application. It is Step 1 of the example ByHand .

 graphics/codeexample.gif Imports System.Windows.Forms Class MainWindow    Inherits System.Windows.Forms.Form    Public Shared Sub Main()       Application.Run(new MainWindow())    End Sub End Class 

Our MainWindow class inherits from System.Windows.Forms.Form . The key to Windows Forms programming is the Form base class. This class contains a great deal of functionality, which is inherited by form classes that we design.

The class System.Application has shared methods , such as Run and Exit , to control an application. Our form's Main method instantiates a new form and runs it as the main window using the Application class.

You can build this application at the command line using the batch file build.bat . To run the batch file, open up a DOS window and navigate to the ByHand\Step1 directory and type build . Remember that you must have the environment variables set up properly. (If you bring up a DOS window by running the Visual Studio .NET Command Prompt, you can be sure that your environment variables are set properly.) The build.bat file contains:

 vbc /t:winexe /r:System.dll,System.Windows.Forms.dll    BasicWindow.vb 

The /t flag specifies the target is a Windows executable. The /r flag specifies there are references to the required .NET libraries, System.dll and System.Windows.Forms.dll .

After you have built the application using the batch file, you can run it by typing BasicWindow at the command line. You can also double-click on the file BasicWindow.exe in Windows Explorer. Figure 7-2 shows this simple application. Although trivial, it already has a great deal of functionality, which it inherited from the Form base class. You can drag the window around, resize it, minimize it, maximize it, open the system menu (click in top left of the window), and so forth.

Figure 7-2. A bare-bones Windows Forms application (Step 1).

graphics/07fig02.jpg

Step 2: Customizing the Form

The form can be customized by manipulating the various properties and methods that were inherited from Windows.Forms.Form.Form . In Step 2 of ByHand , we have added a caption to the window and changed the size of the form.

 graphics/codeexample.gif Imports System.Windows.Forms Class MainWindow    Inherits System.Windows.Forms.Form    Public Sub New()       MyBase.New()  Me.Text = "First Program"   Me.Height = 175  End Sub    Public Shared Sub Main()       Application.Run(new MainWindow())    End Sub End Class 

The constructor of the form does initializations. The Text property specifies the caption that is to be shown in the title bar of the form. The Height property sets or retrieves the height of the new form in pixels. There are many other properties of controls that can be set or retrieved. We review many of the common properties via examples in this book, but the .NET Framework documentation should be your source for a complete listing. Figure 7-3 shows this new version of the application.

Figure 7-3. A Windows Forms application with custom caption and size (Step 2).

graphics/07fig03.jpg

Windows Messages

Visual Studio .NET supplies a tool called Spy++, which can be used to "spy" on windows, gaining some understanding of things taking place under the hood. Spy++ can be started from the Visual Studio Tools menu. With the Step 2 version of BasicWindow.exe running, start Spy++. Bring up the Find Window dialog from the menu Spy Find Window. Click on the Messages radio button. See Figure 7-4.

Figure 7-4. The Finder Tool lets you select a window to spy upon.

graphics/07fig04.jpg

Using the left mouse button, drag the Finder Tool over the window of the BasicWindow application and release the button. Now, as you interact with the BasicWindow window, you will see windows messages displayed in a window of Spy++, as illustrated in Figure 7-5.

Figure 7-5. Spy++ can display Windows Messages.

graphics/07fig05.jpg

The Windows operating system sends messages to applications in response to user actions such as clicking a mouse button, typing at the keyboard, and selecting a menu. A Windows application is designed to respond to these messages.

The nice thing about Windows programming using the .NET Framework classes is that we do not have to concern ourselves with the structure of these messages or the mechanics of how they are processed . We simply identify the messages that are of interest to us and build procedures that handle the events. We have already seen how simple it is to build a basic Windows application. In the next few sections, we will progressively implement some basic features of Windows Forms programming, including adding controls and trapping events.

Controls

The System.Windows.Forms.Control class implements the basic functionality required by classes that display to the user. It also handles user input through the mouse and keyboard. At last check, there were well over twenty controls in the .NET Framework SDK that were derived from the Control class, including Button , Label and TextBox .

The Control class defines a set of properties and methods common to all controls. You will find yourself using many of these for each control you add to a window. Properties include:

  • Name , which represents the name of the control.

  • Left and Top , which represent the x and y coordinates, in pixels, of the top left edge of the control on the parent window (these values are also accessible by referencing the Location property).

  • Width and Height , which represent the width and height, in pixels, of the control (these values are also accessible by referencing the Size property).

  • Text , which represents the text (often the caption) associated with the control.

  • BackColor , which will contain a value from the System.Drawing. Color .

You can find all the properties and methods associated with a class in the .NET Framework Documentation. The screenshot in Figure 7-6 shows the overview of the Control class help. The "Control Members" link on the page can be used to learn about the various properties and methods.

Figure 7-6. Documentation of properties and methods in the Control class.

graphics/07fig06.jpg


Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

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