YOUR FEEDBACK
johnpetersen wrote: Great post. You hit some good points, and hopefully me sending this post. It wil...

SYS-CON.TV
TOP MICROSOFT .NET LINKS


Cover Story: Unlocking the Inner Secrets of WinForms
Windows Forms Features Explored

The new version of Windows Forms in .NET 2.0 brings some exciting and much-needed enhancements that not only allow for more productivity, but also allow for creating some extremely professional looking applications that have the look and feel of many of the office applications we use every day. The goal of this article is to introduce many of the new features starting at the base level and building up, much as you would create an application, detailing each feature and how the features can help you.

Base Level Enhancement
The work that has been done at the lower levels of Windows Forms has been tremendous. The designers now separate your code from the code that is automatically generated so you can concentrate on writing your code without worrying if you are going to infringe on anything the designer has generated. The new Application settings feature provides a clean foundation for handling application and user-specific data persistence, and Data Binding has been revamped to provide greater design-time support and easier implementation.

The use of partial classes, a new feature of .NET 2.0, now makes working with Windows Forms designer-generated code much easier. In previous releases your code and the designer code lived in the same file, thus making your development effort a lot less manageable. Now when you add a form to your project you get two generated files: one, [Form Name].Designer.(vb or cs), which contains the designer generated code, and another, [Form Name].(vb or cs), which is created for your code.

The new Application Settings feature, a replacement for dynamic properties, enables developers to easily create, store, and maintain both application and user data on the client. This feature eases the development experience by providing a built-in framework for working with application data such as Web service locations or database connection strings, and user data such as the user screen positions or button positions on a toolbar. The steps to implement application settings in your application are very simple and require very little to no coding at all. It is as simple as selecting a form or control, opening the simple designer that is under the (ApplicationSettings) groups (PropertyBinding) entry in the properties window, selecting a property to persist, naming the entry, and setting its scope to either application or user. The persistence model for application settings, which is also handled for you, relies on XML and uses either the application configuration file and the <applicationSettings> element for read-only access to application-specific data or the user.config file, which is unique for each user, and the <userSettings> element for read/write access to user-specific data.

The enhancements to Data Binding (see Figure 1) are enormous. From the new wizards and controls to the ability to retrieve data from a variety of data sources, everything is much more integrated than in previous versions. The first thing you will notice is the new Data Sources window that provides a common point of reference when working with data throughout your application. The window relies on an easy-to-use wizard to create data sources from a database, local database file, Web service, or custom business object, and adjusts the user's experience depending upon what type of data you are trying to retrieve. Once you have created a data source you can then use that data source as the basis for data in your form. This can be done in a number of ways, such as dragging a field from the data source field list onto your form and having the designer create the field automatically, or laying out your form and then connecting the field to your form control. Either of these methods creates a series of controls on your form that create a foundation for a new level of indirection and type-safe access to data that is the cornerstone of the new data binding enhancements. The main control that gets created is the DataSource control, which is the common data source for all data bound controls. This control abstracts the complexities of the underlining data, thus providing a common place for navigating, sorting, filtering, and updating data-bound data, and also manages the data concurrency so that all common bound controls show the same data. If you are working with a database, the TableAdaptor control will also be added to your form during this process. This control provides communication between your application and the database and can be considered as a DataAdaptor with a built-in connection object and the ability to contain multiple queries. Each query contained in the TableAdaptor is exposed as a type-safe method and is required to return the same schema. Finally, the BindingNavigator control is added to your form, in the case of working with a database, which works in conjunction with the DataSource control to assist in navigating and manipulating your form data. The BindingNavigator controls UI is composed of a series of ToolStrip buttons, text boxes, and static text elements for most of the common data-related actions such as adding data, deleting data, and navigating through your form data. As you can see, many of the complexities of the previous versions of Data Binding have been abstracted for you, thereby allowing you to be more productive when creating data-driven applications.

Form Layout, Navigation, and Control-Interaction Enhancements
In previous versions of Windows Forms there were a few things that hampered your productivity. For starters, there was the laying out of controls on a form, which was a very tedious and time-consuming task. Then there was the problem of navigating through all of the controls on a form during development and accidentally moving one around when all you wanted to do was get at a property. In the new version of Windows Forms, these issues have been dealt with in an elegant way.

The layout of controls on a form has now been enhanced by the introduction of the Snaplines designer feature, the FlowLayoutPanel control, the TableLayoutPanel control, and the SplitContainer control. The Snaplines designer feature aids in the layout of absolute positioned controls by automatically displaying anchor lines as you move your control in the vicinity of another control, which makes it extremely easy to line up and space your controls. The logic for the appearance of the anchor lines is a combination of the values of the new margin and padding properties now on all controls and the Windows Design Guidelines for the spacing of controls on a form. The FlowLayoutPanel control is a new container control that bases the layout of controls contained in it on relative positioning instead of absolute positioning, which is the case when you drag a control onto a form. This control allows you to determine how the controls inside of it are displayed either horizontally or vertically based on the FlowDirection property, and if the contents are clipped when the control's dimensions are reduced based on the WrapContents property. This control work very well in situations where you want to add buttons dynamically at run time to a preexisting area and have them arrange themselves automatically. The TableLayout control, which is also a container control, brings some of the layout behavior that HTML tables bring in, allowing the user to place controls inside of the grid. Just as in HTML, the rows and columns can shrink or grow depending on changing content and screen dimensions. The RowCount, ColumnCount, and GrowStyle properties control just how the grid will respond to the contents added to it and the RowStyles and ColumnStyles properties control how the rows and columns behave when the page is drawn. This control can be very helpful in situations where you are laying out a form that requires the controls to respond appropriately to changes to the size of the screen. The last control I will talk about in this category is the SplitContainer control, which is a replacement for the Splitter control. This control is a composite control consisting of two panes and a moveable splitter bar, and the splitter bar's vertical and horizontal orientation is adjustable based on the Orientation property. This control enables you to create complex user interfaces similar to Outlook with one panel determining what is displayed in the other panel.

