Visual Studio Integration

This example illustrates how to use Ranorex within a simple Visual Studio C# console application. It shows how to create a new Visual Studio C# console application and how to start and automate the Windows Calculator.

Note: The sample works with both Microsoft Visual Studio 2005 and 2008.

Create a new Visual Studio project

Start Microsoft Visual Studio. From the 'File' menu click 'New Project' to open the New Project Dialog. In this example we use C# as programming language. Choose another language if you would like to implement Ranorex code in VB.NET or Visual C++.

Create a new Console Application with Microsoft Visual Studio

Add Ranorex core assemblies as references

Right-click the 'References' folder within the projects 'Solution Explorer' and open the 'Add Reference' dialog. Select the components 'System.Drawing', 'Ranorex Core' and all 'Ranorex Plugin'-References.

Add new references ...
... select System.Drawing and Ranorex.Core

Write some Ranorex automation code

Open the file 'Program.cs' and add following 'using' statement to your existing using section:

C#

using System.Drawing;
using Ranorex;

VB.NET

Imports System.Drawing
Imports Ranorex
Mark the main thread with the attribute [STAThread] and change the return value of the Main function to int.

C#

[STAThread]
static int Main(string[] args)

VB.NET

<STAThread> _
Public Shared Function Main(args As String()) As Integer
Add the following code lines to the 'Main' routine of the class 'Program':

C#

int error = 0;

try
{
    System.Diagnostics.Process.Start("calc.exe");
    Form form = Host.Local.FindChild<Ranorex.Form>("Calculator");
    form.Activate();

    Button button = form.FindSingle<Ranorex.Button>(".//button[@controlid='132']");
    button.Click();

    button = form.FindSingle<Ranorex.Button>(".//button[@controlid='92']");
    button.Click();

    button = form.FindSingle<Ranorex.Button>(".//button[@controlid='133']");
    button.Click();

    button = form.FindSingle<Ranorex.Button>(".//button[@controlid='121']");
    button.Click();
}
}
catch (RanorexException e)
{
    Console.WriteLine(e.ToString());
    error = -1;
}

return error;

VB.NET

Dim returnError As Integer = 0

Try
  System.Diagnostics.Process.Start("calc.exe")
  Dim form As Form = Host.Local.FindChild(Of Ranorex.Form)("Calculator")
  form.Activate()

  Dim button As Button = form.FindSingle(Of Ranorex.Button)(".//button[@controlid='132']")
  button.Click()

  button = form.FindSingle(Of Ranorex.Button)(".//button[@controlid='92']")
  button.Click()

  button = form.FindSingle(Of Ranorex.Button)(".//button[@controlid='133']")
  button.Click()

  button = form.FindSingle(Of Ranorex.Button)(".//button[@controlid='121']")
  button.Click()
Catch e As RanorexException
  Console.WriteLine(e.ToString())
  returnError = -1
End Try
Return returnError

Build and start the application by pressing F5.