API Guide > Code Examples > Example 3: MS Dev Studio VB.NET
Example 3: MS Dev Studio VB.NET
This example is a VB.NET application using the PTC Mathcad Prime API to exercise some fundamental methods and properties, and to demonstrate how to enable events.
There are two main pieces of code:
Form1.vb builds the user interface and implements callbacks for the VB user interface.
class1.vb implements the methods of the IMathcadPrimeEvents2 interface in MathcadPrimeComEvents2 class.
The Form1 class routine calls Button1_Click. This callback creates a MathcadPrimeApplication3 and a MathcadPrimeComEvents2 object. These PTC Mathcad Prime objects are part of the API, and are the only two objects that you create with "new". Note that in VB, we create ApplicationCreator().
After you create the MathcadPrimeComEvents2 object, you can implement the methods in terms of what you want to happen upon events, including OnExit and OnWorksheetClosed. There are a total of nine events.
1. Download the example here: VB.NET.
2. To run the example, execute MathcadAutomation.exe. The VB Win 64 Application dialog window opens:
3. Click Start Mathcad Prime and wait until PTC Mathcad Prime opens.
4. Click Show Mathcad Prime to bring PTC Mathcad Prime to the foreground.
5. Click Hide Mathcad Prime to hide PTC Mathcad Prime.
6. Click Open Worksheet in Prime. The Open dialog window opens. Select test.mcdx and click Open.
* 
The following events are enabled by default. To disable them, click Disable Events.
Click Save As and save the worksheet under a different name. A MathcadAutomation dialog window opens to inform you the worksheet was renamed. Dismiss this dialog window. A new MathcadAutomation dialog window opens to inform you a Save As event occurred. Dismiss this dialog window.
8. Click Open New Worksheet and ensure that a new worksheet entitled Untitled-n opens. Click Get Active Worksheet. Click the tab of the untitled worksheet to make it active.
9. To close your previously saved worksheet, and not this untitled worksheet, click Close Worksheet. A MathcadAutomation dialog window opens to inform you the worksheet was closed, and to give you its complete path. Dismiss this dialog window.
10. Click Close Worksheet and ensure that the last opened worksheet is closed. A MathcadAutomation dialog window opens to inform you which worksheet was closed. Dismiss this dialog window.
11. To close PTC Mathcad Prime, click Stop and Close Mathcad Prime.
* 
This example was generated using VS2019 and .NET 4.8
Was this helpful?