Windows form programming Walkthrough: localized Windows Forms (typical examples of localization)

Source: Internet
Author: User
Windows form Programming
Walkthrough: localized Windows Forms

The Visual Studio project system is a localized Windows Forms ApplicationProgramProvides considerable support. 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 writeCodeTo 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 And .

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 Site. 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, call the New function before calling the initializecomponent function. In Visual C # and Visual J #, you must use form1 and call the initializecomponent function.

    copy the 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 propertyTrue. 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.

Reference from:
MS-help: // Ms. msdnqtr. v80.chs/ms. msdn. v80/ms. visualstudio. v80.chs/dv_mancli/html/9a96220d-a19b-4de0-9f48-01e5d82679e5.htm

Related Article

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.