Ranorex Help Center How can we help you?

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 Microsoft Visual Studio 2005 and later versions.

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

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

Add new references ...


Select System.Drawing and Ranorex.Core

... 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:
using System.Drawing;  
using Ranorex;  



Imports System.Drawing  
Imports Ranorex  



Mark the main thread with the attribute [STAThread] and change the return value of the Main function to int.
[STAThread]  
static int Main(string[] args)  


  



<stathread> _  
Public Shared Function Main(args As String()) As Integer  



Add the following code lines to the 'Main' routine of the class 'Program':
int error = 0;  
  
try  
{  
    System.Diagnostics.Process pr = System.Diagnostics.Process.Start("calc.exe");  
    Form form = Host.Local.FindSingle <ranorex.form>("form[@processname='"+pr.ProcessName+"']");  
    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;  



Dim returnError As Integer = 0  
  
Try  
  Dim pr As System.Diagnostics.Process = System.Diagnostics.Process.Start("calc.exe")  
  Dim form As Form = Host.Local.FindSingle(Of Ranorex.Form)("form[@processname='" & pr.ProcessName & "']")  
  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.