ShapeCollection Class

 

Represents a collection of Shape objects.

Namespace:   Microsoft.VisualBasic.PowerPacks
Assembly:  Microsoft.VisualBasic.PowerPacks.Vs (in Microsoft.VisualBasic.PowerPacks.Vs.dll)

Inheritance Hierarchy

System.Object
  Microsoft.VisualBasic.PowerPacks.ShapeCollection

Syntax

public sealed class ShapeCollection : IList, IDisposable
public ref class ShapeCollection sealed : IList, IDisposable
[<Sealed>]
type ShapeCollection = 
    class
        interface IList
        interface IDisposable
    end
Public NotInheritable Class ShapeCollection
    Implements IList, IDisposable

Constructors

Name Description
System_CAPS_pubmethod ShapeCollection(ShapeContainer)

Initializes a new instance of the ShapeCollection class.

Properties

Name Description
System_CAPS_pubproperty Count

Gets the number of shapes in the collection.

System_CAPS_pubproperty IsReadOnly

Gets a value indicating whether a collection is read-only.

System_CAPS_pubproperty Item[Int32]

Gets the Shape at the specified indexed location in the collection.

System_CAPS_pubproperty Owner

Gets the ShapeContainer that owns the ShapeCollection.

Methods

Name Description
System_CAPS_pubmethod Add(Shape)

Adds the specified Shape to the ShapeCollection.

System_CAPS_pubmethod AddRange(Shape[])

Adds an array of Shape objects to the ShapeCollection.

System_CAPS_pubmethod Clear()

Removes all shapes from the collection.

System_CAPS_pubmethod Contains(Shape)

Determines whether the specified Shape is a member of the collection.

System_CAPS_pubmethod ContainsKey(String)

Determines whether the ShapeCollection contains an item with the specified key.

System_CAPS_pubmethod CopyTo(Shape[], Int32)

Copies the whole ShapeCollection to a compatible one-dimensional Array, starting at the specified index of the destination array.

System_CAPS_pubmethod Dispose()

Releases the unmanaged resources used by the ShapeCollection.

System_CAPS_pubmethod Equals(Object)

(Inherited from Object.)

System_CAPS_pubmethod GetChildIndex(Shape)

Retrieves the index of the specified Shape in the ShapeCollection.

System_CAPS_pubmethod GetChildIndex(Shape, Boolean)

Retrieves the index of the specified Shape in the ShapeCollection, and optionally raises an exception if the specified Shape is not in the ShapeCollection.

System_CAPS_pubmethod GetEnumerator()

Retrieves a reference to an enumerator object that is used to iterate over a ShapeCollection.

System_CAPS_pubmethod GetHashCode()

(Inherited from Object.)

System_CAPS_pubmethod GetType()

(Inherited from Object.)

System_CAPS_pubmethod IndexOf(Shape)

Retrieves the index of the specified Shape in the ShapeCollection.

System_CAPS_pubmethod IndexOfKey(String)

Retrieves the index of the first occurrence of the specified item in the collection.

System_CAPS_pubmethod Remove(Shape)

Removes the specified Shape from the ShapeCollection.

System_CAPS_pubmethod RemoveAt(Int32)

Removes a Shape from the ShapeCollection at the specified indexed location.

System_CAPS_pubmethod SetChildIndex(Shape, Int32)

Sets the index of the specified Shape in the ShapeCollection to the specified index value.

System_CAPS_pubmethod ToString()

(Inherited from Object.)

Explicit Interface Implementations

Name Description
System_CAPS_pubinterfaceSystem_CAPS_privmethod ICollection.CopyTo(Array, Int32)

Copies the ShapeCollection object to a compatible one-dimensional Array, starting at the specified index of the destination array.

System_CAPS_pubinterfaceSystem_CAPS_privmethod IList.Add(Object)

Adds the specified object to the collection.

System_CAPS_pubinterfaceSystem_CAPS_privmethod IList.Contains(Object)

Determines whether the specified object is in the collection.

System_CAPS_pubinterfaceSystem_CAPS_privmethod IList.IndexOf(Object)

Retrieves the index of the specified object in the collection.

System_CAPS_pubinterfaceSystem_CAPS_privmethod IList.Insert(Int32, Object)

Inserts the object at the specified index.

System_CAPS_pubinterfaceSystem_CAPS_privmethod IList.Remove(Object)

Removes the specified object from the collection.

Remarks

The Add, Remove, and RemoveAt methods enable you to add and remove individual shapes from the collection. You can also use the AddRange or Clear method to add or remove all the shapes from the collection.

You can determine whether a Shape is a member of the collection by passing the shape into the Contains method. To get the index value of the location of a shape in the collection, pass the shape into the IndexOf method. You can copy the collection into an array by calling the CopyTo method.

Examples

The following code example removes a Shape from the ShapeCollection of a form if it is a member of the collection. The example requires that you have a LineShape, an OvalShape, and a RectangleShape control on a form. When a shape is clicked, it is removed from the ShapeCollection unless it is the last shape in the collection.

private void Shapes_Click(System.Object sender, System.EventArgs e)
{
    // Determine whether the shape is in the collection.
    if (shapeContainer1.Shapes.Contains((Shape)sender))
    // If the Index is greater than 0, remove the shape.
    {
        if (shapeContainer1.Shapes.IndexOf((Shape)sender) > 0)
        {
            shapeContainer1.Shapes.Remove((Shape)sender);
        }
    }
}
Private Sub Shapes_Click(
    ByVal sender As System.Object, 
    ByVal e As System.EventArgs
  ) Handles RectangleShape1.Click, 
            OvalShape1.Click, LineShape1.Click

    ' Determine whether the shape is in the collection.
    If ShapeContainer1.Shapes.Contains(sender) Then
        ' If the Index is greater than 0, remove the shape.
        If ShapeContainer1.Shapes.IndexOf(sender) > 0 Then
            ShapeContainer1.Shapes.Remove(sender)
        End If
    End If
End Sub

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Microsoft.VisualBasic.PowerPacks Namespace
Introduction to the Line and Shape Controls (Visual Studio)
How to: Draw Lines with the LineShape Control (Visual Studio)
How to: Draw Shapes with the OvalShape and RectangleShape Controls (Visual Studio)

Return to top