Share via


ShapeCollection Class

Represents a collection of Shape objects.

Inheritance Hierarchy

System.Object
  Microsoft.VisualBasic.PowerPacks.ShapeCollection

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

Syntax

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

The ShapeCollection type exposes the following members.

Constructors

  Name Description
Public method ShapeCollection Initializes a new instance of the ShapeCollection class.

Top

Properties

  Name Description
Public property Count Gets the number of shapes in the collection.
Public property IsReadOnly Gets a value indicating whether a collection is read-only.
Public property Item Gets the Shape at the specified indexed location in the collection.
Public property Owner Gets the ShapeContainer that owns the ShapeCollection.

Top

Methods

  Name Description
Public method Add Adds the specified Shape to the ShapeCollection.
Public method AddRange Adds an array of Shape objects to the ShapeCollection.
Public method Clear Removes all shapes from the collection.
Public method Contains Determines whether the specified Shape is a member of the collection.
Public method ContainsKey Determines whether the ShapeCollection contains an item with the specified key.
Public method CopyTo Copies the whole ShapeCollection to a compatible one-dimensional Array, starting at the specified index of the destination array.
Public method Dispose Releases the unmanaged resources used by the ShapeCollection.
Public method Equals Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public method GetChildIndex(Shape) Retrieves the index of the specified Shape in the ShapeCollection.
Public method 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.
Public method GetEnumerator Retrieves a reference to an enumerator object that is used to iterate over a ShapeCollection.
Public method GetHashCode Serves as the default hash function. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method IndexOf Retrieves the index of the specified Shape in the ShapeCollection.
Public method IndexOfKey Retrieves the index of the first occurrence of the specified item in the collection.
Public method Remove Removes the specified Shape from the ShapeCollection.
Public method RemoveAt Removes a Shape from the ShapeCollection at the specified indexed location.
Public method SetChildIndex Sets the index of the specified Shape in the ShapeCollection to the specified index value.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate method IList.Add
Explicit interface implemetationPrivate method IList.Contains
Explicit interface implemetationPrivate method ICollection.CopyTo
Explicit interface implemetationPrivate method IList.IndexOf
Explicit interface implemetationPrivate method IList.Insert
Explicit interface implemetationPrivate property IList.IsFixedSize
Explicit interface implemetationPrivate property ICollection.IsSynchronized
Explicit interface implemetationPrivate method IList.Remove
Explicit interface implemetationPrivate property ICollection.SyncRoot

Top

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 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
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);
        }
    }
}

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

Reference

Microsoft.VisualBasic.PowerPacks Namespace

Other Resources

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)