What's New in Windows Forms (Part 1)
In this first instalment of what's new in Windows Forms 2.0 I am going to begin showing you some of the great new features of Visual Studio 2005 and .NET 2.0 by taking you through the process of creating and editing a new Windows Forms project and by looking at the new Properties panes for Windows Forms projects.
Initial Reactions
When you create a new C# Windows Forms project, one of the first things you’ll notice is the new project template has changed. As shown in the following image, there is a new Program.cs file (this doesn’t exist in a new VB .NET Windows Forms project):

As you might guess by its name, this file contains the main entry point for your application:

In previous versions of Visual Studio, the main entry point in a Windows Forms application was a Main method on Form1 (this is still true in VB .NET). What are the chances you want a form to be the first object that instantiates in your application? Pretty slim—typically you want to instantiate an application object and some other core components first. This provides a better starting point for a real-world .NET application. Note the Program class is marked as static illustrating the use of the new static class feature in .NET 2.0.
Creating a Main Application Form
If you’re building an MDI (multiple document interface) application, you often want to make the default Form1 your main document window. To do this, you go to the Properties Window and set the form’s IsMdiContainer property to True. As you can see, this changes the background color based on your Windows Settings:

If you drag and drop a MenuStrip object on the form, it adds the menu to the Component Tray below the form and adds a [Type Here] box to the top of the form for editing. I’ve added a single File pad and File | Exit menu bar:

Double-clicking on the File | Exit menu bar adds an event handler to the form just as it did in VS .NET 2003. Within this handler method you can call the form’s Close method so the main form closes when the user selects File | Exit:

However, if you look closely, you’ll see something missing…in previous version of Visual Studio, double-clicking on a user interface element in C# added two pieces of code to the form…an event handler method (which is shown in the previous image), and event registration code, which is NOT seen in this code (although you CAN see the small rectangles surrounding the opening and closing class definition braces—a great new visual cue in Visual Studio 2005):

You would normally expect to see the event registration code in the [Windows Forms Designer generated code] block. So, where is it?
Partial Classes
Note the use of the new partial keyword in the class definition. You don’t see this in VB .NET, even though also uses partial classes. This is because in VB .NET only one part of the class must be marked as Partial:

The Windows Forms team is taking advantage of the new partial class feature of C# and VB .NET 2.0 that allows you to split a class definition between one or more source code files. So, if this is only part of the class definition, where is the other part? If you go to the Solution Explorer, click the Show All Files button, and expand the Form1.cs/vb node, you see a Form1.Designer.cs/vb file:

If you double-click this file to open it in the IDE, you can see it contains the other half of the Form1 class definition (again, note the use of the new partial keyword). Here it is in C#:

And in VB .NET:

If you expand the “Windows Forms Designer generated code” region in C#, you’ll see the traditional event registration code:

And if you scroll down in the VB .NET source code you see the WithEvents declaration:

Your custom properties, events, methods, etc. are stored in Form1.cs/vb and any code generated by Visual Studio is stored in Form1.Designer.cs/vb. This design is intended to avoid crowding the main Form class. In reality, you may find this partial class feature is a bit of a pain. For example, if you want to search a form for all code associated with a particular control, you need to search BOTH source code files manually.
Renaming the Main Form
Form1 is not the most descriptive name for either the source code file or the class itself, so let’s change it to MainForm instead. To change the name of the source code file, go to the Solution Explorer, right-click File1.cs, select Rename from the shortcut menu then change the name of the form to MainForm.cs or MainForm.vb. When you do this, you will see this dialog:

If you click Yes, Visual Studio searches all references to this form in your project and renames them. For example, the code in Program.cs that instantiates the form is changed to:

Setting Application Properties
VS 2005 provides a new way to set project properties. In C#, there is a new Properties item in the project:

And in VB .NET there is a new My Project item:

If you right-click either of these items and select Open from the shortcut menu, the new Properties interface is displayed in the IDE.
Application Properties Pane
The Application Properties panes are slightly different in C# and VB .NET. Here it is in C#:

And in VB .NET:

Both C# and VB .NET Application Properties panes have new Assembly Information… buttons. If you click this button it displays an Assembly Information dialog that provides a higher level user interface for editing the project’s AssemblyInfo.cs/vb file:

