代码之家  ›  专栏  ›  技术社区  ›  Simos Sigma

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

  •  0
  • Simos Sigma  · 技术社区  · 6 年前

    我正在做一个 UserControl 我正在寻找一种方法 防止 开发商至 位置 用户控件 多次转换为一种形式。所有这些 设计时间 换句话说,我怎么能 发现 如果我的 用户控件 已经是 已放置 进入 ParentForm 或不是,在 设计时间 (!!!),和 防止 这个 第二次安置 如果已经有了?

    我试过下面这个例子。。。首先,我不确定这是否是“正确”的方式,其次,我找不到如何删除或停止放置 用户控件 万一已经有了。

    再一次,所有这些,在 设计时间 !!!

    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
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Norbert Ziemniak    6 年前

    因为这种形式很容易使用,新的和处理,它不适用于打断奶的形式。只有在这个相同的地方。您可以使用互斥体或此模块属性的某个单例即时,或其他通知方法,告知此模块已创建以及何时释放。

        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
    

    结束模块

        2
  •  0
  •   Simos Sigma    6 年前

    最后我结束了。。。

    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