Using Measurement Studio ActiveX Controls in Visual Studio .NET

Publish Date: Sep 06, 2006 | 82 Ratings | 1.95 out of 5 |  PDF

Overview

National Instruments is committed to helping Visual Studio and Visual Studio .NET programmers develop measurement applications in the language of their choice. With Measurement Studio 7.0 or later, you can use native .NET class libraries for your .NET measurement and automation applications. If the library or functionality that you need does not yet exist in the Measurement Studio .NET class libraries, you can use the Measurement Studio ActiveX controls instead. Visit ni.com/mstudio for information about the .NET libraries that are available with Measurement Studio.

This document explains environment and programming differences you will encounter as you use the Measurement Studio ActiveX controls in Visual Basic .NET or Visual C# .NET. This document does not discuss changes to Visual C++ .NET. If you are a Visual C++ user, take advantage of the Measurement Studio tools customized for Microsoft Visual C++ .NET programmers – integrated wizards, measurement classes, and measurement-specific user interface controls.


Table of Contents

  1. Introduction to Visual Studio .NET
  2. Loading the Measurement Studio ActiveX Controls
  3. Browsing Properties
  4. Browsing Generated Code
  5. Changes to the Visual Basic Language
  6. Variants versus Objects
  7. Event Handling
  8. Getting Help with the Measurement Studio API
  9. Distributing Visual Studio .NET Applications
  10. Conclusion

1. Introduction to Visual Studio .NET

Microsoft Visual Studio .NET, Microsoft’s next-generation application development environment, offers enhancements over previous versions of Visual Studio and Visual Basic. Visual Studio .NET features an integrated development environment for Microsoft programming languages, Windows Forms and controls for creating user interfaces, the .NET Framework for consistent programming across .NET languages, and the newly designed Visual Basic .NET and Visual C# .NET programming languages.

Visual Studio .NET

Microsoft designed Visual Studio .NET as a single integrated development environment for Visual Basic, Visual C#, and Visual C++ programmers. Visual Studio .NET offers several features and tools accessible from all .NET languages. For example, the Toolbox in Visual Basic 6.0 is now available when you use any .NET language. Another feature common to Visual Studio .NET is the Object Browser, a tool for exploring Visual Studio .NET projects, references, and external libraries. As you write a .NET program, use the Object Browser to browse or verify properties, method and function prototypes, parameters, and data types for the Measurement Studio controls. To view the Object Browser, select View»Other Windows»Object Browser.

For a complete list and description of Visual Studio .NET features, refer to Introducing Visual Studio .NET in the MSDN Library.

Note: Microsoft offers a rich set of documentation to help you understand and use the .NET Framework and technologies. The links in this document refer to topics in the MSDN Library, which is available online.

Working with Windows Forms

Visual Studio .NET offers a new platform for creating graphical user interfaces in any .NET language – Windows Forms. You interact with Windows Forms and design user interfaces much the same as you do in Visual Basic 6.0.

Visual Basic 6.0 forms are ActiveX control containers, designed specifically to host ActiveX controls or COM-based components. Visual Studio .NET Windows Forms are designed to host standard Windows Forms controls. Visual Studio .NET offers many standard controls for creating user interfaces. Refer to the Windows Forms and ActiveX Controls section in this document to learn how Windows Forms can host ActiveX controls.

For information about Windows Forms and the controls you can place on them, refer to the following topics in MSDN:



Windows Forms and ActiveX Controls

Because Windows Forms can host only Windows Forms controls, Visual Studio .NET automatically creates Windows Forms wrappers for ActiveX controls so you can continue to use them in Visual Studio .NET projects. For example, when you place a Measurement Studio ActiveX control on a Windows Form, Visual Studio .NET uses the COM type library to automatically create two DLLs for the entire OCX, which is a distributable package that contains one or several ActiveX controls. One DLL contains a common language runtime proxy for the OCX, and the second contains a Windows Form proxy for the OCX.

The common language runtime proxy DLL is known as the Runtime Callable Wrapper (RCW) and contains a .NET version of the COM type library. It defines all objects and interfaces found in the original COM type library and the classes that implement the interfaces. Visual Studio .NET names this DLL Interop.ID.dll, where ID is the name of the type library. The Interop prefix denotes that the RCW incorporates services from the Interop layer, which controls marshalling and interoperability between COM and the .NET Framework.

The Windows Form proxy DLL provides a .NET control that calls the RCW; that is, this DLL defines the properties, methods, and events you can set, call, and handle programmatically. Visual Studio .NET names this DLL AxInterop.ID.dll, where ID is the name of the type library. The Ax prefix denotes that the wrapper classes inherit from the AxHost class to expose the ActiveX control as a Windows Forms control.

You can view references to both of these files in the References node of the Solution Explorer, and you can explore both files in the Object Browser.

For information about the automatic conversion of ActiveX controls to .NET assemblies and generating ActiveX control wrappers, refer to Type Library to Assembly Conversion Summary and Windows Forms ActiveX Control Importer.

For information about the AxHost class and Interop layer, refer to AxHost class and COM Interoperability in Visual Basic and Visual C#.

Programming with the .NET Framework

Microsoft created the .NET Framework to simplify development of Internet technology and distributed programs. The .NET Framework is a platform that provides a standard class library and a Common Language Runtime (CLR) to developers in different .NET languages. This common platform ensures code reuse and reliability across Visual Basic .NET, Visual C# .NET, and Visual C++ .NET. For information about the .NET Framework and the CLR, refer to Overview of the .NET Framework and Common Language Runtime Overview.

When to Use Measurement Studio ActiveX Controls in .NET Applications

With Measurement Studio 7.0 or later, you can use native .NET class libraries for your .NET measurement and automation applications. If the library or functionality that you need does not yet exist in the Measurement Studio .NET class libraries, you can use the Measurement Studio ActiveX controls instead. Visit ni.com/mstudio for information about the .NET libraries that are available with Measurement Studio.

Back to Top

2. Loading the Measurement Studio ActiveX Controls


Unlike the Toolbox in Visual Basic 6.0, which retains customizations for a single project, the Visual Studio .NET Toolbox retains customizations for the entire development environment. When you load the Measurement Studio ActiveX controls in the Toolbox, as described below, they are available in any Windows Form designer, regardless of the language.

1. Open a Windows Form in a Visual Basic .NET or Visual C# .NET project.

2. Right click anywhere in the Toolbox and select Customize Toolbox. If the Toolbox is not visible, first select View»Toolbox.

3. From the COM Components list, select the ActiveX controls that you want in the project. You will find all Measurement Studio ActiveX controls, starting with CW, in the list. Unlike Visual Basic 6.0, Visual Studio .NET lists each COM component rather than OCX. For example, if you want to use a Measurement Studio graph in a Visual Basic 6.0 project, you load National Instruments CW UI, which includes the Measurement Studio Knob, Slide, Button, Numerical Edit, and graph controls. In Visual Studio .NET, select CWGraph Control to load the Measurement Studio graph control.

Note There is one exception to the Measurement Studio ActiveX control-naming scheme. The NI Motion control is named NIMotion Control in the COM Components list.

4. Click OK. Visual Studio .NET loads the selected controls on the currently selected tab of the Toolbox.

Tip You can create tabs to help you organize by functionality the Measurement Studio controls. For example, you might create tabs named Measurement Studio UI and Instrument Control and add the appropriate Measurement Studio controls to these tabs. You also can drag-and-drop components on a tab or between tabs to organize them in any way you like.

Back to Top

3. Browsing Properties


Place a Measurement Studio graph control on the Windows Form and explore its properties (View»Properties Window). You might notice that several properties have been added or changed, including the default name of the control. Visual Studio .NET prefixes all ActiveX controls with Ax (in Visual Basic .NET) or ax (in Visual C# .NET). For example, when you drop the first instance of a Measurement Studio graph control on a Visual Basic 6.0 form, Visual Basic 6.0 names that control CWGraph1. When you drop the first instance of a Measurement Studio graph control in Visual Studio .NET, Visual Basic .NET names the control AxCWGraph1.

Note If you add the CWButton control, you can find it in the Toolbox listed as CWBoolean. After you place the button on the form, Visual Basic .NET names the first instance AxCWButton1, and Visual C# .NET names it axCWButton1.

Back to Top

4. Browsing Generated Code


Open the code editor, and examine the code that Visual Studio .NET automatically generated. In both Visual Basic .NET and Visual C# .NET, the code generated contains a call to InitializeComponent() to initialize properties on the Windows Form and controls, a call to Dispose() for resource cleanup, and declaration of and reference to the instance of the graph you placed on the form.

Microsoft recommends that you do not directly modify the generated code. If you need to change properties for the Windows Form or controls that you placed on the Windows Form, use the Properties window.

Back to Top

5. Changes to the Visual Basic Language


Microsoft made many changes to its Visual Basic language to add object-oriented features and make it more like other .NET languages. This section describes programming changes you will encounter, specifically with respect to default properties and the Item method, as you develop applications with the Measurement Studio ActiveX controls in Visual Basic .NET or Visual C# .NET. Refer to the following topics for complete information about changes to the Visual Basic programming language:
If you are interested in learning more about the Visual C# .NET language, take the C# Language Tour or read about it via the link below.

Default Properties

Unlike Visual Basic 6.0, Visual Basic .NET does not support default properties, which are the most commonly used properties of the control. For example, you might use the following Visual Basic 6.0 syntax to set the value of a Measurement Studio Knob:

CWKnob1 = 10

In Visual Studio .NET, you must modify the code to explicitly name the property. In this example, explicitly name the Value property:

'Visual Basic .NET
AxCWKnob1.Value = 10

'Visual C# .NET
axCWKnob1.Value = 10;

Item Method

You can retrieve an item from a collection in Visual Basic 6.0 without explicitly stating the Item method, as demonstrated in the following example:

'Visual Basic 6.0: Retrieving an Item in a Collection
CWGraph1.Plots(1).Name = "Temperature"

In Visual Studio .NET, you must explicitly use the Item method when you retrieve items from a collection, as shown in the following examples:

'Visual Basic .NET: Retrieving an Item in a Collection
AxCWGraph1.Plots.Item(1).Name = "Temperature"

'Visual C# .NET: Retrieving an Item in a Collection
axCWGraph1.Plots.Item(1).Name = "Temperature";

Collection objects implement the IEnumerable interface, so you can iterate through items in a collection using For Each just as you can in Visual Basic 6.0. The following examples show how to iterate through a collection of CWPlot objects on a Measurement Studio graph control:

'Visual Basic .NET: Iterating through a Collection
Dim p As CWUIControlsLib.CWPlot

'Clear data from each plot
'on the graph

For Each p In AxCWGraph1.Plots
    p.ClearData()
Next p

'Visual C# .NET: Iterating through a Collection
//Clear data from each plot
// on the graph

foreach (CWUIControlsLib.CWPlot p in axCWGraph1.Plots)
    p.ClearData();

See Also:
Microsoft's Latest Programming Language: Visual C#

Back to Top

6. Variants versus Objects


In Visual Basic 6.0, Variants are the universal data type. That is, you can store any type of data in a Variant. Also, any variable not specifically declared as a type defaults to Variant. The Measurement Studio ActiveX controls frequently use Variant data types to simplify passing data between controls and among properties, functions, methods, and events.

In Visual Studio .NET, Microsoft created a common data type system for all .NET languages. Now all .NET languages have the same native data types, and you can easily pass data between programs designed in different languages. Each data type is actually a data class that encapsulates relevant data members, properties, and methods for that type. The Object class is a base class from which all other data types are derived. Thus, Objects are the universal data type in Visual Studio .NET, and Variants are not supported.

For information about this new common type system, refer to Common Type System.

When you use Measurement Studio ActiveX controls in Visual Studio .NET projects, all Variants are automatically converted to Objects. Like a variable of type Variant in Visual Basic 6.0, a variable of type Object in Visual Studio .NET can reference any actual type. When you program with the Measurement Studio controls in Visual Studio .NET, you must convert data to its actual type before passing it to the controls; that is, you must determine the actual data type that a Measurement Studio control property or method is expecting and pass it data of that type.

The rest of this section presents examples that demonstrate how to determine the actual type that a Measurement Studio control is expecting and convert variables of type Object to that actual type.

Comparing a Variable to a .NET Type

If you want to compare the actual type of a variable to a native .NET data type, use the TypeOf () Is expression in Visual Basic .NET and the is keyword in Visual C# .NET.

For example, if you want to display a temperature measurement as the value of a thermometer – a configurable style of the Measurement Studio Slide – first verify that the Measurement Studio Slide can accept the measurement as the type it is currently stored. The Measurement Studio Slide Value property requires a variable of type double. The following examples verify that the Slide can correctly display the temperature data:

'Visual Basic .NET
If TypeOf (temperature) Is Double Then
    AxCWSlide1.Value = temperature
End If

'Visual C# .NET
if (temperature is double)
    axCWSlide1.Value = temperature;

Retrieving the Type Name as a String

The System.Object class in the .NET Framework contains many properties and methods that you can use to work with variables of type Object. For example, use the GetType().Name property to get the .NET string representation of the actual type of an Object variable, as shown in the following examples:

'Visual Basic .NET
'determine the actual type
'of the data variable
Dim actualType As String
actualType = data.GetType().Name

'Visual C# .NET
//determine the actual type
//of the data variable
string actualType;
actualType = data.GetType().Name;

Converting Variables to an Actual Type

When you determine the actual type of an Object, you might want to convert the Object data and store it in a strongly typed variable. The System.Convert class in the .NET Framework contains several methods that you can use to convert variables of type Object to actual types. The following examples demonstrate how to covert the data stored in an Object variable to type double:

'Visual Basic .NET
Dim d As Double
'data is an Object variable
d = Convert.ToDouble(data)

'Visual C# .NET
double d;
//data is an Object variable
d = Convert.ToDouble(data);

Visual Basic .NET also offers the following casting expressions:

DirectCast(Expr, Type)

CType(Expr, Type)

DirectCast() casts a variable of type Object to any other type as a direct cast down the hierarchy, which provides better run-time performance; however, DirectCast() throws an exception if the specified type is not the same as the run-time type of the expression. For example, to cast an Object variable to a double, you might use the following syntax:

//data is of type Object
Dim d As Double
d = DirectCast(data, Double)

Refer to Cast Expressions for more information about which casting expression you should use.

In Visual C# .NET, use the cast operator to cast an Object variable to another type, as shown in the following example:

'data is of type Object
double d;
d = (double)data;

The cast operator throws an InvalidCastException if the cast fails.

You also can use the as operator to convert types. The is operator returns false if the conversion cannot be completed, and the as operator returns null.

Refer to () Operator and as Operator for more information about casting variables in Visual C# .NET.

Back to Top

7. Event Handling


Microsoft implemented a new event model in Visual Studio .NET. In this event model, an object generates an event in response to some action or change in state, such as a mouse click or x number of points being acquired. The object that generates the event is called the event sender. Another object, called the event receiver, handles the event and responds by executing an event handler, which processes the event. However, the event sender needs a pointer to the event receiver. The .NET Framework refers to this pointer as a delegate. A delegate is a class that holds a reference to a method.

When handling events that come from Measurement Studio ActiveX controls in .NET, it is important to remember that .NET exceptions will be swallowed by the RCW (Runtime Callable Wrapper) without your knowledge. To prevent this from happening, use a try catch block. In the catch block, report the exception using the following code:

'Visual Basic .NET
Try
 'code
Catch e As Exception
 MessageBox.Show(e.Message)
End Try

'Visual C# .NET

try
{
 //code
}
catch(Exception e)
{
  MessageBox.Show(e.Message);
}

To learn more about this new event model, refer to the following items in MSDN:


Consider a CursorChange event, which occurs when you drag a cursor on a Measurement Studio graph. Visual Basic .NET generates the following event handler skeleton for you. In this example, the Handles keyword indicates that the event handler (named AxCWGraph1_CursorChange) is called when the control names AxCWGraph1 generates a CursorChange event.

Private Sub AxCWGraph1_CursorChange(ByVal sender As Object, ByVal e As AxCWUIControlsLib._DCWGraphEvents_CursorChangeEvent) Handles AxCWGraph1.CursorChange

End Sub

The event sender is the object that raises the event – the Measurement Studio graph. The event receiver is the event handler.

To generate the same event handler in Visual C# .NET, select the graph on the Windows Form, and view the Events in the Properties window. Locate the CursorChange event and enter a name for event handler. For example, you might name the event handler OnCursorChange. Visual C# .NET then generates the following event handler for the CursorChange event:

private void OnCursorChange(object sender, AxCWUIControlsLib._DCWGraphEvents_CursorMouseMoveEvent e)
{
}

Visual C# .NET automatically adds the following event handler reference to the code:

this.axCWGraph1.CursorMouseMove += new AxCWUIControlsLib._DCWGraphEvents_CursorMouseMoveEventHandler(this.OnCursorChange);

Each Measurement Studio event returns two data items. The first item, named sender, is the object that raised the event. The second item, named e, contains all data returned from the event. To determine which data e contains, refer to the reference topic for the event in the Measurement Studio Reference. Remember that Variant data is now returned by the event as Object data.

In response to the user moving a cursor, the graph control passes the following data to the event handler:

  • Index of the cursor, which is valuable if the graph has more than one cursor
  • Coordinates of the cursor crosshair – XPos returns the X coordinate, and YPos returns the Y coordinate
  • Boolean value indicating if the mouse button was pressed as the cursor was moved

You can access the event data through e, as in the following examples:

'Visual Basic .NET CursorChange Event
Private Sub AxCWGraph1_CursorChange(ByVal sender As System.Object, ByVal e As AxCWUIControlsLib._DCWGraphEvents_CursorChangeEvent) Handles AxCWGraph1.CursorChange

        'XPosition and YPosition are
       'Measurement Studio NumEdit controls

        XPosition.Value = e.xPos
       YPosition.Value = e.yPos

End Sub

'Visual C# .NET CursorChange Event
private void OnCursorChange(object sender, AxCWUIControlsLib._DCWGraphEvents_CursorMouseMoveEvent e)
{
      //XPosition and YPosition are
      //Measurement Studio NumEdit controls

       XPosition.Value = e.xPos;
      YPosition.Value = e.yPos;
}

Back to Top

8. Getting Help with the Measurement Studio API


Each Measurement Studio ActiveX control is documented in the Measurement Studio Reference. You can launch the Measurement Studio Reference from the Windows Start menu (Programs»National Instruments»Measurement Studio»Help»Measurement Studio Reference) or from Visual Studio .NET – just right click a Measurement Studio control on a Windows Form and select ActiveX-Help. Remember that syntax examples are in Visual Basic 6.0 syntax and all Variants are Objects in Visual Studio .NET.

Back to Top

9. Distributing Visual Studio .NET Applications


Visual Studio .NET offers several built-in setup and deployment project templates and a Setup Wizard, so you can add setup and deployment directly to a Visual Studio .NET solution. The Setup Wizard includes all files necessary to distribute the application and is a good place to start if you are not sure which type of deployment project you need.

For information about Visual Studio .NET solutions, refer to Solution Explorer. For information about setup and deployment projects, refer to Deploying Applications and Components. For information about the Setup Wizard, refer to Setup Wizard.

Back to Top

10. Conclusion


National Instruments is committed to helping Visual Studio programmers develop measurement and automation applications while taking advantage of the latest technologies. If you are interested in learning more about Measurement Studio and support for Visual Studio .NET, visit ni.com/mstudio.
Related Links:
Learn more about NI and Visual Studio .NET
Microsoft's Latest Programming Language: Visual C#
Frequently Asked Questions about Microsoft .NET - Part 1 of 3
Frequently Asked Questions about Microsoft .NET - Part 2 of 3
Frequently Asked Questions about Microsoft .NET - Part 3 of 3

Back to Top

Bookmark & Share

Ratings

Rate this document

Answered Your Question?
Yes No

Submit