Gaining Control Over the Visual Studio 2005 Toolbox
For many developers, the Visual Studio 2005 toolbox contains a set of familiar controls—and then there are those controls that you might want to try, but never seem to find time to do it. The odd thing is that developers have requested these controls, and even occasionally ordered them from third parties.
This article focuses on the following controls, which seem to be missed by most developers:
- BackgroundWorker
- ContextMenuStrip (replaces ContextMenu)
- FlowLayoutPanel
- MaskedTextBox
- MenuStrip (replaces MainMenu)
- PropertyGrid
- SerialPort
- SplitContainer (replaces Splitter)
- StatusStrip (replaces StatusBar)
- ToolStrip (replaces ToolBar)
- ToolStripContainer
Each of these controls helps you in a particular way with the process of creating robust applications. For example, the BackgroundWorker control makes it considerably easier than ever before to create background threads. A background thread makes your application more attentive to user needs. Creating threads in the past could be cumbersome, but this new control makes it a snap.
In some cases, the new Visual Studio 2005 controls replace existing controls. The MenuStrip control replaces MainMenu. The reason for the replacement is that MenuStrip includes considerable new functionality that decreases the effort of creating fully-functional menus. In addition, this new menu includes a number of tricks that can help you to add flexibility to your application.
BackgroundWorker
The BackgroundWorker control helps you to create threads of execution. Many developers associate threads with background tasks that have nothing to do with applications, but threads actually are also useful for applications and make the application more responsive. Whenever the user requests that the application perform a time-consuming task, you should use a thread to handle that request. For example, printing is a time-consuming task. Moving printing to a thread allows the main application thread to return immediately and respond to other user requests.
User requests can cover a lot of territory, but the BackgroundWorker control handles two particular needs with aplomb:
- Reporting the progress of the background task. Even though the user probably trusts your application to complete a printing task, having a progress bar showing that the print job is underway will help maintain the user’s confidence. You can obtain reports from the background process by setting the WorkerReportsProgress property to true.
- Canceling a background task. In some cases, the user might decide to cancel a task in progress, such as printing. Users become downright agitated when an application refuses to cancel an action. Fortunately, the BackgroundWorker control also includes the WorkerSupportsCancellation property. Set this property to true and you can cancel a task at the user’s request.
Using the BackgroundWorker control is easy. Create an event handler for the DoWork event and place the thread code in it. Any time you need to activate the thread, call the BackgroundWorker control’s RunWorkerAsync() method. When you need to track the progress of the worker thread, create event handlers for the ProgressChanged and RunWorkerCompleted events.