Creating a Simple Windows Forms Application

Windows Forms is the .NET technology for building rich client applications. It simplifies Windows development by hiding the Win32 API in an object-oriented wrapper. C#Builder contains wizards to create a project and many designer and graphical tools to assist in building Windows Forms applications. I'll be showing you how to use them in this chapter, but it is important that you understand the code used to build a Windows Form. C#Builder creates great amounts of code behind the scenes while building a Windows Forms application with its visual tools. However, you still need to know what the code means so that you can fix problems when they occur. This first section will introduce you to the basics of Windows Forms programming.

Listing 6.1 shows you how to create a simple Windows Forms application with just a few lines of code, and the next section will show how to use wizards and designers. To try out this program, create a new Windows Forms application by choosing File, New, Other; select C# Projects in the New Items dialog, and then double-click on the Application icon. This will create a new project in the Project Manager with a file named WinForm.cs. Right-click on WinForm.cs in the Project Manager and choose Show Code. This brings up the code for WinForm.cs in the Source Code Editor. Delete all the code from WinForm.cs and replace it with the code in Listing 6.1. After the code has been added, press the F9 key to make it run.

Listing 6.1 A Simple Windows Forms Application (Welcome.cs)
 using System; using System.Windows.Forms; public class Welcome : Form {    public  Welcome()    {       Text = "Welcome!";    }    static void Main()    {       Application.Run(new Welcome());    } } 

The code in Listing 6.1 is as simple a Windows Forms application as can be. It only pops up a window with "Welcome!" in the title bar, as shown in Figure 6.1. Ignoring all the class and method identifiers and curly braces, this program effectively contains only two executable statements. For a small amount of work, an application is produced that contains a title bar; system menu; minimize, maximize, and close buttons; and a resizable border.

Figure 6.1. Running a Windows Forms program.

graphics/06fig01.jpg

The magic behind this power is created by the object-oriented features of C#. Windows Forms applications inherit from the Form class, which is a member of the System.Windows.Forms namespace. The Form class has literally dozens of class members, many of which it inherits from other classes (see Figure 6.2). The classes in the Form hierarchy are also reusable and will be used later in this book when creating controls.

Figure 6.2. Form inheritance hierarchy.

graphics/06fig02.gif

For those of you familiar with Windows programming, the message loop and all the internal guts are still there. However, they are encapsulated in the Application class. Starting the program is simply a matter of passing a new instance of a Form derived class to the static Run method.



C# Builder KickStart
C# Builder KickStart
ISBN: 672325896
EAN: N/A
Year: 2003
Pages: 165

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