The biggest difference between the C# and VB .NET Application Properties panes are the Application Framework settings:

These Application Framework settings are unique to VB .NET. This is not a true application framework, but simply provides a new application-level object and settings that specify:
- If XP visual styles are enabled
- If only one instance of the application can be run at a time
- If Application settings are saved when the application is shut down
- The application’s authentication mode (either Windows or Application-Defined)
- The application shutdown mode (either When startup form closes or When last form closes)
- The application splash screen
If you go to the Solution Explorer and expand the My Project node you can see three new files that are associated with the application framework (Application.myapp, Application.Designer.vb, ApplicationEvents.vb):

If you double-click Application.myapp, you can see it stores the application settings in XML:

Go back to the Solution Explorer and double-click the Application.Designer.vb file. As you can see, this is the application class and it is part of the new My namespace:
Namespace My
Partial Friend Class MyApplication
_
Public Sub New()
MyBase.New(_
Microsoft.VisualBasic.ApplicationServices.AuthenticationMode.Windows)
Me.IsSingleInstance = false
Me.EnableVisualStyles = true
Me.SaveMySettingsOnExit = true
Me.ShutDownStyle = _
Microsoft.VisualBasic.ApplicationServices.ShutdownMode._
AfterMainFormCloses
End Sub
_
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Global.VS2005_Windows_Application_VB.MainForm
End Sub
End Class
End Namespace
If you look at the form’s constructor method New(), you can see it contains code that sets application properties. At design time when you change settings in the VB .NET Application Properties pane Visual Studio automatically puts corresponding code in the constructor of the application class.
Notice there is also an OnCreateMainForm() method that instantiates the form you specified as the Startup form in the Application Properties pane:

Go back to the Solution Explorer and double-click the ApplicationEvents.vb file. This source code file contains the following class definition:
Namespace My
Class MyApplication
End Class
End Namespace
You can also open ApplicationEvents.vb by clicking the View Application Events button in the Application Properties pane:

There are five different application events you can plug into:
| Event |
Description |
| Startup |
Raised when the application starts, before the startup form is created |
| Shutdown |
Raised after all application forms are closed. This event is not raised if the application is terminating abnormally |
| UnhandledException |
Raised if the application encounters an unhandled exception |
| StartupNextInstance |
Raised when launching a single-instance application and the application is already active |
| NetworkAvailabilityChanged |
Raised when the network connection is connected or disconnected |
To create a handler for one of these events, select (MyApplication Events) from the combo box at the upper left of the code window:

Then select the event you want to create a handler for from the combo box at the upper right of the code window:

