共用方式為


ShapeCollection.GetChildIndex 方法 (Shape, Boolean)

擷取指定的ShapeShapeCollection 中的索引,並選擇性地引發例外狀況 (如果指定的Shape不在ShapeCollection)。

命名空間:  Microsoft.VisualBasic.PowerPacks
組件:  Microsoft.VisualBasic.PowerPacks.Vs (在 Microsoft.VisualBasic.PowerPacks.Vs.dll 中)

語法

'宣告
Public Function GetChildIndex ( _
    child As Shape, _
    throwException As Boolean _
) As Integer
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 : Shape, 
    throwException : boolean
) : int

參數

  • throwException
    類型:Boolean

    true 表示如果 child 參數中指定的 Shape 不是 ShapeCollection 中的控制項,則擲回例外狀況,否則為 false。

傳回值

類型:Int32
以零起始的索引值,表示指定的 ShapeShapeCollection 中的位置。如果在 ShapeCollection 中找不到指定的 Shape,則為 -1。

例外狀況

例外狀況 條件
ArgumentException

child 圖案不在ShapeCollection 中,而且 throwException 參數值是 true。

備註

索引表示圖案加入至集合的順序。 如果圖案從集合中移除,重新指派圖案的索引。

只有當 throwException 參數是 false 時,才會傳回 -1 的值。

範例

下列程式碼範例示範如何使用 GetChildIndex 方法擷取 Shape 位置的 ShapeCollection。 這個範例要求您的表單上必須有至少兩個 OvalShape 控制項。

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

.NET Framework 安全性

請參閱

參考

ShapeCollection 類別

GetChildIndex 多載

Microsoft.VisualBasic.PowerPacks 命名空間

IndexOf

其他資源

Line 和 Shape 控制項簡介 (Visual Studio)

如何:使用 LineShape 控制項繪製線條 (Visual Studio)

如何:使用 OvalShape 和 RectangleShape 控制項繪製圖案 (Visual Studio)