Gets or set Control Properties from another thread

Introduction

Sometimes you need to change some of the control properties from another thread, in such a case, System.Reflection.PropertyInfo Class will come handy.

Code

 ' Refert to MSDN, PropertyInfo Class
    ' http://msdn.microsoft.com/en-us/library/vstudio/System.Reflection.PropertyInfo%28v=vs.110%29.aspx
    Public Shared Function GetControlPropertyValue(name As String, control As System.Windows.Forms.Control) As Object
        Dim info As System.Reflection.PropertyInfo = control.GetType().GetProperty(name)
        Return info.GetValue(control, Nothing)
    End Function

    ' Refert to MSDN, PropertyInfo Class
    ' http://msdn.microsoft.com/en-us/library/vstudio/System.Reflection.PropertyInfo%28v=vs.110%29.aspx
    Public Shared Sub SetControlPropertyValue(name As String, control As System.Windows.Forms.Control, value As Object)
        Dim info As System.Reflection.PropertyInfo = control.GetType().GetProperty(name)
        info.SetValue(control, value, Nothing)
    End Sub


Implementation

 

    ' Example 1
        ' Get and Set backcolor of the form
        Dim FormBackColor As Color = ReflectionUtility.GetControlPropertyValue("BackColor", Me)
        If FormBackColor = SystemColors.Control Then
            Dim formColor As Color = Color.FromArgb(0, 0, 0)
            ReflectionUtility.SetControlPropertyValue("BackColor", Me, formColor)
        End If

        ' Example 2
        ' Set form text
        ReflectionUtility.SetControlPropertyValue("Text", Me, "PropertyInfo Class Test")


Enjoy the code

Comments

Popular posts from this blog

Image Transition in VB.NET Windows Forms

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

مقدمة إلي إخفاء المعلومات - الجزء الثاني