How to manage a multilanguage component solution in Visual C# (816175)
The information in this article applies to:
- Microsoft Visual C# 2005, Express Edition
- Microsoft Visual C# .NET (2003)
- Microsoft Visual C# .NET (2002)
For a Microsoft Visual Basic .NET version of this
article, see
317702. IN THIS TASKSUMMARYThis article describes how to manage a multilanguage
component solution using Visual C# .NET or Visual C# 2005. MORE INFORMATIONRequirementsThe following list outlines the recommended hardware,
software, network infrastructure, and service packs that are required: - One of the following operating systems:
- Microsoft Windows 2000 Professional (or
Server)
- Microsoft Windows XP Professional with the .NET Framework
installed
- Microsoft Windows Server 2003
- Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
- Microsoft SQL Server version 7.0 or later with Northwind
database.
This article assumes that you are familiar with the
following topics: - Implementation inheritance
- Windows Forms
- ADO.NET
Back to the
topManage a Multilanguage Component SolutionThis article describes how to create a solution that contains
the following three projects: - A Visual C# Windows application that uses methods in the
other two project classes.
- A Visual Basic .NET or Visual Basic 2005 Library project that accesses the
NorthWind database and returns a DataSet.
- A Visual C# Class Library project that has a method that
takes an ArrayList of string objects and returns a single string that is
concatenated by using an instance of the StringBuilder class.
To create the shell of a Visual C# Windows Forms
application in a solution, following these steps: - Click Start, point to Programs, and then point to either Microsoft Visual Studio .NET (for
Visual Studio .NET 2002), Microsoft Visual Studio .NET 2003
(for Microsoft Visual Studio .NET 2003), or Microsoft Visual Studio 2005 (for Microsoft Visual Studio 2005).
- Click Microsoft Visual
Studio .NET or Microsoft Visual Studio 2005.
- Click New Project, and then click
Visual C# Projects
Note In Visual Studio 2005, click Visual C#. - Under Templates, click
Windows Application.
- In the Name field, type
HowToMultiComp, and then click
OK.
- When the project is created, make sure that Solution Explorer is visible. If Solution
Explorer is not visible, press
CTRL+ALT+L.
You now have the shell of a Visual
C# Windows Forms application in a solution named HowToMultiComp.
To create a multilanguage component solution, add a Visual
C# Class Library, and then add a Visual Basic .NET or Visual Basic 2005 Class
Library. The Visual Basic Class Library must be added first. To add
the Visual Basic Class Library, follow these steps: - In Solution Explorer, right-click
Solution 'HowToMultiComp', point to Add, and
then click New Project.
- Click Visual Basic Projects.
Note In Visual Studio 2005, click Visual Basic. - Under Templates, click
Class Library.
- In the Name box, type
NWClassLib, and then click OK.
The
Visual Basic .NET or Visual Basic 2005 Class Library is added to the
solution.
Class1.vb is now open in the Editor
window. - Add two Imports directives at the top to gain shorthand dot notation access
to classes in the following namespaces:
Imports System.Data
Imports System.Data.SqlClient - To create and fill a DataSet with the data from the Northwind products table, add the following method to Class1 (you may have to
adjust the connection string for your computer):
Public Shared Function GetProducts() As DataSet
Dim ds As New DataSet()
Dim sqlDS As New SqlDataAdapter("select * from products", New SqlConnection("server=<network address of instance of SQL Server>;database=northwind;integrated security=sspi"))
sqlDS.Fill(ds)
return ds
End Function
Create the Visual C# Class Library. To do this, follow these steps: - In Solution Explorer, right-click
Solution 'HowToMultiComp', point to Add, and
then click New Project.
- Click Visual C# Projects.
Note In Visual Studio 2005, click Visual C#. - Under Templates, click
Class Library.
- In the Name box, type
SBClassLib, and then click OK.
The Visual C# Class Library is added to the solution.
Class1.cs is now open in the Editor window. - Add the following using directives at the top of the Class1.cs code sample to gain
shorthand dot notation access to the classes in the System.Text namespace and the System.Collections namespace:
using System.Text;
using System.Collections; - Add the following code in public class Class1, which uses an instance of the StringBuilder class to concatenate a single string that is contained in an ArrayList:
public static String BuildString(ArrayList arlStrings)
{
StringBuilder sb = new StringBuilder();
foreach(String s in arlStrings)
{
sb.Append(s);
sb.Append(", ");
}
return sb.ToString();
}
You can now create the Visual C# Windows Forms application
that consumes this component. To create the Visual C# Windows Forms
application, follow these steps:
- To add references to two Class Library components,
follow these steps:
- In Solution Explorer, right-click
References under HowToMultiComp, and
then click Add Reference.
- Under Projects, press and hold
the CTRL key, click NWClassLib, and then click
SBClassLib.
- With both projects selected, click
Select.
- When both the projects appear in the Selected
Components box, click OK.
The projects are now
listed under References for the Window Application.
- Form1.cs is open in Design view. If Form1.cs in not already
open, double-click Form1.cs in Solution
Explorer.
- Press CTRL+ALT+X to open the
toolbox, and then click Windows
Forms.
- Drag a button onto the form, and then press F4
to access the property page for the button.
- Change the Text property setting to
Show Products.
- Add another button, and then change the Text
property to Build String.
- Drag a DataGrid control onto the form, and
then drop it below the buttons. Resize the
DataGrid control so that it fills the remaining area on the
form.
- Double-click Show Products. Visual
Studio .NET automatically creates a Click event handler.
- Add the following code the the Click event handler:
dataGrid1.DataSource = NWClassLib.Class1.GetProducts().Tables[0]; This code sets the DataSource property of the DataGrid to the DataSet that was returned by the GetProducts method of the Class Library. - To create a
second Click event handler, double-click Build String.
- The following code takes the contents of each cell in the
first row of the DataGrid up to cell 7, and then adds the contents to an ArrayList. The ArrayList is then passed to the SBClassLibrary method, which returns a string that appears in a MessageBox function. An instance of the class is not necessary, because the
method was declared earlier by using the static keyword.
Add the following
code to the Click event handler:ArrayList arl = new ArrayList();
for(int i = 0; i <= 8; i++)
arl.Add(dataGrid1[0, i].ToString());
MessageBox.Show(SBClassLib.Class1.BuildString(arl)); Back to the
topComplete Code ListingComplete Code Listing (Class1.vb)Imports System.Data
Imports System.Data.SqlClient
Public Class Class1
Public Shared Function GetProducts() As DataSet
Dim ds As New DataSet()
Dim sqlDS As New SqlDataAdapter("select * from products", New SqlConnection("server=<network address of instance of SQL Server>;database=northwind;uid=sa"))
sqlDS.Fill(ds)
Return ds
End Function
End Class Back to the
topComplete Code Listing (Class1.cs)using System;
using System.Text;
using System.Collections;
namespace SBClassLib
{
public class Class1
{
public Class1()
{
}
public static String BuildString(ArrayList arlStrings)
{
StringBuilder sb = new StringBuilder();
foreach(String s in arlStrings)
{
sb.Append(s);
sb.Append(", ");
}
return sb.ToString();
}
}
}
Back to the
topComplete Code Listing (Form1.cs)using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace HowToMultiComp
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.DataGrid dataGrid1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Show Products";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 0);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Build String";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 24);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(296, 256);
this.dataGrid1.TabIndex = 2;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.dataGrid1,
this.button2,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
dataGrid1.DataSource = NWClassLib.Class1.GetProducts().Tables[0];
}
private void button2_Click(object sender, System.EventArgs e)
{
ArrayList arl = new ArrayList();
for(int i = 0; i <= 8; i++)
arl.Add(dataGrid1[0, i].ToString());
MessageBox.Show(SBClassLib.Class1.BuildString(arl));
}
}
}
Note The code should be changed in Visual Studio 2005. When you create a Windows Forms project, Visual C# adds one form to the project by default. This form is named Form1. The two files that represent the form are called Form1.cs and Form1.designer.cs. You write your code in Form1.cs. The Sesigner.cs file is where the Windows Forms Designer writes the code that implements all the actions that you performed by adding controls.
For more information about the Windows Forms Designer in Visual C# 2005, visit the following Microsoft Web site: Back to the
topVerify That It Works- Press F5 to run the application in debug mode.
- When the form appears, click Get
Products.
The DataGrid displays all the products from the Northwind products
table. - Click Build String.
A message box appears that shows the concatenated contents of the cells of the first row, through
the cell with an index of 7 (zero-based). - Press SHIFT+F5 to stop debugging and
return to Visual Studio .NET.
Back to the
topTroubleshootingMake sure that you do not confuse the syntax when you are
working with two languages. Visual C# is very particular, and the error
messages are not always as prompt or as intuitive as the error messages in
Visual Basic .NET or in Viusal Basic 2005. Therefore, you may want to bookmark the "Language Equivalents"
section of the .NET Framework software development kit to troubleshoot these
difficulties. Locate the following Help topic in MS
Help: ms:help://MS.VCC/MS.MSDNVS/vsintro7/html/vxgrfLanguageEquivalents.htm Back to the topREFERENCESFor more information, visit the following MSDN Web
site: Back to the top
Modification Type: | Major | Last Reviewed: | 1/5/2006 |
---|
Keywords: | kbHOWTOmaster kbinterop kbAppDev kbhowto KB816175 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|