C # localized Windows Forms

Source: Internet
Author: User
Walkthrough: localized Windows Forms

 

The Visual Studio project system provides considerable support for localized Windows Forms applications. The following are two ways to generate resource files using the Visual Studio development environment:

  • Make the project system generate resource files for localized user interface elements (such as text and images on the form. Then, the resource file is generated in the Affiliated Program set.

  • Add a resource file template and use the XML designer to edit the template. One of the reasons for taking the latter method is to generate a localized string that appears in the dialog box and error message. Then, you must write code to access these resources.

This walkthrough demonstrates the two procedures in a Windows application project.

You can also convert text files to resource files. For more information, see resource and resource file generator (resgen.exe) in text file format ).

Use Visual Studio to generate resource files

  1. Create a new windows application named "windowsapplication1. For more information, see How to: create a Windows application project.

  2. In the "properties" windowLocalizableSet propertyTrue.

    LanguageThe property has been set to "(default )".

  3. SetButtonDrag the control from the "Windows Forms" tab of the "toolbox" to the form, and then drag itTextSet propertyHello World.

  4. SetLanguageSet the attribute to "German )".

  5. SetTextSet propertyHallo Welt.

  6. SetLanguageSet the property to "French )".

  7. SetTextSet propertyBonjour Le Monde. If needed, you can adjust the size of the button to accommodate long strings.

  8. Save and generate a solution.

  9. Click the show all files button in Solution Explorer.

    The resource file appears under form1.vb, form1.cs, or form1.jsl. Form1.resx is the resource file of the default culture, which is generated in the main assembly. Form1.de-De. resx is a German-speaking resource file. Form1.fr-Fr. resx is a French-speaking resource file in France.

    In addition, you will see the files named form1.de. resx and form1.fr. resx. Visual Studio automatically creates these files to address a limitation in visual sourcesafe, that is, visual sourcesafe has to process the operation of adding new files to the project during the save operation .. The resx file is empty and does not contain any resources.

  10. Press F5 or select start from the Debug menu ".

    Now you can see greetings in English, French, or German in the dialog box, depending on the operating system UI Language.

    Note:

    The user interface language used in Windows is a function set by "currentuiculture. If your windows copy contains a multi-language user interface package (Mui), you can change the user interface language in the control panel. For more information, see Windows Server 2003, Windows XP & Windows 2000 Mui sites. If Mui is not installed, you can change the current UI culture programmatically, as described below.

    The following procedure describes how to set a user interface culture so that the application displays French resources. In actual applications, you do not need to hardcode the user interface culture in this way. The settings of the user interface culture depend on the user settings or application settings.

Set User Interface culture to view specific resources

  1. In the Code Editor, add the following code before the form1 declaration at the beginning of the module:

    Copy code

    ' Visual BasicImports System.GlobalizationImports System.Threading// C#using System.Globalization;using System.Threading;// Visual J#import System.Globalization.*;import System.Threading.*;

  2. Add the following code. In Visual Basic, callInitializecomponentUseNewFunction. In Visual C # and Visual J #, useForm1And is also callingInitializecomponentBefore the function.

    Copy code

    ' Visual Basic' Sets the UI culture to French (France).Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr-FR")// C#// Sets the UI culture to French (France).Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");// Visual J#// Sets the UI culture to French (France).System.Threading.Thread.get_CurrentThread().set_CurrentUICulture( new CultureInfo("fr-FR"));

  3. Save and generate a solution.

  4. Press F5 or select start from the Debug menu ".

    The form is always displayed in French. If you have changed the size of the button to hold a long French string, note that the button size is also kept in the French resource file.

Manually add resource files to the project and edit them

  1. Click Add new item on the project menu ".

  2. In the template box, select the program funding source file template. In the Name box, type the file name "winformstrings. resx ". The winformstrings. resx file will contain English backup resources. Each time the application cannot find resources that are more suitable for the user interface, these resources are accessed.

    The file is added to the project in Solution Explorer and automatically opened in the data view in the XML designer.

  3. In the data table pane, select data ".

  4. In the data pane, click an empty row and enterStrmessageIn the "value" column, enterHello World.

    You do not need to specify the string type or MIME type; they are used for objects. Type specifiers retain the Data Type of the stored object. If the object is composed of binary data, the MIME type specifier retains the base type (base64) of the binary information stored ).

  5. On the File menu, click Save winformstrings. resx ".

  6. Perform Step 1-5 twice to create two resource files named winformstrings.de-De. resx and winformstrings. fr-FR.resx, which have the string resources specified in the following table. The winformstrings.de-De. resx file will contain resources specific to German speaking. The winformstrings. fr-FR.resx file will contain resources specific to French speaking in France.

    Resource file name Name Value

    Winformstrings.de-De. resx

    Strmessage

    Hallo Welt

    Windows. Strings. fr-FR.resx

    Strmessage

    Bonjour Le Monde

Access manually added resources

  1. In the "Code Editor", import data at the beginning of the Code module.System. ResourcesNamespace.

    Copy code

    ' Visual BasicImports System.Resources// C#using System.Resources;// Visual J#import System.Resources.*;

  2. In the design view, double-click the button to display itsClickEvent Handler code and add the following code.ResourceManagerThe constructor has two parameters. The first parameter is the root name of the resource, that is, the name of the resource file without the culture or. resx suffix. The second parameter is the main assembly.

    In this walkthrough, no namespace is declared, soResourceManagerThe first parameter of the constructor can beProjectname. resourcefilerootname. However, in the actual application, you should set the defaultnamespace attribute. In this case, you need to declare the resource manager through the fully qualified root name (including its namespace) of the resource file. For example, if the default namespace isMycompany. myapplication. mycomponent, ThenResourceManagerThe first parameter of the constructor can beMycompany. myapplication. mycomponent. winformstrings.

    Copy code

    ' Visual Basic' Declare a Resource Manager instance.Dim LocRM As New ResourceManager("WindowsApplication1.WinFormStrings", GetType(Form1).Assembly)' Assign the string for the "strMessage" key to a message box.MessageBox.Show(LocRM.GetString("strMessage"))// C#// Declare a Resource Manager instance.ResourceManager LocRM = new ResourceManager("WindowsApplication1.WinFormStrings",typeof(Form1).Assembly);// Assign the string for the "strMessage" key to a message box.MessageBox.Show(LocRM.GetString("strMessage"));// Visual J#// Declare a Resource Manager instance.ResourceManager LocRM = new ResourceManager("WindowsApplication1.WinFormStrings",   System.Type. GetType("WindowsApplication1.Form1").get_Assembly());// Assign the string for the "strMessage" key to a message box.MessageBox.Show(LocRM.GetString("strMessage"));

    Note:

    By default,ResourceManagerThe object is case sensitive. If you want to perform a case-insensitive search (for example, to retrieve the same resource for "txtwelcome" and "txtwelcome"), you can set the ignorecase attribute of the resource managerTrue. However, to improve performance, it is best to always specify the correct case for the Resource Name. If you perform case-insensitive resource searches, this may cause performance problems.

  3. Generate and run the form. Click.

    The message box displays strings suitable for the user interface's regional settings; or if it cannot find resources in the user interface's regional, it displays strings from the backup resources.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.