Microsoft KB Archive/315838

= How to manage multilanguage Windows Forms solutions in Visual Studio .NET or in Visual Studio 2005 =

Article ID: 315838

Article Last Modified on 6/1/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q315838



IN THIS TASK
SUMMARY Requirements Create a Multilanguage Windows Application Complete Code Listing
 * Class1.cs Non-Inheritance Example
 * Class1.cs Inheritance Example
 * Form1.vb Inheriting System.Windows.Forms.Form
 * WebForm1.vb Inheriting NWClassLib.Class1

Verify That It Works Steps to Demonstrate Multilanguage Inheritance Troubleshooting REFERENCES



SUMMARY
This article demonstrates how to create a small, multilanguage Windows application. The main application is built in Visual Basic .NET or in Visual Basic 2005. This Visual Basic application uses a Visual C# Class Library component to illustrate multilanguage references and implementation inheritance.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows XP Professional, or Microsoft Windows XP Server with the Microsoft .NET Framework installed
 * Microsoft Data Engine (MSDE) or Microsoft SQL Server 7.0 or later with the Northwind database

This article assumes that you are familiar with the following topics:
 * Implementation inheritance
 * Windows Forms
 * Microsoft ADO.NET

back to the top

Create a Multilanguage Windows Application
These steps demonstrate how to create a small, multilanguage Windows application. When you click a button, you can view all products from the Northwind database in a DataGrid control.  Create a Visual Basic Windows Application project as follows:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, and then click New Project. Under Project Types, click Visual Basic Projects. Under Templates, click Windows Application. Rename the project MultiLangHowTo, and then click OK.

Note In Visual Studio 2005, click Visual Basic under Project Types.  Make sure that the Solution Explorer window is visible. If it is not, click Solution Explorer on the View menu. You now have the shell of a Visual Basic .NET or Visual Basic 2005 Windows Forms application within a solution named MultiLangHowTo. To make this a multilanguage solution, you must add a Visual C# Class Library.

In the Solution Explorer window, right-click MultiLangHowTo, point to Add, and then click New Project. Under Project Types, click Visual C# Projects. Under Templates, click Class Library. Rename the Class Library NWClassLib. Click OK to add this Class Library to the solution.

Note In Visual Studio 2005, click Visual C# under Project Types.  Class1.cs appears in the Editor window. Add the following namespace declarations to the top of this window. These declarations allow you to access various classes so that you can create and fill a DataSet object. using System.Data; using System.Data.SqlClient; </li>  Replace the Class1 method with the following code, which creates and fills a DataSet with all of the data from the Northwind products table: public static DataSet GetProducts {  DataSet ds = new DataSet; SqlDataAdapter sqlDS = new SqlDataAdapter(     &quot;select * from products&quot;, new SqlConnection( &quot;server=(local);database=northwind;Integrated Security=SSPI&quot;)); sqlDS.Fill(ds); return ds; } NOTE: Your development system may require a different SQL connection string. If problems occur, consult your system administrator. </li> You can now build the Visual Basic Windows Forms application that will consume this component. Form1.vb should already be open in Design View. If not, double-click Form1.vb in the Solution Explorer window.</li> Add a Button control and a DataGrid control to the form as follows:  On the View menu, click Toolbox to open the toolbox. Alternately, you can press the CTRL+ALT+X key combination to open the toolbox.</li> In the Toolbox, click Windows Forms.</li> Add a Button control to the form.</li> Press the F4 key to access the property page for the Button control. Change the Text property to Show Products .</li> Add a DataGrid control to the form.</li></ol> </li> Add a project reference to the NWClassLib component as follows:  In the Solution Explorer window, under MultiLangHowTo, right-click References, and then click Add Reference.</li> On the Projects tab, double click NWClassLib. Notice that NWClassLib appears in the Selected Components list box.</li> Click OK. Notice that NWClassLib appears under References for the Windows Application.</li></ol> </li>  Double-click the button. Visual Studio .NET automatically creates a Click event handler. Add the following code to the Click event handler: Dim objNW As New NWClassLib.Class1 DataGrid1.DataSource = objNW.GetProducts.Tables(0) </li></ol>

