Click here to Skip to main content
Click here to Skip to main content

Localizable ErrorProvider

, 16 Nov 2010
Rate this:
Please Sign up or sign in to vote.
Make your ErrorProvider adhere to .NET localization standards

Introduction

The .NET ErrorProvider is a great way to tell the user that there's something wrong with the data he or she entered. However, if you are developing an application for multiple languages, it becomes very difficult to maintain the messages for the different languages.

You have to set ErrorText in code, and there is no easy way to maintain these texts, like you do for labels, buttons and the like.

Using the Code

My ErrorProvider is a subclassed ErrorProvider, that allows you to store one piece of text per control. If you want more than one message per control (e.g. "This field must be filled" and "No digits are allowed"), then you would need two ErrorProviders.

Here's an example for a form that has been marked Localizable True, with a TextBox called Email:

ControlWithErrorProvider.png

ErrorProviderInPropertyGrid.png

After you have entered text in the designer, you get generated code like this:

Private Sub InitializeComponent  
....
        Me.ErrorProvider.SetError(Me.Email, resources.GetString("Email.Error"))
        resources.ApplyResources(Me.Email, "Email")
        Me.Email.Name = "Email"
.... 

In the Validating event handler for the control, you put code like this:

Private Sub Email_Validating(ByVal sender As Object, _
	ByVal e As System.ComponentModel.CancelEventArgs) Handles Email.Validating
    If (Me.Email.TextLength = 0) _
    OrElse (Me.Email.Text.Contains("@") _
    AndAlso Me.Email.Text.Contains(".")) Then
        Me.ErrorProvider.HideError(Me.Email)
    Else
        Me.ErrorProvider.ShowError(Me.Email)
    End If
End Sub

History

  • 16 November 2010: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Pinx
Software Developer
Netherlands Netherlands
My first encounters with computers where Sinclair, TI99-4A, C64, Apple II. Have programmed ever since. Worked for a few logistic companies.
I have been a VB.NET programmer for quite some years. I work with a small firm, that specializes in supply chain planning for bulk products. We have our own application, Delivery+, that helps vehicle planners with their daily job.
I am married and I have two children. I am building an eco-friendly house and try to live green.

Comments and Discussions

 
Generalgood work PinmemberPranay Rana19-Dec-10 19:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 16 Nov 2010
Article Copyright 2010 by Pinx
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid