تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
Generic Delegates & ًWindows Forms Control - Part 1
#1
لسلام عليكم ورحمة الله وبركاته

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

الهدف:
الهدف من هذا الموضوع هو مشاركة بعض الافكار البرمجية

المثال

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

PHP كود :
  Friend Function GetControlBackColor(ctrl As Control) As Color
        Return CType
((ctrl.Invoke(New Func(Of Color)(Function() ctrl.BackColor))), Color)
 
   End Function 

بشكل عام الاسلوب المستخدم في هذا المثال يمكن استخدامه لإسترجاع قيمة اي صفة من صفات الكونترول

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



PHP كود :
    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 


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

الكود التالي يوضح شكل الدالة التي يمكن استخدامها و تطويرها للحصول علي كل صفات الكونترول
PHP كود :
   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 wy to get other control properties
                '
 
               '
        End Select
    End Function 

PHP كود :
Friend Enum ControlProperties
    Handle
    Bounds
    ClientRectangle
    BackColor
    ForeColor
    DoubleBuffered
End Enum 

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


PHP كود :
       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
الرد }}}
تم الشكر بواسطة: khodor1985 , khodor1985
#2
شرح ممتازززززززززززززز
الرد }}}
تم الشكر بواسطة:



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


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