back to the top

Class1.cs Non-Inheritance Example
using System; using System.Data; using System.Data.SqlClient;

namespace NWClassLib {   ///     /// Summary description for Class1. ///    public class Class1 {       public static DataSet GetProducts {           DataSet ds = new DataSet; SqlDataAdapter sqlDS = new SqlDataAdapter(               &quot;select * from products&quot;, new SqlConnection( &quot;server=(local);database=northwind;Integrated Security=SSPI&quot;)); sqlDS.Fill(ds); return ds; }    } } back to the top

Class1.cs Inheritance Example
using System; using System.Data; using System.Data.SqlClient;

namespace NWClassLib {   ///     /// Summary description for Class1. ///    public class Class1 : System.Windows.Forms.Form {       public static DataSet GetProducts {           DataSet ds = new DataSet; SqlDataAdapter sqlDS = new SqlDataAdapter(               &quot;select * from products&quot;, new SqlConnection( &quot;(local);database=northwind;uid=sa&quot;)); sqlDS.Fill(ds); return ds; }   } } back to the top

Form1.vb Inheriting System.Windows.Forms.Form
Public Class Form1 Inherits System.Windows.Forms.Form


 * 1) Region &quot; Windows Form Designer generated code &quot;

Public Sub New MyBase.New

'The Windows Form Designer requires this call. InitializeComponent

'Add any initialization after the InitializeComponent call.

End Sub

'Form overrides Dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If       End If        MyBase.Dispose(disposing) End Sub

'The Windows Form Designer requires this code. Private components As System.ComponentModel.IContainer

'The Windows Form Designer requires this procedure. 'You can use the Windows Form Designer to modify it. 'However, do not use the Code editor to modify it. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent Me.Button1 = New System.Windows.Forms.Button Me.DataGrid1 = New System.Windows.Forms.DataGrid CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit Me.SuspendLayout '       'Button1 '       Me.Button1.Location = New System.Drawing.Point(24, 16) Me.Button1.Name = &quot;Button1&quot; Me.Button1.Size = New System.Drawing.Size(96, 23) Me.Button1.TabIndex = 0 Me.Button1.Text = &quot;Show Products&quot; '       'DataGrid1 '       Me.DataGrid1.DataMember = &quot;&quot; Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText Me.DataGrid1.Location = New System.Drawing.Point(24, 48) Me.DataGrid1.Name = &quot;DataGrid1&quot; Me.DataGrid1.Size = New System.Drawing.Size(256, 200) Me.DataGrid1.TabIndex = 1 '       'Form1 '       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.AddRange(New System.Windows.Forms.Control {Me.DataGrid1, Me.Button1}) Me.Name = &quot;Form1&quot; Me.Text = &quot;Form1&quot; CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit Me.ResumeLayout(False)

End Sub


 * 1) End Region

Private Sub Button1_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles Button1.Click Dim objNW As New NWClassLib.Class1 DataGrid1.DataSource = objNW.GetProducts.Tables(0) End Sub End Class Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

back to the top

WebForm1.vb Inheriting NWClassLib.Class1
Public Class Form1 Inherits NWClassLib.Class1


 * 1) Region &quot; Windows Form Designer generated code &quot;

Public Sub New MyBase.New

'The Windows Form Designer requires this call. InitializeComponent

'Add any initialization after the InitializeComponent call.

End Sub

'Form overrides Dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If       End If        MyBase.Dispose(disposing) End Sub

'The Windows Form Designer requires this code. Private components As System.ComponentModel.IContainer

'The Windows Form Designer requires this procedure. 'You can use the Windows Form Designer to modify it. 'However, do not use the Code editor to modify it. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent Me.Button1 = New System.Windows.Forms.Button Me.DataGrid1 = New System.Windows.Forms.DataGrid CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit Me.SuspendLayout '       'Button1 '       Me.Button1.Location = New System.Drawing.Point(24, 16) Me.Button1.Name = &quot;Button1&quot; Me.Button1.Size = New System.Drawing.Size(96, 23) Me.Button1.TabIndex = 0 Me.Button1.Text = &quot;Show Products&quot; '       'DataGrid1 '       Me.DataGrid1.DataMember = &quot;&quot; Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText Me.DataGrid1.Location = New System.Drawing.Point(24, 48) Me.DataGrid1.Name = &quot;DataGrid1&quot; Me.DataGrid1.Size = New System.Drawing.Size(256, 200) Me.DataGrid1.TabIndex = 1 '       'Form1 '       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.AddRange(New System.Windows.Forms.Control {Me.DataGrid1, Me.Button1}) Me.Name = &quot;Form1&quot; Me.Text = &quot;Form1&quot; CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit Me.ResumeLayout(False)

