You are on page 1of 11

Microsoft Virtual Labs

Interop Forms Toolkit 1.0:


Bringing the Power of .NET to
Visual Basic 6, Utilizing Data
sources and Web Services
Interop Forms Toolkit 1.0: Bringing the Power of .NET to Visual Basic 6, Utilizing Data sources and Web Services

Table of Contents
Interop Forms Toolkit 1.0: Bringing the Power of .NET to Visual Basic 6, Utilizing Data
sources and Web Services ............................................................................................................ 1
Exercise 1 Create the InteropForm Library ...................................................................................................................2
Exercise 2 Create the web service .................................................................................................................................4
Exercise 3 Create a Visual Basic 6 Application and Utilize the InteropForm Library ..................................................7
Conclusion .....................................................................................................................................................................9
Interop Forms Toolkit 1.0: Bringing the Power of .NET to Visual Basic 6, Utilizing Data sources and Web Services

Interop Forms Toolkit 1.0: Bringing the


Power of .NET to Visual Basic 6, Utilizing
Data sources and Web Services
After completing this lab, you will be better able to:
Objectives  Leverage .NET forms in your existing Visual Basic 6 applictation
 Add data driven controls to your .NET form
 Create and consume a web service

Estimated Time to 45 Minutes


Complete This Lab
mergedVHD
Computer used in this Lab

Page 1 of 9
Interop Forms Toolkit 1.0: Bringing the Power of .NET to Visual Basic 6, Utilizing Data sources and Web Services

Exercise 1
Create the InteropForm Library
Scenario
We’ll start off by creating our InteropForm library, which will be called from our Visual Basic 6 application. This
form will include data from 2 tables, and will call a web service to retrieve data.

Tasks Detailed Steps


Complete the following 4 a. Open Visual Studio 2005.
tasks on: b. Select File | New Project. Then under Visual Basic | Windows, choose VB6
InteropForm Library
mergedVHD c. Name it InteropForm.
1. Create a new project
in Visual Studio
2005
2. Add a Data Source to a. From the Data Menu, click on Add New Data Source…
our project. b. Choose Database, and press Next.
c. Select New Connection…
d. Under Data source, press the Change button and select Microsoft Access
Database File. Press OK.
e. Under Database file name, press the browse button and select the file
c:\Demo\FPNWIND.MDB.
f. Press the Test Connection button. The connection test will succeed. Close that
dialog, and press OK.
g. From the “Choose Your Data Connection” screen, press Next.
h. You will now be presented with a pop up asking if you want Visual Studio to copy
the file to your project. Click No. We will leave the MDB file in it’s current
location.
i. Leave the connection string name alone and press Next.
j. Expand the Tables tree view.
k. Check both Products and Suppliers
l. Click Finish.
Note: We have just added the MDB resource to our project, allowing us to drag and
drop these tables into our project.
3. Create a Details a. Select the Solution Explorer.
control for the b. Double click InteropForm1.vb
Products table
Note: We will now see our Windows Form now in the designer.
c. Click back to the Data Sources tab.
d. Expand Products.
e. On the arrow next to Products, choose Details.
f. Click on ProductID, choose the arrow, and select [None].
g. Click on SupplierID, choose the arrow, and select [None].

Page 2 of 9
Interop Forms Toolkit 1.0: Bringing the Power of .NET to Visual Basic 6, Utilizing Data sources and Web Services
Tasks Detailed Steps
h. Click on CategoryID, choose the arrow, and select [None].
i. Now drag “Products” over to your form in the designer.
Note: Because we selected “Details”, we are given controls based upon the data type
of each element in the Products table.
4. Create a a. Drag our InteropForm1 form to resize it bigger. We need to add room to the right
DataGridView for of our current text boxes. Make it roughly double it’s current size.
the Suppliers table b. Under Data Sources, click on the arrow next to Suppliers, and choose
DataGridView.
c. Drag Suppliers to our InteropForm1 form, to the right of our last control.
d. Expand the DataGridView to fill in the area next to the text boxes.
e. Choose File | Save All. Press the Save button when Save Project dialog comes up.
Note: We now have a form that will display information in both a data grid and in text
boxes.

Page 3 of 9
Interop Forms Toolkit 1.0: Bringing the Power of .NET to Visual Basic 6, Utilizing Data sources and Web Services

Exercise 2
Create the web service

Scenario
This web service will return information back to our InteropForm Library. Creating the service and consuming it is
very easy, as illustrated below.

Tasks Detailed Steps


Complete the following 4 Note: For this part of the exercise, we are going to create a web service that can be
tasks on: called that will return us stock quotes for 3 companies, Microsoft, Contoso, and
Fabrikom. For now, we will use static values for these stock quotes. As you will see,
it’s very easy to create and consume a web service in Visual Studio 2005.
mergedVHD
a. Select File | Add | New Web Site.
1. Add a web service
b. Select ASP.NET Web Service
c. Change the Location to be C:\Documents and Settings\Administrator\My
Documents\Visual Studio 2005\WebSites\TestWebService
Note: We now see our Service.vb code. Visual Studio 2005 creates a simple
HelloWorld function to give you a head start.
d. Add the following code under the HelloWorld function

<WebMethod()>_
Public Function GetQuote(ByVal ticker As String) As
String

Select Case ticker


Case "msft"
Return "35.50"
Case "ctso"
Return "15.98"
Case "fabk"
Return "7.48"
Case Else
Return "1.15"
Exit Function
End Select

End Function
Note: As stated earlier, we are returning static values for our 3 different companies
we are tracking with this sample. In an actual web service, we would put in code to
get the real-time value of the stocks and return those values. For future web service
development, you can have many different functions available to the calling program,
all residing the same service. Just as we now have GetQuote and HelloWorld, we can
add many more funcitons to this service. You would then just call the appropriate
function that’s going to return the specific data you’re looking for.
2. Add the web service Note: Visual Studio 2005 has a built in web server to test your web based projects on.
into our InteropForm This prevents you from having to have IIS fully installed and configured, as well as
Library giving us a very controlled environment over your development projects. We need to
start our debugger here to find what port it starts our web service on, since it chooses
a different port every session visual studio is run. For the most part, if Visual Studio

Page 4 of 9
Interop Forms Toolkit 1.0: Bringing the Power of .NET to Visual Basic 6, Utilizing Data sources and Web Services
Tasks Detailed Steps
is left open, the same port will be used for that entire session.
a. Right-Click on TestWebService, and select Set As Startup Project
b. Click Debug | Start Debugging.
c. Click on OK when the “Debugging Not Enabled” prompt comes up.
Note: A new browser will open up with a Directory Listing to the TestWebService
folder.
d. Copy the URL in the address bar of the browser.
e. Close the browser.
f. Under Solution Explorer, Right-Click on InteropForm, and select Add Web
Reference.
g. Paste in the URL that we copied before into the URL text box, and add
Service.asmx?wsdl to the end of it.
Note: The entire url will end up looking similar to
http://localhost:1097/TestWebService/Service.asmx?wsdl
(The port number will in all likelihood be different than 1097)
h. Press the Go button.
i. Visual Studio will attach to the web service, and then show what methods are
available. Here, we see there is our newly created GetQuote method and the auto-
generated HelloWorld method.
j. Under Web reference name, name it myWebService, and press Add Reference.
Note: We will now be able to connect to this web service and utilize the functions
available through it.
3. Add a call to the web a. In the Solution Explorer, double click on InteropForm1.vb
service Note: This brings up our form in the designer
b. From the Toolbox, drag a button to the bottom left of the Form.
c. In the properties window, change the Text attribute to “Get Quote”.
d. From the Toolbox, drag a ComboBox to just above our Get Quote button.
e. Click on the ComboBox Tasks button “>” and choose Edit Items.
f. Add “msft”, “ctso”, and “fabk” to our String Collection Editor. Click OK.
g. From the Toolbox, drag a Label to the right of our button.
h. In the properties, change the Text property to “quote price”.
i. Double click on our Get Quote button. This will generate a click event.
j. Add the following code to the Button1_Click function

Dim ws As New myWebService.Service


Label1.Text = ws.GetQuote(
ComboBox1.SelectedItem.ToString() )
Note: This will call our web service and return the value to the text attribute of our
label, which will display on our interop forum. We’re passing in the stock ticker from
our selected combobox item into the webservice. As you can see, consuming and
calling a webservice acts just like any other object. Create an instance, and call a
member funciton. Very simple, and very powerful.
4. Add the InteropFrom Note: In order to get our form working with VB6, we need to add the InteropForm
wrappers wrappers.
a. Select Tools | Generate InteropForm Wrapper Classes.
b. Select Build | Build Solution

Page 5 of 9
Interop Forms Toolkit 1.0: Bringing the Power of .NET to Visual Basic 6, Utilizing Data sources and Web Services
Tasks Detailed Steps
Note: That’s it, we’re all set now. Because we generated the wrapper classes, then
built the solution, it’s now registered in Visual Basic 6 so we can add a reference to it.

Page 6 of 9
Interop Forms Toolkit 1.0: Bringing the Power of .NET to Visual Basic 6, Utilizing Data sources and Web Services

Exercise 3
Create a Visual Basic 6 Application and Utilize the
InteropForm Library
Scenario
To show the interoperability of our new .NET form with Visual Basic 6, we need a simple application that will
initialize and show an instance of our new form.

Tasks Detailed Steps


Complete the following 5 a. Click Start | Microsoft Visual Basic 6.0
tasks on: b. Standard EXE under the New tab.
c. Click the Open button
mergedVHD Note: This shows the designer. We are going to keep this app very basic.
1. Open Visual Basic 6
and Create a Project
2. Add a button that a. From the Toolbox, select the button icon, and draw a button on our From1.
will launch our b. Under the Properties section, change the Caption property to “Launch
InteropForm InteropForm”
c. Double click our Launch InteropForm button to create the click event.
Note: When this button is pressed, we will show the .NET form we created earlier.
3. Add the needed a. Select Project | References, and check InteropForm. Click OK.
reference Note: This adds the references to our project, so we can now start using our new
InteropForm.
4. Add the call to our a. In our Command1_Click event that we generated by double clicking the button,
InteropForm add the following code.

Dim frm As New InteropForm.InteropForm1


Frm.Show vbModal
Note: This adds the necessary references to our source.
5. Run the program Note: Now we’re going to run our program and see our VB6 application show our
new Visual Studio .NET 2005 form.
a. In Visual Studio 2005, make sure the TestWebService is selected as the start up
project. Click the Start Debugging button, which will launch in an instance of
IIS.
Note: We need to start the web service in order for our InteropForm to be able to
make calls to it.
b. Start our Visual Basic 6 application.
c. Click on the Launch InteropForm button
Note: We now see our InteropForm being displayed. Notice how our controls have
data filled in them based upon the tables we imported from our Access database file.
Use the next/back buttons at the top to switch through rows of data.
d. In the bottom left of the form, select one of the stock ticker symbols from our

Page 7 of 9
Interop Forms Toolkit 1.0: Bringing the Power of .NET to Visual Basic 6, Utilizing Data sources and Web Services
Tasks Detailed Steps
combo box, and click the Get Quote button.
Note: This calls the web service, which returns back the stock quote. We now have
successfully displayed information out of a database, and from a web service, all from
our new .NET form which was launched from Visual Basic 6.

Page 8 of 9
Interop Forms Toolkit 1.0: Bringing the Power of .NET to Visual Basic 6, Utilizing Data sources and Web Services

Conclusion
Making Visual Studio .NET 2005 forms available to Visual Basic 6 applications allows you to keep your existing
program, while leveraging the power of .NET, preventing you from having to rewrite applications. All of this was
able to be performed while only adding 2 lines of code to our Visual Basic 6 application. This will allow your old
applications to live on, while taking full advantage of .NET technologies and libraries.

Page 9 of 9

You might also like