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;
- 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. - 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.
- 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.
- In the Toolbox, click Windows Forms.
- Add a Button control to the form.
- Press the F4 key to access the property page for the Button control. Change the Text property to Show Products.
- Add a DataGrid control to the form.
- 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.
- On the Projects tab, double click NWClassLib. Notice that NWClassLib appears in the Selected Components list box.
- Click OK. Notice that NWClassLib appears under References for the Windows Application.
- 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)
back to the top
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;
}
}
}
back to the top
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;
}
}
}
back to the top
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:
For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:
back to the top
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
back to the top
Verify That It Works
- Press the F5 key to run the application in debug
mode.
- When the form appears, click Show Products. The DataGrid displays all of the products from the Northwind products
table.
- 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.
- Add a reference to the System.Windows.Forms.Form namespace in the Class Library before you modify the Visual C#
code.
- In the Solution Explorer window, under NWClassLib, right-click References, and then click Add Reference.
- On the .NET tab, double-click System.Windows.Forms.dll. Notice that System.Windows.Forms.dll appears in the Selected Components list box.
- Click OK. Notice that System.Windows.Form appears under References in your Class Library component.
- Modify the Class1 declaration to inherit the Form class:
public class Class1 : System.Windows.Forms.Form
- 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
- 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. - Modify the second line as follows:
DataGrid1.DataSource = GetProducts.Tables(0)
- Repeat the steps in the Verify That It Works section.
back to the top
Troubleshooting
back to the top