ErrorProvider providing localization difficulties

The .Net framework provides several mechanisms for indicating invalid user inputs. You can use a MessageBox to indicate that the state of the control is not valid. A MessageBox can then provide the reason and remedies in the popup dialog box. Having several controls that needs user input this approach can be both cumbersome and intrusive for the user. Other approaches could be a balloon message, using the status panel or giving an audio clue of the invalid input. The .Net framework also provides a control named ErrorProvider, which is a control you can attach to any control that needs validation logic. If the error description string is set, an icon will appear next to the control. Hovering the mouse over the icon, a tooltip will display the error description string.

How to use the ErrorProvider

You can either drag the control from the ToolBox or you can programatically create an instance of the ErrorProvider. To modify the behavior, you can set the following properties:

Whenever the control's state is invalid, you set the error description string, by using the SetError method on the ErrorProvider control.

if (!validUserInput(textBox.Text))
{
  errorProvider1.SetError(this, "Invalid user input");
}
else
{
  errorProvider1.SetError(this, "");
}

This displays the error symbol next to the control.

Localization difficulties

This is a very useful control for providing non-intrusive feedback to the user. But, there is something that bothers every time I'm using this control. Why do you have to set a 'string' to display an icon?! I don't get it. Its the only control in the .Net framework that uses this "mechanism". Elsewhere its either a Start() method, Enable() method etc.

By combining a string with control display features it is difficult to use localization. In many applications it is evident to provide error descriptors in different languages, but when you cannot set the descriptor in whatever language without displaying the icon, you face additional work. Fortunately there are multiple options to come around this. One neat and clean solution is to create a user control that inherites the ErrorProvider. The new user control holds the local error descriptor string, which you can change whenever you'd like to without displaying the icon. Then you provide an Enable() method which calls SetError with the local string and displays the icon.

class NewUserControl : ErrorProvider
{
  private string errorDescriptor;

  ...

  string ErrorDescriptor
  {
    set
    {
      errorDescriptor = value;
    }
    get
    {
      return errorDescriptor;
    }
  }

  void Enable()
  {
    base.SetError(errorDescriptor);
  }
}

Another option is to use the ResourceManager. When using the ResourceManager, you add a resource file, .resx file,  to your project. Name it this way, Res.XX.resx, where the XXs are you language you're writing strings for, e.g. 'en' for english, 'da' for danish. When using the ResourceManager's GetString() method, you can receive the correct localized string. To use this method across the whole solution, a satellite assembly should be used. I'll make a post of this on another occation.


comments powered by Disqus