Generic Delegates & ًWindows Forms Control - Part 1

في هذا الموضوع سنوضح كيفية الاستفادة من
Func and Action Delegates
في استرجاع قيمة من صفات الكونترول

كيف نسترجع بعضا من صفات الكونترول
Control BackColor
الدالة التالية توضح كيفية الحصول علي لون خلفية الكونترول
  
 Friend Function GetControlBackColor(ctrl As Control) As Color
        Return CType
((ctrl.Invoke(New Func(Of Color)(Function() ctrl.BackColor))), Color)
 
   End Function
 
بشكل عام الاسلوب المستخدم في هذا المثال أعلاه يمكن استخدامه لإسترجاع قيمة اي صفة من صفات الكونترول

الكود التالي يوضح كيفية استرجاع بعض القيم الخاصة بالكونترول و بنفس الاسلوب المستخدم في المثال 

   Friend Function GetControlHandle(ctrl As Control) As IntPtr
        Return CType
((ctrl.Invoke(New Func(Of IntPtr)(Function() ctrl.Handle))), IntPtr)
 
   End Function

 
   Friend Function GetControlBounds(ctrl As Control) As Rectangle
        Return CType
((ctrl.Invoke(New Func(Of Rectangle)(Function() ctrl.Bounds))), Rectangle)
 
   End Function 
 
 طبعا ليس من المنطقي اننا و عندما نريد ان نحصل علي صفة نضطر لكتابة دالة خاصة بهذه الصفة لذلك و من الأفضل لنا أن نكتب شئ عام نستطيع استخدامه مع اي كونترول

الكود التالي يوضح شكل الدالة التي يمكن استخدامها و تطويرها للحصول علي كل صفات الكونترول

   Friend Function GetControlProperty(Of T)(ctrl As ControlpropertyName As ControlProperties) As T
        Dim result 
As CType(NothingT)
 
       Select Case propertyName
            Case ControlProperties
.Handle
                result 
= (ctrl.Invoke(New Func(Of IntPtr)(Function() ctrl.Handle)))
 
           Case ControlProperties.Bounds
                result 
= (ctrl.Invoke(New Func(Of Rectangle)(Function() ctrl.Bounds)))
 
           Case ControlProperties.BackColor
                result 
= (ctrl.Invoke(New Func(Of Color)(Function() ctrl.BackColor)))

 
               ' continue the same way to get other control properties
                '
                
'
        End Select
    End Function 

Friend Enum ControlProperties
    Handle
    Bounds
    ClientRectangle
    BackColor
    ForeColor
    DoubleBuffered
End Enum 

  الكود التالي يوضح بعض الاساليب لكيفية استخدام الدالة أعلاه

 Dim clr As Color GetControlProperty(Of Color)(MeControlProperties.BackColor)
 
Dim ptr As IntPtr GetControlProperty(Of IntPtr)(MeControlProperties.Handle) 

Dim rect As Rectangle GetControlProperty(Of Rectangle)(MeControlProperties.Bounds


   

Comments

Popular posts from this blog

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

VB.NET Translucent Control using GDI+

Add Custom Event to a Class in VB.NET