Retrieve Owner and Form of a Control
أحيانا و عندما تقوم بكتابة كونترول جديد قد نحتاج الي الحصول علي الكونترول الموجود به الكونترول الخاص بنا وأيضا قد نحتاج ان نحصل علي الفورم الموجود به الكونترول الخاص بنا
و لكي نفعل هذا علينا فقط ان نضيف للكونترول الخاص بنا هاتين الصفتين
' Retrieve container of our control
Public Property Owner As Control
Get
Return MyBase.Parent
End Get
Set(value As Control)
If MyBase.Parent IsNot value Then
If value Is Nothing Then
value.Controls.Add(Me)
End If
End If
MyBase.Parent.Controls.Remove(Me)
End Set
End Property
' ' Retrieve form of our control
Public ReadOnly Property Form As ContainerControl
Get
Dim _designer As Control = MyBase.Parent
While _designer IsNot Nothing
If TypeOf _designer Is ContainerControl Then
Return TryCast(_designer, ContainerControl)
End If
_designer = _designer.Parent
End While
Return Nothing
End Get
End Property
Example:
Public Class CairoControl
Inherits Control
' Retrieve container of our control
Public Property Owner As Control
Get
Return MyBase.Parent
End Get
Set(value As Control)
If MyBase.Parent IsNot value Then
If value Is Nothing Then
value.Controls.Add(Me)
End If
End If
MyBase.Parent.Controls.Remove(Me)
End Set
End Property
' ' Retrieve form of our control
Public ReadOnly Property Form As ContainerControl
Get
Dim _designer As Control = MyBase.Parent
While _designer IsNot Nothing
If TypeOf _designer Is ContainerControl Then
Return TryCast(_designer, ContainerControl)
End If
_designer = _designer.Parent
End While
Return Nothing
End Get
End Property
End Class
و لكي نفعل هذا علينا فقط ان نضيف للكونترول الخاص بنا هاتين الصفتين
' Retrieve container of our control
Public Property Owner As Control
Get
Return MyBase.Parent
End Get
Set(value As Control)
If MyBase.Parent IsNot value Then
If value Is Nothing Then
value.Controls.Add(Me)
End If
End If
MyBase.Parent.Controls.Remove(Me)
End Set
End Property
' ' Retrieve form of our control
Public ReadOnly Property Form As ContainerControl
Get
Dim _designer As Control = MyBase.Parent
While _designer IsNot Nothing
If TypeOf _designer Is ContainerControl Then
Return TryCast(_designer, ContainerControl)
End If
_designer = _designer.Parent
End While
Return Nothing
End Get
End Property
Example:
Public Class CairoControl
Inherits Control
' Retrieve container of our control
Public Property Owner As Control
Get
Return MyBase.Parent
End Get
Set(value As Control)
If MyBase.Parent IsNot value Then
If value Is Nothing Then
value.Controls.Add(Me)
End If
End If
MyBase.Parent.Controls.Remove(Me)
End Set
End Property
' ' Retrieve form of our control
Public ReadOnly Property Form As ContainerControl
Get
Dim _designer As Control = MyBase.Parent
While _designer IsNot Nothing
If TypeOf _designer Is ContainerControl Then
Return TryCast(_designer, ContainerControl)
End If
_designer = _designer.Parent
End While
Return Nothing
End Get
End Property
End Class
Comments
Post a Comment