/// <summary>
 /// Get Properties
 /// </summary>
 /// <param name="session"></param>
 /// <param name="itemId"></param>
 /// <param name="node"></param>
 private void AddProperties(DaAddressSpaceElement addSpaceElem, DaGetPropertiesOptions propertyGetOptions, TreeNode node)
 {
     //get properties
     DaProperty[] properties = null;
     if (ResultCode.SUCCEEDED(addSpaceElem.GetDaProperties(new DaGetPropertiesOptions(),
                                                           out properties,
                                                           executionOptions)))
     {
         if (properties == null)
         {
             return;
         }
         for (int j = 0; j < properties.Length; j++)
         {
             if (properties[j].Id > 99)
             {
                 TreeNode proNode = new TreeNode(properties[j].Description, PROP_INDEX, PROP_INDEX);
                 proNode.Name = properties[j].Description;
                 proNode.Tag  = properties[j];
                 node.Nodes.Add(proNode);
             }
         }
     }
 }
        public OpcForm(OutProc anOutProc)
        {
            InitializeComponent();

            try
            {
                m_outProc = anOutProc;

                m_opcClient = m_outProc.OpcClient;

                m_session = m_opcClient.GetSession();

                m_executionOptions = m_opcClient.GetExecutionOptions();

                //create the browse tree root element
                TreeNode treeRoot = new TreeNode(m_session.Url, 0, 0);
                DaAddressSpaceElement rootDaAddressSpaceElement = new DaAddressSpaceElement(EnumAddressSpaceElementType.BRANCH, String.Empty, string.Empty, string.Empty, 0, null);
                rootDaAddressSpaceElement.Session = m_session;
                treeRoot.Tag = rootDaAddressSpaceElement;
                itemsTreeView.Nodes.Add(treeRoot);

                DaAddressSpaceElement[] addressSpaceElements = null;

                #region Browse branches
                //---------------------
                DaAddressSpaceElementBrowseOptions browseOptions = new DaAddressSpaceElementBrowseOptions();
                browseOptions.ElementTypeFilter = EnumAddressSpaceElementType.BRANCH;

                //get the root's children
                if (ResultCode.SUCCEEDED(m_session.Browse(
                                             ((DaAddressSpaceElement)treeRoot.Tag).ItemId,
                                             ((DaAddressSpaceElement)treeRoot.Tag).ItemPath,
                                             browseOptions,
                                             out addressSpaceElements,
                                             m_executionOptions)))
                {
                    if (addressSpaceElements != null)
                    {
                        for (int i = 0; i < addressSpaceElements.Length; i++)
                        {
                            TreeNode node = new TreeNode(addressSpaceElements[i].Name, 1, 1);
                            node.Tag = addressSpaceElements[i];
                            treeRoot.Nodes.Add(node);
                        }        //end for
                    }
                }                //end if

                //-
                #endregion

                addressSpaceElements = null;

                #region Browse Leaves
                //-------------------
                browseOptions.ElementTypeFilter = EnumAddressSpaceElementType.LEAF;

                //get the root's children
                if (ResultCode.SUCCEEDED(m_session.Browse(
                                             ((DaAddressSpaceElement)treeRoot.Tag).ItemId,
                                             ((DaAddressSpaceElement)treeRoot.Tag).ItemPath,
                                             browseOptions,
                                             out addressSpaceElements,
                                             m_executionOptions)))
                {
                    if (addressSpaceElements != null)
                    {
                        for (int i = 0; i < addressSpaceElements.Length; i++)
                        {
                            TreeNode node = new TreeNode(addressSpaceElements[i].Name, 2, 2);
                            node.Tag = addressSpaceElements[i];
                            treeRoot.Nodes.Add(node);
                        }        //end for
                    }            //  end if
                }                //end if
                                 //-
                #endregion

                #region GetDaProperties
                //---------------------

                DaProperty[] daProperties = null;
                m_propertyGetOptions.WhatPropertyData = EnumPropertyData.ALL;

                if (ResultCode.SUCCEEDED(rootDaAddressSpaceElement.GetDaProperties(
                                             m_propertyGetOptions,
                                             out daProperties,
                                             m_executionOptions)))
                {
                    if (daProperties != null)
                    {
                        for (int i = 0; i < daProperties.Length; i++)
                        {
                            TreeNode node = new TreeNode(daProperties[i].Name, 3, 3);
                            //add all properties except OPC-specific properties
                            if (daProperties[i].Id >= 100)
                            {
                                treeRoot.Nodes.Add(node);
                            }
                        }           //end for
                    }
                }                   //  end if

                //-
                #endregion
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }       //	end try...catch
        }           //	end constructor
        }           //	end Main

        private void TreeViewBeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
        {
            try
            {
                itemsTreeView.Cursor = System.Windows.Forms.Cursors.WaitCursor;

                foreach (TreeNode node in e.Node.Nodes)
                {
                    m_propertyGetOptions.WhatPropertyData = EnumPropertyData.ALL;
                    DaAddressSpaceElement[] addressSpaceElements = null;

                    node.Nodes.Clear();

                    if (node.Tag is DaAddressSpaceElement)
                    {
                        DaAddressSpaceElement element = node.Tag as DaAddressSpaceElement;
                        DaAddressSpaceElementBrowseOptions browseOptions = new DaAddressSpaceElementBrowseOptions();

                        #region Branches
                        //-----------
                        if (m_session.isConnected())
                        {
                            browseOptions.ElementTypeFilter = EnumAddressSpaceElementType.BRANCH;

                            if (ResultCode.SUCCEEDED(m_session.Browse(
                                                         element.ItemId,
                                                         element.ItemPath,
                                                         browseOptions,
                                                         out addressSpaceElements,
                                                         m_executionOptions)))
                            {
                                if (addressSpaceElements != null)
                                {
                                    for (int i = 0; i < addressSpaceElements.Length; i++)
                                    {
                                        TreeNode newNode = new TreeNode(addressSpaceElements[i].Name, 1, 1);
                                        newNode.Tag = addressSpaceElements[i];
                                        node.Nodes.Add(newNode);
                                    }            //end for
                                }
                            }                    //end if
                        }                        //end if
                                                 //-
                        #endregion

                        addressSpaceElements = null;

                        #region Leaves
                        //-----------
                        if (m_session.isConnected())
                        {
                            browseOptions.ElementTypeFilter = EnumAddressSpaceElementType.LEAF;

                            if (ResultCode.SUCCEEDED(m_session.Browse(
                                                         element.ItemId,
                                                         element.ItemPath,
                                                         browseOptions,
                                                         out addressSpaceElements,
                                                         m_executionOptions)))
                            {
                                if (addressSpaceElements != null)
                                {
                                    for (int i = 0; i < addressSpaceElements.Length; i++)
                                    {
                                        TreeNode newNode = new TreeNode(addressSpaceElements[i].Name, 2, 2);
                                        newNode.Tag = addressSpaceElements[i];
                                        node.Nodes.Add(newNode);
                                    }            //end for
                                }
                            }                    //end if
                        }                        //end if
                                                 //-
                        #endregion

                        DaProperty[] daProperties = null;

                        #region Properties
                        //----------------
                        if (m_session.isConnected())
                        {
                            if (ResultCode.SUCCEEDED(element.GetDaProperties(
                                                         m_propertyGetOptions,
                                                         out daProperties,
                                                         m_executionOptions)))
                            {
                                if (daProperties != null)
                                {
                                    for (int i = 0; i < daProperties.Length; i++)
                                    {
                                        //add all properties except OPC-specific properties
                                        if (daProperties[i].Id >= 100)
                                        {
                                            TreeNode newNode = new TreeNode(daProperties[i].Description, 3, 3);
                                            node.Nodes.Add(newNode);
                                        }
                                    }    //end for
                                }
                            }            //  end if
                        }                //end if
                                         //-
                        #endregion
                    }                    // end if

                    node.Collapse();
                }                   //  end if
                itemsTreeView.Cursor = System.Windows.Forms.Cursors.Default;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
        } //	end treeview_beforeexpand