Lab: Creating a multilingual Windows Application
In this lab, you will create a multilingual Windows application using the Visual Studio .NET Integrated Development Environment (IDE)
Task 1 – Creating a new Visual Studio project
To create a new Windows application project named swagath using Microsoft Visual Studio .NET
- Navigate to Start | Programs | Microsoft Visual Studio .NET 2003 | Microsoft Visual Studio .NET 2003
- Select File | New | Project menu command or press Ctrl + Shift + N
- Click Visual Basic Projects in Project Types list
- Click Windows Application in Templates list
- Type “swagath” in the Name field (you can change the location, if you prefer)
- Visual Studio .NET creates a default form named Form1.vb. It also creates swagath.vbproj, swagath.sln and AssemblyInfo.vb files.
Task 2 : Saving the project with Unicode encoding
We will be using Unicode text for the Windows forms application. Hence we need to save the Form1.vb file with Unicode encoding.
- Select File | Save Form1.vb As … menu command
- In the dialog box click the small down traingle in front of the Save button. It will show a drop-down list with Save and Save with Encoding options. Selct Save with Encoding option.
- Click and select Unicode – Codepage 1200 from the dropdown list under Encoding
- Leave the default Current Settings for Line endings as it is
- Click OK
- Select File | Save All menu command or press Ctrl + Shift + S
Task 3: Adding controls to the form
Click on the Form1 and press F4, which will take the focus to the Properties window for the control (the Form1, in this case).
- Set the following property for the Form1: Text - (delete the default text of Form1) Size Width – 450 Height – 216
- Add a label control to the form
- Set the following properties for the label: Text - Font size – 12 Location X – 16 Y – 24 Size Width – 160 Height – 24 Name – lblName
- Add a textbox control to the form
- Set the following properties for the textbox: Text - Font size – 12 Location X - 180 Y – 24 Size X – 240 Y – 26 Name – txtName
- Add a Button control to the form below the textbox
- Set the following properties for the button: Text - Font size – 12 Location X – 180 Y – 64 Size Width – 120 Height – 40 Name – btnOk
- Add another label control to the form just below the button
- Set the following properties for the label: Text - Font size – 12 Location X – 16 Y – 120 Size Width – 406 Height – 24 Name – lblWelcome
A sample screenshot of the form at this stage will look like this –
This screenshot was taken after selecting the controls on the form.
Note: We have not used any text for any of the controls. This is very important for multilingual applications. All the texts must come from language dependent resource files.
Task 4: Making the Form localizable and adding languages
- Click on the Form1 and press F4 to open the properties window
- Select True for the Localizable property from the dropdown list
- Select Hindi for the Language property from the huge list of languages (for the time being, select only Hindi and not Hindi (India))
- Click on the Solution Explorer
- Click on the Show All Files Icon (fourth from the left) or select Project | Show All Files. Now the solution explorer will show Form1.resx and Form1.hi.resx files
- Click on the form. Press F4. Select English for the Language property from the huge list of languages (for the time being, select only English and not English (country name)). You will notice Form1.en.resx being added in the solution explorer.
Task 5: Adding Menu control
- Click on the Form1 Press F4.
- Select (Default) for Language. This step is very important. Any control that is being added to the form should be added while the form is in the invariant culture.
- DoubleClick on the MainMenu icon in the toolbox, which will add a menu control by name MainMenu1
- Type Language for top level menu
- Type English below that. Name it mnuEnglish using the Properties window
- Type Hindi हिंदी below that. Name it mnuHindi.
Task 6: Coding the form
- Add the following code at the top, before PublicClass Form1
Imports System.Globalization
Imports System.Threading
Imports System.Text
Imports System.Resources
- Add the following code above the Public Sub Form1_Load
Dim strWelcome As String
Dim ci As String
Dim strMsg As String
- Add the following codes for mnuEnglish and mnuHindi click events
Private Sub mnuEnglish_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles mnuEnglish.Click
ci = "en"
SetUICulture(ci)
txtName.Text = ""
lblWelcome.Text = ""
End Sub
- Add the following code to the Form1_Load event
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
'set English culture
ci = "en"
SetUICulture(ci)
txtName.Text = ""
lblWelcome.Text = ""
End Sub
This is just to make sure that the application starts with some culture.
- Add the following procedure before the last End Class statement.
Sub SetUICulture(ByVal culture As String)
' get a reference to the ResourceManager for this form
Dim rm As System.Resources.ResourceManager = New _
System.Resources.ResourceManager(GetType(Form1))
Try
'set the culture as per the selection and
'load the appropriate strings for button, label, etc.
System.Threading.Thread.CurrentThread.CurrentUICulture = New _
System.Globalization.CultureInfo (culture)
lblName.Text = rm.GetString ("lblName.Text")
btnOk.Text = rm.GetString ("btnOk.Text");)
strMsg = rm.GetString ("strMsg");)
Me.Text = rm.GetString("Form1.Text")
Catch
'display a message if the culture is not supported
'try passing bn (Bengali) for testing
MessageBox.Show("Locale '" & culture & "' isn't supported", _ _
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Task 7: Adding the resources
The Visual Studio IDE has made the task of adding language dependent resources quite easy. The Form1.en.resx and Form1.hi.resx files are nothing but the resource files in the XML form.
- Double click on Form1.en.resx file in the Solution explorer. This open the resource file
- Use the Data view (by default VS IDE opens in data view. If not, select Data view)
- Add the following for name and value
lblName.Text Enter your name
btnOk.Text OK
strMsg Welcome
- Double click on Form1.hi.resx file in the solution explorer. This will open the resource file
- Use the Data view
- Add the following for name and value
lblName.Text आप का नाम ?
btnOk.Text ठीक
strMsg सुस्वागत
Task 8: Execution
- To run the application, select Debug | Start or press F5
- Enter your name in the textbox and press OK button
- Change the language to Hindi, enter your name in Hindi and press the button
Task 9: Add more languages
- Add any number of languages as you like.