1. OSIsoft.SDK.Controls.MDBTreeNet Tutorial

In this example we illustrate the use of the MDBTreeNet control by adding it to a Visual Studio.Net project. The example demonstrates how to connect to different servers and the utilization of some of the events and properties. The code also demonstrates the drag and drop features, error handling capabilities and how to use ActiveX Interops in a .NET project.

  1. STEP 1. Set up the project

Start Visual Studio and select a new "Visual C# - Windows Application" project. Follow the instructions described earlier in this chapter on how to add the necessary references and the controls to the project.

The only reference we need is for the PI-ServerPickList Interop assembly (OSIsoft.AXPISDKCTL.dll).

  1. STEP 2. Insert controls

From the toolbox, select and place the following controls onto the form

The Panel is a container for the ServerPickList control; it helps us to position and size the PickList on the form.

See the screen capture later in this chapter for placement and sizing details.

  1. STEP 3. Set control properties

Select all three textboxes and from the Properties pane, locate the "Multiline" property and change its value to True (note: you may not be able to resize the control before you do this). Select textBox1 and change the "Allow Drop" property to True.

  1. STEP 4. Add/Select event handlers

Switch to design view, right-click on textBox1 and select Properties item. Select the Events-tab and double-click on the following events: DragDrop and DragEnter.

Repeat this step for the mdbTreeNet1 events: nodeSelectedEvent and errorEvent.

  1. STEP 5. Add initialization code

Add a namespace usage definition in the "using" section:

 

using OSIsoft.SDK.Controls;

 

Add the following private class variable:

 

// Server Picklist control

private AxPISDKCtl.AxPISrvPickList serverPickList;

Paste the following code into the form constructor method right after the "InitalizeComponent();" statement.

// This code was added for the server picklist control

serverPickList = new AxPISDKCtl.AxPISrvPickList(); // create the control

// We'll use the panel and dock the picklist (fill) so re-sizing is easier

((System.ComponentModel.ISupportInitialize)(this.serverPickList)).BeginInit();

this.panel1.SuspendLayout();

// the following 3 lines could be commented out, they don't seem to affect this?

this.serverPickList.Enabled = true;

this.serverPickList.Name = "ServerPickList";

this.serverPickList.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("serverPickList.OcxState")));

serverPickList.Dock = DockStyle.Fill; // Picklist will fill the panel

// subscribe to click event

serverPickList.ClickEvent += new EventHandler(serverPickList_clickEvent);

this.panel1.Controls.Add(serverPickList); // add picklist to panel

((System.ComponentModel.ISupportInitialize)(this.serverPickList)).EndInit();

this.panel1.ResumeLayout();

 

  1. STEP 6. Add event handler code

Replace the event handler stubs that were created by the Designer with the following:

//---------------------------------------------------------

// SERVER_PICKLIST-CLICK EVENT:

/// <summary>

/// ServerPickList was clicked, i.e. the server is propably changed.

/// Go and get the new server and apply to the tree control.

/// </summary>

private void serverPickList_clickEvent(object sender, System.EventArgs e)

{

PISDK.Server server = serverPickList.SelectedServer; // get server

mdbTreeNet1.serverList = new string[] { server.Name }; // and apply.

}

//---------------------------------------------------------

/// <summary>

/// This method is invoked whenever a user selects a tree node.

/// This example demonstrates how to utilize the various properties

/// of the IMDBNode object that the application receives as one

/// of the event arguments.

/// </summary>

/// <param name="sender">Event source</param>

/// <param name="e">Event arguments class for this event</param>

private void mdbTreeNet1_nodeSelectedEvent(object sender, OSIsoft.SDK.Controls.NodeSelectedEventArgs e)

{

//get the node interface object

IMDBNode treeNode = e.TreeNode;

// show name

textBox1.Text = "You selected: " + treeNode.ItemName;

// show full pathname

textBox2.Text = "Full pathname = " + treeNode.FullPathName;

// show persistence string

textBox3.Text = "Persistence string = " + treeNode.PersistenceString;

}

//---------------------------------------------------------

// OLEDragDrop - ENTER EVENT

/// <summary>

/// This method is called when drag/drop operation enters the textBox1.

/// We will change drag image to match "COPY" if the desired formats are

/// present in the passed DataObject.

/// </summary>

/// <param name="sender">Event source</param>

/// <param name="e">Drag event arguments</param>

private void textBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)

{

if (e.Data.GetDataPresent(DragDropFormats.PATHNAMES))

e.Effect = DragDropEffects.Copy;

else

e.Effect = DragDropEffects.None;

}

//---------------------------------------------------------

// OLEDragDrop-EVENT

/// <summary>

/// This method is called when the user drops data from MDBTreeNet

/// on the textbox.

/// We will use the DataObject that is passed with event arguments to

/// get the data and show it in the text boxes.

/// </summary>

/// <param name="sender">Event source object</param>

/// <param name="e">Event arguments with DataObject </param>

private void textBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)

{

// Show name

textBox1.Text = (string)e.Data.GetData("System.String", false).ToString();

// Show full pathname

if (e.Data.GetDataPresent(DragDropFormats.PATHNAMES))

textBox2.Text = e.Data.GetData(DragDropFormats.PATHNAMES).ToString();

// Show persistence string

if (e.Data.GetDataPresent(DragDropFormats.PERSISTENCE))

textBox3.Text = e.Data.GetData(DragDropFormats.PERSISTENCE).ToString();

}

//---------------------------------------------------------

// ERROR_EVENT: show possible errors in the text box.

/// <summary>

/// This is the error handler. The handler gets invoked whenever the

/// MDBTree detects an error.

/// </summary>

/// <param name="sender">Error event source</param>

/// <param name="e">Error event arguments</param>

private void mdbTreeNet1_errorEvent(object sender, OSIsoft.SDK.Controls.ErrorEventArgs e)

{ // show error message with current date/time

MessageBox.Show(this, DateTime.Now.ToString() + e.message);

}

 

  1. STEP 7. Test the control

Save the project and run it. The tree-view should come up and show a single root node for the default server. Expand the tree down to "module" level (expand server and modules nodes at least) and select (click) a module. The three text boxes should show the name, pathname and persistence string of the selected module. Experiment with this by expanding and clicking different nodes.

  1. STEP 8. Other features

The form includes a server picklist-control and you can use it to connect to different servers. Experiment with this by selecting a server from the drop-down list and notice that the tree-view collapses and has a single node for the selected server. Please see the PI-ServerPicklist control for details about the picklist control.

The example code also allows you to drag a tree node onto the textBox1. Try this with different module nodes and see that the module name, full pathname and persistence strings show up in the text boxes. The pathnames and persistence strings are empty for certain node type like Modules, Aliases and other collections.

This is how the completed form should appear:

 

Enabling Operational Intelligence