This creates a handler method into which you can place code that is automatically called when the corresponding event is fired.
The Compile / Build Properties Pane
C# has two property panes for specifying project build options—Build and Build Events. VB .NET has a single Compile property pane but has an additional Advanced Compile Options… button on this pane which launches a dialog containing additional build options. It also has a Build Events... button that launches a dialog that corresponds to the settings on C# Build Events properties pane.
Platform Target - The .NET 2.0 Common Language Runtime now supports 64-bit processors. You can specify the platform target for C# (on the Build pane) and VB .NET (in the Advanced Compiler Settings dialog. This setting corresponds to the /platform compiler option.
The options are:
- Any CPU – (Default) Specifies that any processor is acceptable
- x86 – Use for any 32-bit Intel-compatible processor
- x64 – Use for other 64-bit processors
- Itanium – Use for the Intel 64-bit Itanium processor
Generally you do not need to create platform-specific versions of your application. For more information, see the MSDN topic /platform (Specify Output Platform) (C# Compiler Options)
Generate Serialization Assemblies - .NET 2.0 contains a new XML Serializer Generator Tool called SGen.exe that generates serialization assemblies that can be deployed with your application and improve startup performance.
You can specify whether you want Visual Studio to generate these serialization assemblies by setting the new Generate serialization assembly property for C# on the Build pane or in VB .NET in the Advanced Compiler Settings dialog. The options are:
For more information on serialization assemblies, see the MSDN topic Introducing XML Serialization
Language Version - The new Language Version setting is for C# only. There are two options that correspond to the /langversion compiler option:
- ISO-1 – Restrict language features to those present only in the ISO standard version of the C# language. Using features such as generics with this option is an error
- Default – (Default) Target the current version. Equivalent to leaving out the /langversion switch altogether.
Internal Compiler Error Reporting - This new setting is for C# only. The options are:
- None
- Prompt (Default)
- Send
As of this writing there is no published information from Microsoft on this setting.
Generate Debug Info - This setting instructs the compiler whether to generate debugging information and if so, how it is generated. The options are:
- None – Do not generate debugging information
- Full – (Default) Enables attaching a debugger to the running program
- pdb-Only – Allows source code debugging when the program is started in the debugger but only displays assembler when the running program is attached to the debugger.
- Compiler Alerts in VB .NET
The VB .NET Compile Properties pane has a new list of compiler conditions and notifications that can be set to specify how the compiler responds to specific error conditions:

The Debug Properties Pane
The Debug properties pane allows you to specify debug setting familiar in VS .NET 2003. The only new setting is Enable the Visual Studio hosting process. This setting specifies whether to use the new Visual Studio 2005 hosting process. For more information, see the section later in this document on “Understanding the New Visual Studio Hosting Model”.
The Settings Properties Pane
In VS .NET 2002 and 2003, Windows Forms application settings could be stored in an app.config file at design time. When the application was rebuilt, this file was copied to the output directory and renamed to the same name as the output assembly, but with a .config extension. You could retrieve settings (returned as strings) from this file at run time by means of the ApplicationSettings .NET Framework class.
In Windows Forms 2.0, the API for application settings is now strongly typed. In addition to application settings, there is also support now for a new user.config file that saves user-specific (as opposed to application-level) settings. These are stored in a new user.config file.
The Settings properties pane provides a high-level user interface for application and user settings:

You can right-click the Settings pane and select Add Setting from the shortcut menu. You can then specify the following properties for the new setting:
- Name – Setting name
- Type – Setting type (i.e. string. bool, int, (connection string), (Web Service URL)
- Scope – Application or User
- Value – Default value of the setting
If you specify the scope as Application, the setting is stored in the configuration file under the node. If you specify the scope as User, the setting is stored in the configuration file under the node. For example:
Northwind
There is also great design-time support for binding settings to user interface controls. Windows Forms UI controls now have an (ApplicationSettings) entry in the Properties Window. If you click the ellipses button […] next to this entry, it displays the Application Settings dialog. You can use this dialog to select a UI control property to bind to an application or user setting:

For more information check out this MSDN topic Application Settings Architecture
The Resources Properties Pane
The Resources properties pane allows you to add resources to your project using a high-level user interface:

The Images toolbar button allows you to select the type of resource you want to view. The choices are:
- Strings
- Images
- Icons
- Audio
- Files
- Other
To add a new resource to your project, click the Add Resource button and select one of the following options:

By selecting the Views button, you can also choose to view your resources in a list:

Or view details:

You can easily reference your resources from within your project. For example:
this.BackgroundImage =
OakLeaf.WindowsFormsDemo.Properties.Resources.Sunset_Moon;
The syntax is:
.Properties.Resources.
The Signing Properties Pane
The Signing properties pane provides a UI for signing the application and deployment manifests with a public/private key pair:

The Security Properties Pane
The Security pane gives you the ability to configure code access security settings for applications that are deployed using ClickOnce:

For more information check out the MSDN topic Code Access Security for ClickOnce Applications.
The Publish Properties Pane
The Publish pane allows you to specify settings for ClickOnce deployment:

For more information see the MSDN article Publish Pane, Project Designer.
The Code Analysis Properties Pane
The Code Analysis pane allows you to enable code analysis for your project. This new tool analyzes your project and reports information on the following categories:

If you select the Enable Code Analysis check box, it tells the compiler to enforce the rules you have specified. This performs similar code analysis as FxCop provided under Visual Studio 2003. For more information see the MSDN online topic Managed Code Analysis Warnings
Conclusion
As you can see there are some great new features coming your way in Visual Studio 2005 and Windows Forms 2.0. There are still some "pain points" when using Visual Studio 2005 Beta 2, but you may find that the enhancements discussed here and in upcoming columns make it worth switching to this new version sooner than later.
PS: OakLeafs is een SDN-Partner!