共用方式為


ShapeContainer Constructor

Initializes a new instance of the ShapeContainer class.

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

Syntax

'宣告
Public Sub New
public ShapeContainer()
public:
ShapeContainer()
new : unit -> ShapeContainer
public function ShapeContainer()

Remarks

When you create a line or shape at run time by using the New method, its Parent property must be set to a ShapeContainer. If a ShapeContainer already exists for the form or container, you should set the Parent property to that ShapeContainer. If no ShapeContainer exists, you can create a ShapeContainer by using the New method and set its Parent property to the form or container.

注意

Be careful that you do not create more than one ShapeContainer for each form or container; doing this may introduce unexpected behavior. If you add a design-time line or shape control to a form or container after you write code to create one programmatically, you should modify that code to use the ShapeContainer created by the designer.

Examples

The following example checks for an existing ShapeContainer and sets the Parent property of an OvalShape control created at run time by using the New method.

Private Sub Form1_Load() Handles MyBase.Load
    Dim NewOval As New OvalShape
    Dim i As Integer
    Dim found As Boolean
    ' Loop through the Controls collection.
    For i = 0 To Me.Controls.Count - 1
        ' If a ShapeContainer is found, make it the parent.
        If TypeOf Controls.Item(i) Is ShapeContainer Then
            NewOval.Parent = Controls.Item(i)
            found = True
            Exit For
        End If
    Next
    ' If no ShapeContainer is found, create one and set the parents.
    If found = False Then
        Dim sc As New ShapeContainer
        sc.Parent = Me
        NewOval.Parent = sc
    End If
    NewOval.Size = New Size(200, 300)
End Sub
private void form1_Load(System.Object sender, System.EventArgs e)
{
    OvalShape NewOval = new OvalShape();
    int i;
    bool found = false;
    // Loop through the Controls collection.
    for (i = 0; i < this.Controls.Count; i++)
    {
        // If a ShapeContainer is found, make it the parent.
        if (this.Controls[i] is ShapeContainer)
        {
            NewOval.Parent = ((ShapeContainer)this.Controls[i]);
            found = true;
            break;
        }
    }
    // If no ShapeContainer is found, create one and set the parents.
    if (found == false)
    {
        ShapeContainer sc = new ShapeContainer();
        sc.Parent = this;
        NewOval.Parent = sc;
    }
    NewOval.Size = new Size(200, 300);
}

.NET Framework Security

See Also

Reference

ShapeContainer Class

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)