Windows Forms Tricks You May Have Missed

This article dives into Windows Forms. Rather than focusing on a specific feature, it covers a variety of topics that will assist you in building fully functional and well performing Windows Forms applications. You'll need to have some level of familiarity with topics such as data binding and validation to follow along.

Data Binding Optimization

Data binding allows data to be processed automatically and displayed in controls such as ComboBox on the user interface (UI). When you change the ValueMember property on controls programmatically, it causes most controls to repopulate the data from the configured DataSource. This means your controls may inadvertently populate multiple times. To prevent your control from being populated multiple times, a good practice is to always set the DataSource property last on controls such as ComboBox and ListBox, as follows:

Control Data Population

Loading items into controls such as ListView and TreeView causes a repainting after each change. The BeginUpdate and EndUpdate methods allow bulk operations to occur without causing excessive repainting. Here's an example:

You also can use the AddRange method to efficiently add an array of pre-created items. It automatically calls BeginUpdate and EndUpdate. Additionally, it performs other optimizations to the methods internally to ensure efficiency in the loading, as follows:

You also should avoid the use of Items.Clear() with non-databound lists. It will cause excessive screen painting if you use it. Rather, encapsulate a block of code with calls to BeginUpdate and EndUpdate. Traverse the non-databound list and add, edit, or remove items. This will avoid unnecessary painting and UI delay.

PropertyGrid Control

Related Articles

The PropertyGrid control is a native control available within .NET. It is a simple model for displaying property settings. It can use a serializable object with properties to display, save, and load state. Below is sample code to create and display a simple property grid.

Threaded UI

A prior set of articles (Part 1, Part 2, and Part 3) contains much more detail about threading, but it is worth mentioning here. You can use threads to perform processing in the background and increase the perceived responsiveness and performance of a WinForm app. It is especially useful for long-running operations such as accessing resources across a network. The following sample code demonstrates the ability to call a method and have it process on a new thread, which would allow the UI to remain responsive:

ClickOnce Deployment

As covered in a previous article, ClickOnce is a mechanism for combining the reach of Web applications with the rich UI of a Windows Forms-based application through auto updateable Windows Forms. It relies on an application manifest, which describes the assemblies, dependencies, files, permissions, and update location, and the deployment manifest, which specifies the version number and location of the application manifest.

You can use ClickOnce in launched or installed mode. In launched mode, nothing is actually installed to the client computer. The application can be deployed to a web site, UNC folder, or CD/DVD. The application can look for updates on startup or at some point afterwards. Updates to the application can be forced as mandatory.

It has a publishing wizard that allows you to publish your projects easily and quickly. It auto generates the application and deployment manifests. The Mage and MageUI tools allow you to edit the manifest files after the fact. You should avoid changing the deployment type (debug or release) because it breaks compatibility with the manifest files. Also, assemblies used in the solution that are part of the GAC are not automatically included in the deployment.

User Input Validation

User input validation is the process of ensuring data is accurate before processing or storage. Typically, it is included in the UI layer to improve performance by reducing network round trips. Validation is enforced at the control level within Windows Forms. The controls have a CausesValidation property that dictates whether or not the control fires validation events when losing focus. The _Validating event is fired as the control is losing focus. You use the ErrorProvider control to control error messages and display. Here is some example code for validating a name field on a form:

One or more forms may not have had focus. This forces you to enumerate all form controls and fire the _Validating event for each control. The following sample code depicts this behavior based on the click of an OK button:

This is all works, but it is more difficult than it really needs to be. Windows Forms use a code-intensive approach that is programmatic. ASP.NET uses a control-based approach that is declarative. An MSDN article describes how to create a set of validation controls that match the ASP.NET validation controls. This solution allows you to eliminate or at least significantly reduce the amount of validation code that you end up writing. Full source code is provided so you can customize to your needs. For example, I modified the controls to include an additional IsRequired property so that I could use a single control to make a field required and match other criteria. I highly recommend reading the article.

Report Enable Your Application

SQL Server 2005 Reporting Services reports can be embedded in Windows applications using a report viewer control. You can use a remote or local connection mode. The remote processing mode allows the report to be hosted from the remote server. It is rendered in output format and sent down to the report viewer for display. The local processing mode does not require a report server at all. The data source is identified in advance. Data from an ADO.NET table or enumerable collection can be used as data for the report.

The report viewer control comes with Visual Studio 2005. You also can download it along with the AdventureWorks sample database from the Microsoft SQL Server site.