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

Article translations Article translations
Article ID: 315838 - View products that this article applies to.
This article was previously published under Q315838
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page

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.

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

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.
  1. Create a Visual Basic Windows Application project as follows:
    1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, and then click New Project.
    2. 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.
  2. Make sure that the Solution Explorer window is visible. If it is not, click Solution Explorer on the View menu.
  3. 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.
  4. 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;
    					
  5. 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(
          "select * from products", new SqlConnection(
          "server=(local);database=northwind;Integrated Security=SSPI"));
       sqlDS.Fill(ds);
       return ds;
    } 
    						
    NOTE: Your development system may require a different SQL connection string. If problems occur, consult your system administrator.
  6. 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.
  7. Add a Button control and a DataGrid control to the form as follows:
    1. On the View menu, click Toolbox to open the toolbox. Alternately, you can press the CTRL+ALT+X key combination to open the toolbox.
    2. In the Toolbox, click Windows Forms.
    3. Add a Button control to the form.
    4. Press the F4 key to access the property page for the Button control. Change the Text property to Show Products.
    5. Add a DataGrid control to the form.
  8. Add a project reference to the NWClassLib component as follows:
    1. In the Solution Explorer window, under MultiLangHowTo, right-click References, and then click Add Reference.
    2. On the Projects tab, double click NWClassLib. Notice that NWClassLib appears in the Selected Components list box.
    3. Click OK. Notice that NWClassLib appears under References for the Windows Application.
  9. 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)
    					

Complete Code Listing

Class1.cs Non-Inheritance Example

using System;
using System.Data;
using System.Data.SqlClient;

namespace NWClassLib
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class Class1
    {
        public static DataSet GetProducts()
        {
            DataSet ds = new DataSet();
            SqlDataAdapter sqlDS = new SqlDataAdapter(
                "select * from products", new SqlConnection(
                "server=(local);database=northwind;Integrated Security=SSPI"));
            sqlDS.Fill(ds);
            return ds;
        } 
    }
}
				

Class1.cs Inheritance Example

using System;
using System.Data;
using System.Data.SqlClient;

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

Form1.vb Inheriting System.Windows.Forms.Form

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    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 = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Show Products"
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(24, 48)
        Me.DataGrid1.Name = "DataGrid1"
        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 = "Form1"
        Me.Text = "Form1"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#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

WebForm1.vb Inheriting NWClassLib.Class1

Public Class Form1
    Inherits NWClassLib.Class1

#Region " Windows Form Designer generated code "

    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 = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Show Products"
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(24, 48)
        Me.DataGrid1.Name = "DataGrid1"
        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 = "Form1"
        Me.Text = "Form1"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#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
				

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.

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.
  1. Add a reference to the System.Windows.Forms.Form namespace in the Class Library before you modify the Visual C# code.
  2. In the Solution Explorer window, under NWClassLib, right-click References, and then click Add Reference.
  3. On the .NET tab, double-click System.Windows.Forms.dll. Notice that System.Windows.Forms.dll appears in the Selected Components list box.
  4. Click OK. Notice that System.Windows.Form appears under References in your Class Library component.
  5. Modify the Class1 declaration to inherit the Form class:
    public class Class1 : System.Windows.Forms.Form
    					
  6. 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
    					
  7. 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.
  8. Modify the second line as follows:
    DataGrid1.DataSource = GetProducts.Tables(0)
    					
  9. Repeat the steps in the Verify That It Works section.

Troubleshooting

  • 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 "Language Equivalents" section of the Microsoft .NET Framework Software Development Kit (SDK):
    Language Equivalents
    http://msdn2.microsoft.com/en-us/library/czz35az4(vs.71).aspx
  • 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.

REFERENCES

For more information, refer to the following Microsoft Web sites:
Basic Instincts: Using Inheritance in .NET, Part 1
http://msdn.microsoft.com/en-us/magazine/cc135806.aspx

Basic Instincts: Using Inheritance in .NET, Part 2
http://msdn.microsoft.com/en-us/magazine/cc301744.aspx

Properties

Article ID: 315838 - Last Review: February 28, 2014 - Revision: 4.8
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
Keywords: 
kbnosurvey kbarchive kbvs2005swept kbvs2005applies kbhowtomaster KB315838

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com