Recursively find all controls on a Form إيجاد جميع الكونترول الموجودة في الفورم





السلام عليكم و رحمة الله وبركاته

مقدمة:

أحد اهم الاسئلة الشائعة في النت هو كيف أمسح جميع التكست الموجود في مجموعة من التكست بوكس كونترول الموجودة علي الفورم؟

بالتأكيد جيمع الاجابات التي سوف تجدها علي النت هي إجابات صحيحية و مناسبة لكن يتبقي السؤال

ماذا لو كان هذا التكست بوكس ليس موجودا بشكل مباشر علي الفورم نفسه بل موجودا داخل كونترول أخر مثل  Panel  أو مثل GroupBox أو مثل TabControl ؟

الإجابة علي مثل هذا السؤال تنحصر في كلمة واحدة و هي Recursion

وهنا قد يتبارد الي ذهن البعض ما هو تعريف هذا الشئ الذي نطلق عليه Recursion ؟

تعريف   Recursion

هو أن تقوم الدالة Function او الروتين Sub او الطريقة Method  بإستدعاء نفسها من داخل نفسها
وهذا يعني أننا هنا نقوم بعمل Looping بدون استخدام جمل برمجية مثل For .... Next او  For .... Each
أي أن Recursion   وببساطة شديدة هو شئ عكس كلمة  Iteration أو بشكل أكثر دقة فإن  Recursion هو عبارة عن محاولة إيجاد حل لمشكلة رئيسية Main Problem و ايضا إيجاد حلول لجميع المشاكل الفرعية Minor Problems المتصلة بالمشكلة الرئيسية ..... مع الوضع في الاعتبار أن المشكلتان من نفس النوعية

مثال توضيحي :

عندما تحاول أن تجد جميع الكونترول علي الفورم فأنت هنا تحاول إيجاد حل لمشكلة رئيسية و عندما تحاول أن تبحث داخل بقية الكونترول الاخري علي الفورم فانت هنا تحاول إيجاد حل لمشكلة فرعية و لكنها من نفس نوعية المشكلة الرئيسية. الكود اتالي يوضح الفكرة بشكل أفضل من الحديث النظري

 
الكود:

لقد قمت باستخدام HashSet(Of T) Class لأنه لا يكرر المدخلات و يمكن استخدام اي Collection  مناسبة بدلا منه


     '  Method 1
    ..........................................................................................

    ' This Method will detect contols only and components will not detected
    Friend Shared Sub FindControls(ctrlContainer As Control, controls As HashSet(Of Control))
        If ctrlContainer Is Nothing Then
            Return
        End If
        For Each ctrl As Control In ctrlContainer.Controls
            controls.Add(ctrl)
            ' If the control has hildren, recursively call this Method
            If ctrl.HasChildren Then
                FindControls(ctrl, controls)
            End If
        Next
    End Sub

    ' This Function will return contols only and components will not be returned
    Friend Shared Function GetControls(list As ICollection, ctrlContainer As Control) As HashSet(Of Control)
        If list Is Nothing OrElse ctrlContainer 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(ctrlContainer, controls)
        Next
        Return controls
    End Function


    '  Method 2
    ..........................................................................................

    ' This Function will return contols only and components will not be returned
    Public Shared Function GetControls(ctrls As ICollection) As HashSet(Of Control)
        Dim controls As New HashSet(Of Control)()
        For Each ctrl As Control In ctrls
            controls.Add(ctrl)
            If ctrl.HasChildren Then
                Dim childs As HashSet(Of Control) = GetControls(ctrl.Controls)
                For Each child As Control In childs
                    controls.Add(child)
                Next
            End If
        Next
        Return controls
    End Function




كيفية استخدام الكود:


    '  Code Usage
    ..........................................................................................

    '  Method 1 Usage

        For Each ctrl As Control In GetControls(Me.GroupBox1.Controls, Me.GroupBox1)
            ListBox2.Sorted = True
            If TypeOf ctrl Is TextBox Then
                ctrl.Text = "أهلا بكم جميعا"
                ListBox2.Items.Add(ctrl.Name)
            End If
        Next

  '  Method 2 Usage

        For Each ctrl As Control In GetControls(Me.Controls)
            Me.ListBox1.Items.Add(ctrl.Name)
            Me.ListBox1.Sorted = True
            If TypeOf ctrl Is TextBox Then
                ctrl.Text = "Hello World"
            End If
        Next

  '  Method 2 Usage

        ' مسح جميع التكست الموجودة في التكست بوكس
        For Each ctrl As Control In GetControls(Me.Controls)
            If TypeOf ctrl Is TextBox Then
                ctrl.Text = ""
            End If
        Next


    '  References
    ..........................................................................................

1 ) HashSet(Of T) Class

Comments

Post a Comment

Popular posts from this blog

مقدمة الي تشفير الحروف الأبجدية العربية

VB.NET Translucent Control using GDI+

Add Custom Event to a Class in VB.NET