The process of quickly finding a control on a form has historically been slow and there has been no support in the Windows Form designer to navigate through the controls easily until now. The Document Outline window has been added to the forms designer, thus enabling form-control navigation just as in HTML pages. The contents of the form are now in a tree view, giving you a hierarchical view of your controls based on the parent-child relationship of the controls inside of the form.

New ToolStrip Family of Controls
The new ToolStrip family of controls (see Figure 2) replaces the old Toolbar, Menu, and StatusBar of previous versions, enabling you to create professional looking applications with the XP, Office, and Internet Explorer appearance and behavior using a rich design-time environment. The ToolStrip supports many advanced features such as docking, run-time reordering of items, built-in support for dragging of items from one ToolStrip to the next, and the ability for toolbars to share the same horizontal or vertical space using the ToolStripContainer control. The ToolStrip control is also very configurable and extensible, and provides many properties, methods, and events to customize its appearance and behavior. To assist in creating professional looking applications, the ToolStrip control comes with many prebuilt controls that you can add to it like the ToolStripButton, ToolStripComboBox, ToolStripLabel, ToolStripDropDownButton, Tool-StripProgressBar, and ToolStrip-TextBox to name a few. The Menu-Strip, ContextMenuStrip, and Status-Strip, which are replacements for their older counterparts, all inherit from ToolStrip and share a common API, which makes programming against them consistent and easy.

DataGridView Control
The DataGridView control (see Figure 3) replaces the DataGrid control of previous versions and provides a highly configurable and extensible platform for displaying and editing tabular data. The control can work with a wide range of data types, including a simple one-dimensional array, a strongly typed business object, a DataSet, and any data provided by the DataSource control discussed earlier. Since the DataGridView was designed for editing and displaying data, it is no surprise that it supports many types of controls that can be placed into its cells, including buttons, checkboxes, comboboxes, images, links, and textboxes. The styling of the control is based on a layered style mode starting at the row level and continuing on to the column and finally the cell level, behaving in a similar fashion to cascading style sheets. The DataGridView comes with many built-in features that make working with it very easy. The auto sizing of columns based on their contents provides a clean presentation to varying size data and the row and column locking feature gives you the impression you're working in an Excel worksheet. Finally the control is programmable from the row level all the way down to the cell level, thereby allowing you to create functionality not possible with the DataGrid control.

Conclusion
As with any topic this large, I have only scratched the surface of what the new version of Windows Forms has to offer. The good news is that there is plenty of information out there on these topics and I have compiled a list (see the References section) of some helpful articles and Webcasts that should be useful in getting you started. This release provides many new enhancements that will enable you to build professional looking Windows Forms application easier then ever before, and with the release version due by the time this is printed, you have no excuses left - so get coding and good luck.

References

About Adam Calderon
Adam Calderon is a C# MVP and the Application Development Practice Lead at Interknowlogy. He is an accomplished software developer, author, teacher and speaker with over 14 years of experience designing and developing solutions on the Microsoft platform. Since joining InterKnowlogy, Adam has worked with many of their top tier clients designing and developing WPF, ASP.NET and ASP.NET AJAX solutions on the .NET 3.0 and .NET 3.5 platforms. His newly released book Advanced ASP.NET AJAX Server Controls that he coauthored with Joel Rumerman is chock-full of information gained from working with these clients.

MICROSOFT .NET LATEST STORIES
OpenSpan and TIBCO have announced a technology and business partnership designed to extend TIBCO solutions to desktop environments. The partnership will enable TIBCO Service-Oriented Architecture, Business Process Management and Business Optimization solutions to more rapidly integrate...
In a move that looks tailor-made for an antitrust suit, Microsoft says it’s going to give away a consumer security kit that it’s building code named Morro. It should be available in the second half of next year – probably more like mid-year. The freebie widgetry is supposed to de...
Tidal Software has announced Intersperse 8.0, a product that monitors J2EE and .NET applications and their transaction component performance to produce meaningful metrics for managing applications and high-level business processes. The product leverages a combination of lightweight Ja...
DataGuise has announced their first masking in place solution for multi-database environments such as Oracle, Microsoft SQL Server, and others. The dgSolution Suite provides secure masking of database content and is designed for the highest level of flexibility and functionality across...
The BlackBerryR Technical Webcast Series is designed to help BlackBerry administrators better manage and leverage the capabilities of their BlackBerry solution. Each webcast is packed with detailed technical information, covering topics that are relevant to you. Our on-demand webcasts ...
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE
BREAKING NEWS FROM THE WIRES
Simba Technologies Inc., industry's choice for standards-based relational and multi-dimensional data...