End Sub


 * 1) End Region

Private Sub Button1_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles Button1.Click 'Dim objNW As New NWClassLib.Class1 DataGrid1.DataSource = GetProducts.Tables(0) End Sub End Class back to the top

Verify That It Works

 * 1) Press the F5 key to run the application in debug mode.
 * 2) When the form appears, click Show Products. The DataGrid displays all of the products from the Northwind products table.
 * 3) Press the SHIFT+F5 key combination to stop debugging and to return to Visual Studio.

back to the top

Steps to Demonstrate Multilanguage Inheritance
To illustrate multilanguage inheritance, you must modify the sample slightly. Instead of creating an instance of the Class Library component, the application inherits from it.

The Form1 class currently inherits from the System.Windows.Forms.Form class. Because Microsoft .NET does not support multiple inheritance, you must move the Windows.Forms.Form class inheritance to the NWClassLib.Class1 class to inherit the Class Library component.

NOTE: It is not good n-tier architectural practice to inherit a user interface class in a data-tier component. However, you can modify the sample as follows to easily demonstrate another aspect of multilanguage solution management. <ol> <li>Add a reference to the System.Windows.Forms.Form namespace in the Class Library before you modify the Visual C# code.</li> <li>In the Solution Explorer window, under NWClassLib, right-click References, and then click Add Reference.</li> <li>On the .NET tab, double-click System.Windows.Forms.dll. Notice that System.Windows.Forms.dll appears in the Selected Components list box.</li> <li>Click OK. Notice that System.Windows.Form appears under References in your Class Library component.</li> <li> Modify the Class1 declaration to inherit the Form class: public class Class1 : System.Windows.Forms.Form </li> <li> The Visual Basic .NET or Visual Basic 2005 Form1 class is now ready to inherit Class1. Because Class1 inherits the System.Windows.Forms.Form class, the MultiLangHowTo.Form1 class will also inherit System.Windows.Forms.Form through its inheritance of Class1.

In the Editor window, click Form1.vb. Under the class declaration, replace System.Windows.Forms.Form with NWClassLib.Class1. The updated code should appear as follows: Inherits NWClassLib.Class1 </li> <li> Comment out the first line in the Click event handler as follows: 'Dim objNW As New NWClassLib.Class1 NOTE: Alternately, you can place the cursor on the line and then click Comment out the selected lines on the toolbar to comment out a line of code. </li> <li> Modify the second line as follows: DataGrid1.DataSource = GetProducts.Tables(0) </li> <li>Repeat the steps in the Verify That It Works section.</li></ol>

back to the top

Troubleshooting
<ul> <li>When you work with two languages, it is easy to confuse syntax. Visual C# syntax does not allow a wide margin of error. Furthermore, Visual C# .NET error messages are not always as prompt or as intuitive as Visual Basic .NET error messages. To troubleshoot these difficulties, refer to the &quot;Language Equivalents&quot; section of the Microsoft .NET Framework Software Development Kit (SDK):

Language Equivalents

http://msdn2.microsoft.com/en-us/library/czz35az4(vs.71).aspx

</li> <li>If you want to use implementation inheritance, study object-oriented principles of design, and consider application architecture in advance. A common mistake is to put code in a main application that you need to inherit in a Class Library. In this scenario, you cannot inherit the main application in a Class Library because of circular dependencies. For example, try to add a reference to the MultiLangHowTo project in the NWClassLib project. You receive the following error message:

A reference to 'MultiLangHowTo' could not be added. Adding this project as a reference would cause a circular dependency.

</li></ul>

back to the top

<div class="references_section">