ShapeContainer 类
更新:2007 年 11 月
为 LineShape、OvalShape、RectangleShape 控件和派生自 Shape 的任何其他控件提供一个容器。
命名空间: Microsoft.VisualBasic.PowerPacks
程序集: Microsoft.VisualBasic.PowerPacks.Vs(在 Microsoft.VisualBasic.PowerPacks.Vs.dll 中)
语法
声明
<BrowsableAttribute(False)> _
Public NotInheritable Class ShapeContainer _
Inherits UserControl
用法
Dim instance As ShapeContainer
[BrowsableAttribute(false)]
public sealed class ShapeContainer : UserControl
[BrowsableAttribute(false)]
public ref class ShapeContainer sealed : public UserControl
public final class ShapeContainer extends UserControl
备注
LineShape、OvalShape 或 RectangleShape 控件只能包含在 ShapeContainer 对象中,该对象充当线条或形状控件的画布。
在设计时向窗体或容器添加线条或形状控件时,会自动创建一个 ShapeContainer(前提是它尚不存在)。线条或形状控件的 Parent 属性设置为 ShapeContainer 的属性。ShapeContainer 的 Parent 属性设置为线条或形状所添加到的窗体或容器控件。
在运行时使用 New 方法创建线条或形状时,必须将控件的 Parent 属性设置为 ShapeContainer。如果窗体或容器已经存在 ShapeContainer,则应该将 Parent 属性设置为该 ShapeContainer。如果不存在 ShapeContainer,则可以使用 New 方法创建一个 ShapeContainer,并将它的 Parent 属性设置为窗体或容器。
说明: |
---|
请注意,不要为一个窗体或容器创建一个以上的 ShapeContainer;否则可能会引入意外的行为。在通过编写代码来以编程方式创建窗体或容器后,如果向其中添加设计时线条或形状控件,则应该修改该代码,让线条或形状控件使用由设计器创建的 ShapeContainer。 |
示例
下面的示例检查是否存在 ShapeContainer,并为在运行时使用 New 方法创建的 OvalShape 控件设置 Parent 属性。
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) 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);
}
继承层次结构
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ScrollableControl
System.Windows.Forms.ContainerControl
System.Windows.Forms.UserControl
Microsoft.VisualBasic.PowerPacks.ShapeContainer
线程安全
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
另请参见
参考
Microsoft.VisualBasic.PowerPacks 命名空间
其他资源
Line 和 Shape 控件简介 (Visual Studio)