Shape.Parent Property

 

Gets or sets the parent container of a line or shape control.

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

[BrowsableAttribute(false)]
public ShapeContainer Parent { get; set; }

Property Value

Type: Microsoft.VisualBasic.PowerPacks.ShapeContainer

A ShapeContainer that represents the parent or container of the control.

A LineShape, OvalShape, or RectangleShape control can be contained only in a ShapeContainer object, which acts as a canvas for line and shape controls.

When you add a line or shape to a form or container at design time, a ShapeContainer is automatically created if one does not already exist. The Parent property of the line or shape is set to that ShapeContainer. The Parent property of the ShapeContainer is set to the form or container control to which the line or shape was added.

When you create a line or shape at run time by using the New method, you must set its Parent property 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.

System_CAPS_noteNote

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 writing code to create one programmatically, you should modify that code to use the ShapeContainer created by the designer.

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

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)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);
found = true;
Return to top
Show: