Introductory Example using OpenSpan and C#
Written by Andrea Ridgway   
Tuesday, 27 July 2010 00:00

OpenSpan Studio Code Sample: Demonstrates Using OpenSpan Interrogated Windows and Web Targets in a C# Visual Studio Project.

A sample GoogleCalc C# project shows how to reference an OpenSpan project and use the interrogated controls from the project in the C# code to start and stop the Windows Calc.exe and Web Google applications. The GoogleCalc C# project also enables running a Google search based on the contents of the Calculator results textbox. This Code Sample demonstrates the following:

  • Creating an OpenSpan project.
  • Using OpenSpan Studio to interrogate a Windows application (calc.exe) and a Web application (www.Google.com)
  • Building an OpenSpan project to use as a reference in a C# project.
  • Referencing an OpenSpan project.
  • Using OpenSpan adapters and interrogated controls in a C# project.

The C# project used in this sample is based on a Windows application template.

Requirements

To build and run this sample, you will need:

  • OpenSpan Studio Plug-In 4.5
  • Windows Calculator application (calc.exe)
  • Internet Explorer version 6+
  • Access to www.google.com

This article assumes that you have completed the OpenSpan Studio Basics training and are familiar with creating C# projects in Visual Studio.

Overview

The sample solution consists of two projects:

  • GoogleCalc project: A Visual C# Windows application which conducts a search in Google when the M+ button on the Calculator application is clicked. The search text is the output of the Calcualtor Results textbox.
  • OSCalcGoogleInt project: An OpenSpan Studio project which uses the OpenSpan Windows and Web adapters to interrogate Google and Calculator targets. This project is referenced by the main .NET project (GoogleCalc).

Notes:

  1. For this sample, the all application adapters are in a single OpenSpan project. However, you are not limitted to referencing a single OpenSpan project. You can reference multiple OpenSpan projects.
  2. It is a best practice to reference OpenSpan projects rather than referencing the built project assembly. If you reference the OpenSpan project assembly file you will need to manually copy OpenSpan required assemblies to the main .NET project's output directory.

Building Sample Projects

C# Project: GoogleCalc.cs

Begin by creating a new Visual Studio solution that includes a C# project as follows:

  1. Create a new solution and project. From the file menu in Visual Studio, select New | Project.
  2. In the New Project dialog, select Visual C# Windows project type with Console Application template. Name the project “GoogleCalc”.
  3. Select a Location folder for the solution and name the solution GoogleCalcSearch. Leave the "Create directory for solution" checkbox checked to create the solution folder and click OK to continue. The new solution and project display in the Solution Explorer.
  4. Modify the GoogleCalc Project Properties. On the Application tab, change the "Output Type" from Console Application to Windows Application.
  5. Save all solution files and close the GoogleCalc Project Properties page.

OpenSpan Project: OSCalcGoogleInt

  1. Add an OpenSpan project to the Solution. Right-click on the GoogleCalcSearch solution in the Solution Explorer and select Add | New Project.
  2. In the New Project dialog, select the OpenSpan | OpenSpan Studio project type with "Empty OpenSpan Project" template.
  3. Name the project OSCalcGoogleInt and use the default folder Location. Click Add to add the project and dismiss the Add New Item dialog.
  4. The OSCalcGoogleInt OpenSpan project displays in the Solution Explorer under the GoogleCalc C# project.
  5. Save the solution.
  6. Add a Windows application adapter to the project. In the Solution Explorer, right-click the OSCalcGoogleInt project and select Add | New Windows Application. The Add New Item dialog displays. Select OpenSpan Studio | General type and the Windows Application template. Name the item “Calc” to complete the Add New Item dialog. The Calc.os project item is added under the OpenSpan project and opens in the Designer.
  7. With the Calc.os project item open in the Designer, view or open the Properties window and set the adapter's Path property to: "calc.exe". (OpenSpan automatically completes the full path since this is a system application).
  8. Save the solution.
  9. Add a Web application adapter to the project. Right-click the OSCalcGoogleInt project and select Add | New Web Application. In the Add New Item dialog select OpenSpan Studio | General type and the Web Application template. Name the item “Google” to complete the Add New Item dialog. The Google.os project is added under the OpenSpan project and opens in the Designer.
  10. With the Google.os project item open in the Designer, view or open the Properties window and set the StartPage property to: www.Google.com.
  11. Save the solution.

Interrogate Applications

Interrogate the Calculator application: Results textbox and M+ button.

Begin by opening the Calc.os project item in the Designer pane and clicking the Start Interrogation button. OpenSpan launches the Calculator application.

  1. Click and drag the target icon from the Interrogation Form over the Calc results textbox and release the mouse. OpenSpan creates a textbox control and adds it to the Object Explorer: txtTextBox.
  2. Drag the Interrogator target over the M+ button in the Calc application. OpenSpan creates a button control and adds it to the Object Explorer: btnMPlus.
  3. The interrogated Calc controls, parent Calculator control and the ActiveXFactory appear matched in the Object Explorer.
  4. Stop the Interrogator by closing the Interrogation Form. OpenSpan closes the Calc application.
  5. Save the solution.

Interrogate the Google application: Search input textbox and Search button.

Begin by opening the Google.os project item in the Designer pane and clicking the Start Interrogation button. OpenSpan opens the Interrogation Form, launches Internet Explorer and navigates to the www.google.com page.

  1. Select the Create Global Web Page checkbox on the Interrogation Form. By using the Global Web Page option, OpenSpan creates controls for the interrogated pages and their child controls and does not include all the page parent controls. This interrogation option provides for more portability of the project between versions of Internet Explorer. See the OpenSpan Help documentation for more information on using the Global Web Page option.
  2. Click and drag the Interrogation target icon to the Google Search input textbox and release the mouse. OpenSpan creates an input text control and adds it to the Object Explorer: q.
  3. Drag the target over the Google Search button. OpenSpan creates a input submit button control and adds it to the Object Explorer: Google_Search.
  4. The interrogated Google controls, parent Form and Page controls, and the required factories (ActiveXFactory, MicrosoftHTMLFactory) appear matched in the Object Explorer.
  5. Stop the Interrogator by closing the Interrogation Form. OpenSpan closes Internet Explorer.
  6. Save all solution files.

Set OpenSpan Project Deployment Properties

  1. Right-click on the OpenSpan project in the solution Explorer and select Properties from the local menu. The Application, Build, and Deployment Properties tabs display.
  2. Open the Deployment tab. By default, OpenSpan creates deployment files when you build the project. In this case, the OpenSpan project will not be deployed (run through the OpenSpan Runtime application). Therefore, change the Create Deployment Package property to False. Leave default entries in the rest of the properties. Repeat this step for both the Debug the Release solution configurations.
  3. Save the solution.

Build OpenSpan Project

Build the OpenSpan OSCalcGoogleInt project so that it can be referenced by the .NET project. Select the Debug solution configuration from the toolbar, right-click on the OpenSpan OSCalcGoogleInt project in the Solution Explorer and select Build.

The build status displays in the status bar and build messages are published to the Output window. If the build is successful (“Build succeeded” status displays), a project assembly is created (e.g., OSCalcGoogleInt.dll) in the OpenSpan project output folder.

Note: The OpenSpan project contains OpenSpan adapters and interrogated controls which can be used directly in the .NET project. In order to access the OpenSpan objects, you must add the OpenSpan project as a reference to the .NET project. If you make changes to the OpenSpan project, such as adding or removing controls/adapters, modifying adapter or control properties, etc., you must rebuild the project so that the references are accessible to the main project.

Add References Required for C# Project

  1. Return to the main project, GoogleCalc.
  2. To use the OpenSpan adapters and controls in the C# code (GoogleCalc project), you must add the OpenSpan project as a reference to the C# GoogleCalc project. Right-click on the GoogleCalc C# project in the Solution Explorer and select Add Reference. In the Add Reference dialog, open the Projects tab and select the OSCalcGoogleInt project. Click OK to continue. The assembly is added to the GoogleCalc project and is listed under the References folder in the Solution Explorer.
  3. Along with the OpenSpan project, you must add references to the OpenSpan libraries for use by the .NET code. These assemblies are located in the “OpenSpan Plug-In for Microsoft Visual Studio 2008” folder. Right-click on the GoogleCalc C# project in the Solution Explorer and select Add Reference. In the Add Reference dialog, select the Browse tab and set the “Look In” folder to "OpenSpan Plug-In for Microsoft Visual Studio 2008" which is located under the Program Files\OpenSpan\ folder by default. Hold the CTRL key down and select the following assemblies:
  • OpenSpan.dll
  • OpenSpan.Adapters.dll
  • OpenSpan.Adapters.Web.dll
  • OpenSpan.Adapters.Windows.dll
  1. Click OK to add the references and dismiss the Add Reference dialog. The added references appear under the References folder for the GoogleCalc project in the Solution explorer.
  2. To enable running the GoogleCalc application without using a Console window or any other UI, add the System.Windows Forms .NET reference to the C# project. Right-click on the GoogleCalc C# project in the Solution Explorer and select Add Reference. In the Add Reference dialog, select the .NET tab and highlight the System.Windows.Forms component. Click OK to add the reference and dismiss the dialog. The reference is added to the GoogleCalc project and is listed in the project’s References folder:
  3. Save the solution.

Modify the GoogleCalc C# project - Program.cs

Complete the Program.cs Code

Open Program.cs and edit the code to start the Calc and Google applications as well initiate a Google search based on the Calculator results text (triggered when the user clicks the M+ button). The following list highlights the requirements for the program:

  1. Define two static methods for the Calc and Google adapters - OSCalcGoogleInt.Calc() and OSCalcGoogleInt.Google().
  2. Add the Start methods for the adapters – Calc.Start(), Google.Start(), and add event handlers for when either of the applications close.
  3. Add an event handler for when the user clicks the MPlus button to initiate the Google search.
  4. Add the Application.Run() method at the end of the Main() method so that the application continues to loop through the code waiting for events.
  5. Add an If-statement to check that the Google search page is created (matched) and then copy the text from the Calc Results to Google Search.
  6. Add a messagebox to display an error if the user attempts to initiate a search (using the M+ button) when the Google search page is not created.
  7. Add the Google.Stop() and Calc.Stop() methods along with the Application.Exit() method to terminate the applications.

An example of the code follows:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GoogleCalc
{
  class Program
  {   
    //Methods for the Calc and Google applications integrated in the OpenSpan project
    static OSCalcGoogleInt.Calc Calc = new OSCalcGoogleInt.Calc();
    static OSCalcGoogleInt.Google Google = new OSCalcGoogleInt.Google();
    static void Main(string[] args)
    {
      //Adapter Start methods added for each application
      Calc.Start();
      Google.Start();

      //Event handlers for stopping the Calc and Google applications
      Calc.Stopped += new EventHandler(StopAll);
      Google.Stopped += new EventHandler(StopAll);

      //Event handler for when user clicks the Calc M+ button
      Calc.btnMPlus.Click += new EventHandler(btnMPlus_Click);
      //Run method enables program to loop rather than exit after completing Main
      Application.Run();
    }
    // An event handler is called whenever the Calculator MPlus button is clicked 
    static void btnMPlus_Click(object sender, EventArgs e)
    {
      //Check added to ensure Google search page is created (matched) before sending Calc Results text
      if (Google.Google_Search.IsCreated)
      {
        // Copy the text from Calc and search for it.
        Google.q.Text = Calc.txtTextBox.Text;
        Google.Google_Search.PerformClick();
      }
      else
      {
        //If the Google search page is not matched, a message displays alerting the user
        MessageBox.Show("Navigate back to Google start page and try again.");
      }
    }
    //StopAll method added. When called, this will stop all the adapters and close this application
    static void StopAll(object sender, EventArgs e)
    {
      if (Google.IsRunning)
      Google.Stop();
      if (Calc.IsRunning)
      Calc.Stop();
      Application.Exit();
    }
  }
}

Last Updated on Monday, 11 October 2010 17:14