Class ViewCollection

ViewCollection class

Contains a list of View objects. Implements ICollection<View> interface.

public class ViewCollection : ICollection<View>

Properties

NameDescription
Count { get; }Gets the number of elements contained in this collection.
IsReadOnly { get; }Gets a value indicating whether this collection is read-only; otherwise, false.
ParentProject { get; }Gets the parent of the View object. Read-only Project.

Methods

NameDescription
Add(View)Adds the specified item to this collection.
Clear()Removes all items from this collection.
Contains(View)Returns true if the specified item is found in this collection; otherwise, false.
CopyTo(View[], int)Copies the elements of this collection to the specified array, starting at the specified array index.
GetByName(string)Searches for a View with the name, and returns the first occurrence within the collection.
GetByViewScreen(ViewScreen)Searches for a View with the specified Screen property, and returns the first occurrence within the collection.
GetEnumerator()Returns an enumerator for this collection.
Remove(View)Removes the first occurrence of a specific object from this collection.
ToList()Converts a view collection to a list of View objects.

Examples

Shows how to work with view collections.

var project = new Project(DataDir + "Project1.mpp");

// convert to a plain list of views
List<View> list = project.Views.ToList();
for (var index = 0; index < list.Count; index++)
{
    var viewToChange = list[index];
    viewToChange.PageInfo.Header.CenteredText = "Header " + index;
}

// add a new view
var view = new GanttChartView();
if (!project.Views.IsReadOnly)
{
    project.Views.Add(view);
}

// iterate over views
Console.WriteLine("Iterate over views of " + project.Views.ParentProject.Get(Prj.Name) + " project.");
Console.WriteLine("Project view count: " + project.Views.Count);
Console.WriteLine();
foreach (var projectView in project.Views)
{
    Console.WriteLine("Name: " + projectView.Name);
}

// remove all views at once
project.Views.Clear();

// or one by one
{
    // approach 1
    List<View> listToDelete = project.Views.ToList();
    foreach (var v in listToDelete)
    {
        if (project.Views.Contains(v))
        {
            project.Views.Remove(v);
        }
    }
}

{
    // approach 2
    var array = new View[project.Views.Count];
    project.Views.CopyTo(array, 0);
    foreach (var v in array)
    {
        if (project.Views.Contains(v))
        {
            project.Views.Remove(v);
        }
    }
}

See Also