Click here to Skip to main content
13,247,005 members (77,150 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 26 Apr 2012

Localized String in Form's resx with Designer Support

, 26 Apr 2012
Rate this:
Please Sign up or sign in to vote.
A component to add localized strings to a Form's res'x.


Visual Studio's designer offers a nice tool to localize your dialogs. Simply set the Form's language to whatever you want, and start editing the labels, menus, etc.

However when it comes to localizing strings that are not present on your form (only in code, such as message box texts), there is no support for that. If you manually save these strings in the Form's resx, VS will warn you that you shouldn't be modifying it. So you have to store these localized strings somewhere else (in general in a resx file that is global to the project) which is a poor design since they are related to the form. Also a single resx is hard to maintain when many people are working on a project.

The tool presented in this article addresses two problems at once.

  1. Save the localized strings within the form's resx.
  2. Have designer support, for easy translation.


The idea is to take advantage of the localization mode present in VS. How would you do it for a single string? Simply put a hidden label on your form (problem 1 solved), and then read its Text property from your code when you need it. Every localized control on a form have their string stored in the form's resx (problem 2 solved).

Now they are some drawbacks: what if you have 30 strings in your code? It would be cumbersome to put 30 labels on your form. And why would you need to position and size your string containers?

Let's simply use a Control that can hold many localized strings. For example a TabControl, can contain several TabPages, each having a localized tab text. Or a ListView can contain several Columns, each having a localized header.

Cool, now what about the position and the size? We don't need it. So we will make our control not beign a Control, but a Component instead (such as Timer). They have no graphical representation, so no need to position and size them.

We also want that the strings editor pops up when double-clicking the component, so we'll add some designer support code.

Using the code

First, we define a LocalizableString class that extends Component, so we get some designer and localization support. This will be the individual strings. The very important thing here is the Localizable attribute set to true.

public class LocalizableString : Component
    private string text;
    public string Text
        get { return text; }
        set { this.text = value; }
    public override string ToString()
        return text;

And then we create our main component LocalizableStringsControl, the one we will be putting on our form. It holds a collection of LocalizableStrings. The code presented here would be sufficient if we would not want to be able to double-click the component to start editing.

public partial class LocalizableStringsControl : Component
    List<LocalizableString> strings = new List<LocalizableString>();

      Editor(typeof(CollectionEditor), typeof(UITypeEditor)),
    public List<LocalizableString> Strings
        get { return strings; }

    public LocalizableStringsControl()

    ... (designer support code)


Version 1.0


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


About the Author

Switzerland Switzerland
No Biography provided

You may also be interested in...


Comments and Discussions

SuggestionSuggestion: Multiline string editor at design time Pin
Heinz Kessler18-Jul-15 8:17
memberHeinz Kessler18-Jul-15 8:17 
GeneralGreat Job, Just what I was looking for. Pin
Mark Schaffer25-Oct-13 8:56
memberMark Schaffer25-Oct-13 8:56 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 26 Apr 2012
Article Copyright 2012 by coffee_machine
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid