Infragistics(R) NetAdvantage(R) Windows Forms
InitializeLocations Event
See Also  Example E-mail your feedback on this topic.
Infragistics.Win.Misc Namespace > UltraNavigationBar Class : InitializeLocations Event

Occurs when a UltraNavigationBarLocation.Locations collection is accessed for the first time.

Syntax

Visual Basic (Declaration) 
Public Event InitializeLocations As InitializeLocationsHandler
C# 
public event InitializeLocationsHandler InitializeLocations

Event Data

The event handler receives an argument of type InitializeLocationsEventArgs containing data related to this event. The following InitializeLocationsEventArgs properties provide information specific to this event.

PropertyDescription
ParentLocation Returns the UltraNavigationBarLocation instance whose UltraNavigationBarLocation.Locations collection is being initialized.

Example

The following code sample demonstrates how to populate an UltraNavigationBar control and an UltraTree control with the contents of the local machine's file system:

For an overview of how to handle events in Visual Basic or Visual C#, see Event Handlers in Visual Basic and Visual C#. For specific information and code examples illustrating how to consume events in your application, see Consuming Events in the .NET Framework Developer's Guide.

Visual BasicCopy Code
Imports System
Imports System.Drawing
Imports System.IO
Imports System.Collections.Generic
Imports System.ComponentModel
Imports Infragistics.Shared
Imports Infragistics.Win
Imports Infragistics.Win.Misc
Imports Infragistics.Win.Misc.UltraWinNavigationBar
Imports Infragistics.Win.UltraWinTree

	Public Class FileSystemSupport
    	Private Const PATH_SEPARATOR As String = "\"
    	Private navigationBar As UltraNavigationBar = Nothing
    	Private tree As UltraTree = Nothing

    Private Sub Populate(ByVal directory As DirectoryInfo, ByVal location As UltraNavigationBarLocation)

        Dim childDirectories As DirectoryInfo()

        Try
            childDirectories = directory.GetDirectories()
        Catch
            Return
        End Try


        Try

            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor

            If (childDirectories Is Nothing Or childDirectories.Length = 0) Then Return

            '  Get a reference to the collection
            Dim locations As NavigationBarLocationsCollection = location.Locations

            Dim wrapper As DirectoryWrapper = Nothing
            If Not location.Tag Is Nothing Then wrapper = location.Tag

            Dim parentNode As UltraTreeNode = wrapper.AssociatedObject
            Dim nodes As TreeNodesCollection = parentNode.Nodes

            '  Clear each collections if it is not empty
            If (locations.Count > 0) Then locations.Clear()

            If (nodes.Count > 0) Then nodes.Clear()

            Dim i As Integer
            For i = 0 To childDirectories.Length - 1

                '  Get the DirectoryInfo object for this child directory
                Dim childDirectory As DirectoryInfo = childDirectories(i)

                '  If the directory is hidden, don't add a location for it.
                If ((childDirectory.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden) Then Continue For

                '  Create an UltraNavigationBarLocation instance for the child directory
                '  also create the associated UltraTreeNode so we can attach it to the location.
                Dim childLocation As UltraNavigationBarLocation = New UltraNavigationBarLocation(childDirectory.FullName, childDirectory.Name)
                Dim childNode As UltraTreeNode = New UltraTreeNode(childLocation.Key, childLocation.Text)

                '  Assign an instance of a wrapper class which associates the node
                '  and directory to this location to its Tag property.
                childLocation.Tag = New DirectoryWrapper(childDirectory, childNode)

                '  Assign an instance of a wrapper class which associates the location
                '  and directory to this node to its Tag property.
                childNode.Tag = New DirectoryWrapper(childDirectory, childLocation)

                '  Add the UltraNavigationBarLocation which represents this directory
                '  to the Locations collection of the specified location.
                locations.Add(childLocation)

                '  Add the child node to the parent's Nodes collection
                nodes.Add(childNode)
            Next

        Finally

            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
        End Try


    End Sub

    ' Handles the UltraNavigationBar's 'InitializeLocations' event.
    Private Sub OnNavigationBarInitializeLocations(ByVal sender As Object, ByVal e As InitializeLocationsEventArgs)

        '  Populate the collection with the contents of the parent directory
        Dim parentLocation As UltraNavigationBarLocation = e.ParentLocation
        Dim wrapper As DirectoryWrapper = Nothing
        If Not parentLocation.Tag Is Nothing Then wrapper = parentLocation.Tag

        If (wrapper Is Nothing) Then Return

        Dim directoryInfo As DirectoryInfo = wrapper.DirectoryInfo

        Me.Populate(directoryInfo, parentLocation)

    End Sub

    ' Handles the UltraTree's 'BeforeExpand' event.
    Private Sub OnTreeBeforeExpand(ByVal sender As Object, ByVal e As CancelableNodeEventArgs)

        '  Acccess the associated UltraNavigationBarLocation's Locations collection;
        '  note that this will trigger the population of the Locations collection
        '  if necessary, which will in turn populate this node's Nodes collection.
        Dim parentNode As UltraTreeNode = e.TreeNode
        Dim wrapper As DirectoryWrapper = Nothing
        If Not parentNode.Tag Is Nothing Then wrapper = parentNode.Tag

        Dim location As UltraNavigationBarLocation = Nothing
        If Not wrapper Is Nothing Then location = wrapper.AssociatedObject Else location = Me.navigationBar.RootLocation
        Dim count As Integer = location.Locations.Count

    End Sub

    ' Handles the UltraNavigationBar's 'SelectedLocationChanged' event.
    Private Sub OnNavigationBarSelectedLocationChanged(ByVal sender As Object, ByVal e As SelectedLocationChangedEventArgs)

        '  Synchronize the UltraTree with the UltraNavigationBar by activating and selecting
        '  the node associated with the new SelectedLocation.
        Dim wrapper As DirectoryWrapper = Nothing
        If Not e.SelectedLocation.Tag Is Nothing Then wrapper = e.SelectedLocation.Tag
        Dim node As UltraTreeNode = Nothing
        If Not wrapper Is Nothing Then node = wrapper.AssociatedObject Else node = Me.tree.Nodes(0)
        Me.tree.ActiveNode = node
        node.Selected = True

    End Sub

    ' Handles the UltraTree's 'AfterActivate' event.
    Private Sub OnTreeAfterActivate(ByVal sender As Object, ByVal e As NodeEventArgs)

        '  Synchronize the UltraNavigationBar with the UltraTree by selecting
        '  the location associated with the new ActiveNode.
        Dim activeNode As UltraTreeNode = e.TreeNode
        Dim wrapper As DirectoryWrapper = Nothing
        wrapper = IIf(Not activeNode Is Nothing, activeNode.Tag, Nothing)
        Dim location As UltraNavigationBarLocation = Nothing
        If Not wrapper Is Nothing Then location = wrapper.AssociatedObject Else location = Me.navigationBar.RootLocation
        If (Not location Is Nothing) Then Me.navigationBar.SelectedLocation = location
    End Sub

	End Class
C#Copy Code
using System;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using Infragistics.Shared;
using Infragistics.Win;
using Infragistics.Win.Misc;
using Infragistics.Win.Misc.UltraWinNavigationBar;
using Infragistics.Win.UltraWinTree;

    public class FileSystemSupport
    {
        private const string PATH_SEPARATOR = "\\";
        private UltraNavigationBar navigationBar = null;
        private UltraTree tree = null;

        private void Populate( DirectoryInfo directory, UltraNavigationBarLocation location )
        {
            DirectoryInfo[] childDirectories = null;
            
            try { childDirectories = directory.GetDirectories(); }
            catch { return; }

            try
            {
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

                if ( childDirectories != null && childDirectories.Length > 0 )
                {
                    //  Get a reference to the collection
                    NavigationBarLocationsCollection locations = location.Locations;

                    DirectoryWrapper wrapper = location.Tag as DirectoryWrapper;
                    UltraTreeNode parentNode = wrapper.AssociatedObject as UltraTreeNode;
                    TreeNodesCollection nodes = parentNode.Nodes;

                    //  Clear each collections if it is not empty
                    if ( locations.Count > 0 )
                        locations.Clear();

                    if ( nodes.Count > 0 )
                        nodes.Clear();

                    for ( int i = 0; i < childDirectories.Length; i ++ )
                    {
                        //  Get the DirectoryInfo object for this child directory
                        DirectoryInfo childDirectory = childDirectories[i];

                        //  If the directory is hidden, don't add a location for it.
                        if ( (childDirectory.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden )
                            continue;

                        //  Create an UltraNavigationBarLocation instance for the child directory;
                        //  also create the associated UltraTreeNode so we can attach it to the location.
                        UltraNavigationBarLocation childLocation = new UltraNavigationBarLocation( childDirectory.FullName, childDirectory.Name );
                        UltraTreeNode childNode = new UltraTreeNode( childLocation.Key, childLocation.Text );

                        //  Assign an instance of a wrapper class which associates the node
                        //  and directory to this location to its Tag property.
                        childLocation.Tag = new DirectoryWrapper( childDirectory, childNode );

                        //  Assign an instance of a wrapper class which associates the location
                        //  and directory to this node to its Tag property.
                        childNode.Tag = new DirectoryWrapper( childDirectory, childLocation );

                        //  Add the UltraNavigationBarLocation which represents this directory
                        //  to the Locations collection of the specified location.
                        locations.Add( childLocation );

                        //  Add the child node to the parent's Nodes collection
                        nodes.Add( childNode );
                    }
                }
            }
            finally
            {
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
            }
        }

        // Handles the UltraNavigationBar's 'InitializeLocations' event.
        private void OnNavigationBarInitializeLocations(object sender, InitializeLocationsEventArgs e)
        {
            //  Populate the collection with the contents of the parent directory
            UltraNavigationBarLocation parentLocation = e.ParentLocation;
            DirectoryWrapper wrapper = parentLocation.Tag as DirectoryWrapper;
            if ( wrapper == null )
                return;

            DirectoryInfo directoryInfo = wrapper.DirectoryInfo;

            this.Populate( directoryInfo, parentLocation );
        }

        /// Handles the UltraTree's 'BeforeExpand' event.
        private void OnTreeBeforeExpand(object sender, CancelableNodeEventArgs e)
        {
            //  Acccess the associated UltraNavigationBarLocation's Locations collection;
            //  note that this will trigger the population of the Locations collection
            //  if necessary, which will in turn populate this node's Nodes collection.
            UltraTreeNode parentNode = e.TreeNode;
            DirectoryWrapper wrapper = parentNode.Tag as DirectoryWrapper;
            UltraNavigationBarLocation location = wrapper != null ? wrapper.AssociatedObject as UltraNavigationBarLocation : this.navigationBar.RootLocation;
            int count = location.Locations.Count;
        }

        /// Handles the UltraNavigationBar's 'SelectedLocationChanged' event.
        private void OnNavigationBarSelectedLocationChanged(object sender, SelectedLocationChangedEventArgs e)
        {
            //  Synchronize the UltraTree with the UltraNavigationBar by activating and selecting
            //  the node associated with the new SelectedLocation.
            DirectoryWrapper wrapper = e.SelectedLocation.Tag as DirectoryWrapper;
            UltraTreeNode node = wrapper != null ? wrapper.AssociatedObject as UltraTreeNode : this.tree.Nodes[0];
            this.tree.ActiveNode = node;
            node.Selected = true;
        }

        /// Handles the UltraTree's 'AfterActivate' event.
        private void OnTreeAfterActivate(object sender, NodeEventArgs e)
        {
            //  Synchronize the UltraNavigationBar with the UltraTree by selecting
            //  the location associated with the new ActiveNode.
            UltraTreeNode activeNode = e.TreeNode;
            DirectoryWrapper wrapper = activeNode != null ? activeNode.Tag as DirectoryWrapper : null;
            UltraNavigationBarLocation location = wrapper != null ? wrapper.AssociatedObject as UltraNavigationBarLocation : null;
            if ( location != null )
                this.navigationBar.SelectedLocation = location;
        }
    }

Remarks

The UltraNavigationBar supports "lazy loading" of its UltraNavigationBarLocation.Locations collections via the InitializeLocations event. When the collection's NavigationBarLocationsCollection.Count property, enumerator, or indexer is accessed for the first time, the event fires, giving listeners the opportunity to populate the collection in a manner that is transparent to the end user. This approach is useful in the case where populating all Locations collections would cause performance degradation; each individual collection is populated in an "on demand" fashion, deferring the usage of CPU time and other resources until the collection is actually requested. The collection can be reinitialized by calling its NavigationBarLocationsCollection.Reset method, specifying true as the value of the 'reInitialize' parameter.

See Also