تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
Control Finder Class
#1
من فترة كنت كتبت بعص الأكواد لإيجاد جميع الكونترول الموجودة في اي كونترول

و الكود موجود في اللينك التالي

اللينـــــــــــــــــــــــــك

لكن و مع التطور الحاصل في الدوت نت قررت أخيرا تحويل معظم الأكواد الي كلاسات بحيث يسهل تطويرها و الاستفادة منها في اي مشروعات مستقبلة

و الكود التالي عبارة عن مساهمة جيدة يمكن إضافتها الي اي مكتبة يتم كتابتها للدوت نت


الكلاس
PHP كود :
Public Class ControlFinder

    Private _controls 
As IEnumerable(Of Control)

 
   Public Sub New(ctrl As Control)
 
       Me.New(ctrl, Function(cc IsNot Nothing AndAlso Not c.IsDisposed AndAlso c.IsHandleCreated AndAlso c.Visible)
 
   End Sub

    Public Sub 
New(ctrl As Controlcriteria As Func(Of ControlBoolean))
 
       If ctrl Is Nothing Then
            _controls 
= New Control() {}
 
       Else
            _controls 
GetControls(ctrl.Controlsctrlcriteria)
 
       End If
 
   End Sub

    Public ReadOnly Property Controls 
As IEnumerable(Of Control)
 
       Get
            Return _controls
        End Get
    End Property

    Private Sub FindControls
(parent As Controlcontrols As HashSet(Of Control), criteria As Func(Of ControlBoolean))
 
       If parent Is Nothing Then
            Return
        End 
If
 
       For Each ctrl As Control In parent.Controls
            If criteria
(ctrlThen
                controls
.Add(ctrl)
 
           End If

 
           If ctrl.HasChildren Then
                FindControls
(ctrlcontrolscriteria)
 
           End If
 
       Next
    End Sub

    Private 
Function GetControls(list As ICollectionparent As Controlcriteria As Func(Of ControlBoolean)) As HashSet(Of Control)
 
       If list Is Nothing OrElse parent Is Nothing Then
            Return Nothing
        End 
If
 
       Dim controls As HashSet(Of Control) = New HashSet(Of Control)()
 
       For Each ctrl As Control In list
 
           controls.Add(ctrl)
 
           FindControls(parentcontrolscriteria)
 
       Next
        Return controls
    End 
Function

End Class 


الإستخدام

PHP كود :
   Private Sub Form1_Load(sender As ObjectAs EventArgsHandles MyBase.Load
        Dim finder 
As New ControlFinder(Me)
 
       Dim count finder.Controls.Count
        If count 
0 Then
            Me
.Text count
        End 
If

 
       ' or
        For Each c As Control In finder.Controls
            ' 
do something
        Next
    End Sub 
الرد }}}
تم الشكر بواسطة: Amir_Alzubidy , محمد كريّم
#2
الله يحفظك أخي الكريم وان يجعلها لك في ميزان الحسنات
الرد }}}
تم الشكر بواسطة: Amir_Alzubidy
#3
تعديل بسيط علي الكود

PHP كود :
  Public Sub New(ctrl As Controlcriteria As Func(Of ControlBoolean))
 
       _controls = If(Not criteria(ctrl), New Control() {}, GetControls(ctrl.Controlsctrlcriteria))
 
   End Sub 
الرد }}}
تم الشكر بواسطة: أبو عمر , Amir_Alzubidy
#4
بارك الله فيك
سبحان الله والحمدلله ولا إله إلا الله والله أكبر
 اللهم اغْفِرْ لِلمؤمنين والمؤمنات والمسلمين والمسلمات الأحياء منهم والأموات
الرد }}}
تم الشكر بواسطة: Amir_Alzubidy



التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم