防止开发人员在设计时多次放置 UserControl

Prevent developer from placing a UserControl more than one time in Design Time

我正在研究 UserControl,我正在寻找一种方法来 防止 开发人员 放置 这个 UserControl不止一次变成一个表格。所有这些都在 设计时间 中完成。换句话说,如果我的 UserControl 已经 放置 ParentForm 或不在 设计时间(!!!),并且防止 第二个放置 如果那里已经有一个?

我尝试了类似下面的示例...首先我不确定这是否是 "correct" 方式,其次我找不到如何删除或停止放置 UserControl如果已经有一个。

同样,所有这些都在设计时!!!

Private Sub MyUserControl_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Dim _Count As Integer
    Dim _UserControl As MyUserControl
    For Each _UserControl In Me.ParentForm.Controls
        If _UserControl.Name.Contains("MyUserControl") Then
            _Count += 1
        End If
    Next
    If _Count > 1 Then
        MsgBox("Control have been placed.")
    Else
        MsgBox("Control haven't placed yet.")
    End If
End Sub

因为这个相同的表格很容易使用新的和处置,它不适用于 beat weans 表格。只有在这个相同的。你可以使用这个模块的互斥锁或一些单例实例 属性 或其他通知方法,它是创建的以及何时被处理的。

    Sub New()
    '  This call is required by the designer.
    InitializeComponent()

    If Not co Is Nothing Then Throw New Exception
    co = Me ' assign public propert in  module or singleton

    ' Add any initialization after the InitializeComponent() call.

End Sub
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        co = Nothing
        MyBase.Dispose(disposing)
    End Try
End Sub
Module common
Property co As UserControl1

模块结束

最后我就这样结束了...

Private Sub Me_ParentChanged(sender As Object, e As EventArgs) Handles Me.ParentChanged

    Dim _ParentForm = DirectCast(Me.FindForm, Control)
    Dim _ControlName As String

    If _ParentForm IsNot Nothing Then
        For Each _Control As Control In _ParentForm.Controls
            If TypeOf _Control Is MyUserControl AndAlso _Control IsNot Me Then
                Throw New ArgumentOutOfRangeException("", "You can place only one " & _ControlName & " control per form.")
            End If
            _Control = _ParentForm.GetNextControl(_Control, True)
        Next
    End If

End Sub