Introduction
This is a sample chapter from C# and the .NET Framework.
In This Chapter
- The Hello Windows Forms Application
- Creating and Using an Event Handler
- Defining the Border Style of the Form
- Adding a Menu
- Adding a Menu Shortcut
- Handling Events from Menus
Windows Forms is the .NET replacement for MFC. Unlike the MFC library, which
was a thin(ish) wrapper on the Win32 API, Windows Forms is a totally object-oriented,
hierarchical answer to Windows development under .NET.
Despite its "Forms" epithet, the layout of components is not done
with a resource file as is the case in MFC dialogs and form windows. Every component
is a concrete instance of a class. Placement of the components and control of
their properties are accomplished by programming them via their methods and accessors.
The drag-and-drop tools used to define a Windows Form are actually maintaining
the source code that initializes, places, and allows interaction with a target
application.
Resource files are used by Windows Forms for tasks such as placing images on
the form or storing localized text, but not in the familiar format that has been
used by Windows since the 1980s. As you might expect, the new resource file format
is an XML file. We'll examine resources in more detail in Chapter 3.4, "Windows
Forms Example Application (Scribble .NET)."
Windows Forms layout can be done by dragging components from a tool palette
onto a form. You can use VS.NET for this or alternatively, if you really want
the whole hands-on experience, you can lay out your forms by coding the objects
directly in C#, VB, managed C++, or any of the .NET languages. To give you the
benefit of understanding what really goes on in a Windows Form, we won't
be dealing with any of the design tools early in this section. Rather, we will
do as much as possible by hand and then, when you understand the basics, move
on to using the drag-and-drop tools.
User interaction in Windows Forms is accomplished through events. The components
provide event sources, such as mouse movement, position, and button clicks, and
then you wire the events to handlers. These are functions called by a standard
form of delegate, which means there are no message maps of which to keep track.