Share via


ShapeCollection.GetChildIndex Method (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.

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

Syntax

public int GetChildIndex(
    Shape child,
    bool throwException
)
public:
int GetChildIndex(
    Shape^ child,
    bool throwException
)
member GetChildIndex : 
        child:Shape *
        throwException:bool -> int
Public Function GetChildIndex (
    child As Shape,
    throwException As Boolean
) As Integer

Parameters

Return Value

Type: System.Int32

A zero-based index value that represents the location of the specified Shape in the ShapeCollection. Or -1 if the specified Shape is not found in the ShapeCollection.

Exceptions

Exception Condition
ArgumentException

The child shape is not in the ShapeCollection and the throwException parameter value is true.

Remarks

The index represents the order in which the shapes were added to the collection. If shapes are removed from the collection, the indexes of the shapes are reassigned.

A return value of -1 is returned only when the throwException parameter is false.

Examples

The following code example demonstrates how to use the GetChildIndex method to retrieve the location of a Shape in a ShapeCollection. This example requires that you have at least two OvalShape controls on a form.

private void ovalShape1_Click(System.Object sender, System.EventArgs e)
{
    int i;
    // Find the index for OvalShape1.
    i = ovalShape1.Parent.Shapes.GetChildIndex(ovalShape2, false);
    // If the shape is not in the collection, display a message.
    if (i==-1)
    {
        MessageBox.Show("ovalShape2 is not in this collection.");
    }
    else
    {
        String index;
        index = i.ToString();
        MessageBox.Show("The index for ovalShape2 is " + index);
    }
}
Private Sub OvalShape1_Click() Handles OvalShape1.Click
    Dim i As Integer
    ' Find the index for OvalShape1.
    i = OvalShape1.Parent.Shapes.GetChildIndex(OvalShape2, False)
    ' If the shape is not in the collection, display a message.
    If i = -1 Then
        MsgBox("OvalShape2 is not in this collection.")
    Else
        MsgBox("The index for OvalShape2 is " & CStr(i))
    End If
End Sub

See Also

IndexOf
GetChildIndex Overload
ShapeCollection Class
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