|In this article, Jeff shows off some RAD features in Visual Studio 2005 to create a Holiday Wish List.|
Time Required: 1-3 hours
I don't know about you, but the one aspect of development I like least is the typing bit. Drag-and-drop development has been evolving over the years, but has rarely made it past being a good demo. Luckily for us, the Visual Studio 2005 editions' drag-and-drop feature set is more evolved than in the past, and works surprisingly well. It is the holiday season, and my gift to you is an overview of some of the features that really do take the pain out of some of the more tedious parts of development.
The application accompanying this article maintains a simple holiday wish list. It lives in the notification area, and the user can print out or email the list. My primary goal was to use the RAD features available in Visual Studio 2005 to reduce the amount of complexity and typing required.
Anyone that's done both Web and Windows development can probably appreciate the beauty of the HTML table. Sure, you can reliably put anything anywhere on a Windows dialog, but that isn't necessarily a good thing. The simple structure of the HTML table makes it easy to write functional user interfaces that are easy on the eyes.
Windows Forms in .NET 1.x introduced easy-to-use layout logic via anchoring and docking. These made manual auto-resizing logic a thing of the past, and gave the developer easy-to-use tools for creating a better user experience. Still, the developer had to make sure the controls' initial layouts were correct. This was more time consuming than it should have been.
Apparently some folks in Redmond thought the same thing, because the TableLayoutPanel was born. The table is really simple to use, and anyone with HTML table experience will be productive right out of the gate.
Figure 1: Simple table layout
TableLayoutPanels are really simple to use. Each cell can contain only one control, and controls can be anchored to any side (or sides) of the containing cell. Column and row sizes can be absolute, a percentage, or autosize. This combination makes dynamically-sized forms a snap to create.
In the image above, the table has three columns; the left and right are set at 48%, and the middle is 4%. Each text box is anchored to the left and right, and each label is anchored to the top and left. The rows are all autosized, which makes them as small as the largest control within them. Finally, the table is anchored on the form, so the controls grow or shrink and change their placement on the form when the user resizes it. The image below illustrates this:
Figure 2: Table in action
More complex table layouts are possible with familiar HTML table concepts like column spanning, row spanning, padding and margins, as well as table nesting. The tables below have examples of some of these features:
The Selected Gift table has four columns. The URL TextBox and button are in their own table. It's nested in a spanned cell with a zero margin, which gives it a fluid appearance within the outer table. The buttons in the Gifts table are in their own tables, anchored to either side of the outer table.
Figure 3: Complex table layouts
TableLayoutPanels are easy to use, and much better than previous methods required to accomplish the same result, but there are a few best practices that make life a little easier when using them. The Windows Forms 2.0 Layout link below has a short video explaining some of these.
Almost every application, no matter how small, has user settings. Maintaining these settings requires maintaining a settings file, wrapping those settings up so that they're consumable within the app, and creating the UI to adjust the settings. None of this is a major undertaking, but it takes time away from more important things. If you're coding for fun, you probably don't have much time to do it, and tasks like these are just begging to be automated.
You're in luck, my friend! This part of life has gotten a whole lot easier. I briefly covered the new user settings features in Visual Studio in my URL Buddy article. Basically, Visual Studio provides an interface for you to enter settings names and optional default values. It creates strongly-typed classes for accessing this data, so reading/writing these settings at run time is only a matter of accessing a strongly-typed property. These settings can be app- or user-level, with user-specific settings saved in the user's ApplicationData folder.
Visual Studio 2005's Property Bindings are similar to Visual Studio 2003's Dynamic Properties, which allowed for a read-only mapping to an appSettings entry in an application's config file. The big difference is that the new version supports two-way binding, so changes may be saved. That makes all the difference in the world! Mapping the value of a property to a setting is as easy as picking it from a list:
Figure 4: Mapping a user setting to a TextBox's Text property
Rounding out this little bit of heaven is automatic creation of settings right from the property grid. Let me give a scenario, because this little nugget is such a great time-saver I want to make sure it's remembered: You're in the middle of writing some very important code, but realize that you need the user's email address and want to save it between sessions. You go to your options form and create a TextBox, then open the PropertyBinding ("Application Settings" above) dialog. You click the drop-drown button next to Text, click "(New…)", then enter the name ("YourEmailAddress"), scope ("User") and optional default value ("firstname.lastname@example.org"). That's it! You now have a strongly-typed YourEmailAddress property on the application's Settings class. The value will be loaded automatically from the user's config file, saved very easily (a single method call for all settings), and the value is bound to the TextBox. And you didn't write a single line of code.
Figure 5: Adding a setting from the property grid
Form layout and application settings are two things that are required in nearly every application, but take away development time from the application's intended purpose. I've touched on just two of the many features in Visual Studio 2005 and the .NET Framework 2.0 that help the developer focus on what's important. A little exploration within Visual Studio will unearth even more goodies.