(How)
C^# You Are - 29 April 2007
This week's topic is more about WinForm controls.
- What purpose does the ErrorProvider serve? Answer
The ErrorProvider is used to provide feedback to a user about an
error in a form in a non-intrusive way. When added to a form all the controls
on the form can be assigned an error message. When enabled the error is displayed
next to the control, generally as an icon. This provides feedback to the user
that something is wrong but does so in a non-intrusive manner. Generally the
form can not be closed until the error provider(s) are disabled.
- How do you add help support to a control? Answer
The HelpProvider can be used to add help to any control on a form.
Similar to the ErrorProvider this component associates help text
with each control. When context-sensitive help is used the help text associated
with the selected control (through the provider) is displayed.
Note that the help can be as simple as a string or as complex as an entire help
file page.
- What is an extender provider? Answer
An extender provider extends a component by adding new properties to it. The
provider itself is added to the parent form. The provider then identifies
(to the designer) the type(s) that it supports. Any supported type is extended
to include the property(ies) exposed by the provider. In the property grid
the property shows up on the component itself. However an extender provider
actually exposes methods to get and set the property for a component. The
property grid silently converts the property get and set to the appropriate method
calls. The provider is responsible for mapping each property value to the
appropriate control. This is normally done through a dictionary.
For example when an ErrorProvider is added to a form then all controls
on the form will then show an Error property in the property grid.
However, behind the scenes, whenever the Error property is retrieved
or set then the grid silently calls the GetError or SetError
method. If you programmatically get or set the provider properties then you
will need to use the methods.
- How can you add a tooltip to a button? Answer
The ToolTip component is yet another extender provider. It
can be used to add tool tips to any control that does not support them directly.
When the mouse hovers over the control the tool tip, if any, is displayed.
- What is a notification icon? Answer
A notification icon (implemented by NotifyIcon) is the little icon
shown in the task bar tray in Windows. The icon is generally used to represent
programs that do not have a main window or do not have a window that is currently
active. When the icon is double clicked the main window for the associated
application is generally opened. When right-clicked the user normally gets
a context menu of options. It is a great way to expose your application to
users without requiring a full window be open always.
- How can you perform validation in WinForms like you can in ASP.NET? Answer
You can't technically. WinForms, supposedly, has superior validation support
because it is stateless. However many people like how validators work.
The main problem with WinForms is that there are a variety of ways to handle validation
and none of them map nicely to validators. Nevertheless several third-party
libraries support WinForms validators and work almost identical to ASP.NET.
Refer to the series of articles from MSDN Magazine on how to implement validators
in WinForms, here.
- What is a task dialog? Answer
First a little background. Prior to .NET you had to define a dialog resource
and associate controls with it in order to display a dialog on the screen.
In most cases this is not a big deal. However for simple things like asking
a question or displaying simple messages it would require a lot of extra work.
Enter message box. Message box is basically a system-provided dialog box.
Message box can be used to display a couple of buttons, an optional icon and some
text. Although you could extend message box even further it was generally
better to just create a custom dialog.
A task dialog is new to Windows Vista. It is basically a message box on steroids.
It takes the functionality of a message box and extends it to take advantage of
all the new UI guidelines and functionality available in Vista. Rather than
limiting a dialog to a couple of buttons, an icon and some text you can now display
multiple areas of text along with images and buttons. Task dialogs will replace
message boxes in Vista.
- How do you prevent a user from typing a value into a combo box that is not already
maintained in the combo box list? Answer
By default combo boxes allow a user to either select an item from the list or type
another value. This is formally known as a drop down. To prevent a user
from entering anything other than what is on the list then you should instead use
a drop down list. This can be done by setting the DrawMode property
of the combo box to DropDownList.
- What is the difference between a Menu and a MenuStrip? Answer
Age for the most part. Both controls represent menus in a WinForms app.
Menu is a standard Windows menu with some additional functionality.
MenuStrip is a menu for v2 applications that greatly extends the
functionality of menus. Menu is still available in v2 apps
but you should prefer MenuStrip. MenuStrip
allows you to add just about anything to a menu such as drop down lists, text boxes
or even buttons. Of course most of the time this isn't useful. MenuStrip
also allows you to associate icons with each menu item and do other things that
the standard Menu control can not do.
- How do you format the contents of a text box? Answer
TextBox is a wrapper around one of the oldest controls in Windows.
Its functionality, although slightly enhanced over the years, is still very limited.
You can change the foreground color, background color and font information but the
changes apply to the entire control. If you want to change the formatting
of only portions of the text then you have to "upgrade" to RichTextbox.
RTB also derives from the basic text box but adds functionality
for embedding images and objects created from other programs, selective formatting
of text and a host of other feature. RTB is the basis of
Word (or perhaps vice versa). RTB provides support for regular
text and also RTF (which is the original format of Word). This opens up a
whole new level of capabilities beyond a simple text box.