One reflection application: values and assignments based on control names, property names

Source: Internet
Author: User
Tags reflection
Control ' must reference a namespace System.reflection,system.componentmodel

' The following values are based on the control name and property name

Public Function Getvaluecontrolproperty (ByVal classinstance as Object, ByVal controlname as String, ByVal PropertyName as String) as Object

Dim result as Object

Dim MyType as Type = Classinstance.gettype

Dim Myfieldinfo as FieldInfo = Mytype.getfield ("_" & ControlName, BindingFlags.NonPublic Or _

BindingFlags.Instance or BindingFlags.Public or bindingflags.instance)

If not myfieldinfo are nothing Then

Dim properties as PropertyDescriptorCollection = Typedescriptor.getproperties (MyType)

Dim MyProperty as PropertyDescriptor = properties. Find (PropertyName, False)

If not MyProperty are nothing Then

Dim Ctr as Object

CTR = Myfieldinfo.getvalue (classinstance)

Try

result = Myproperty.getvalue (CTR)

Catch ex as Exception

MsgBox (ex. Message)

End Try

End If

End If

return result

End Function

' The following assigns values based on the control name and property name

Public Function Setvaluecontrolproperty (ByVal classinstance as Object, ByVal controlname as String, ByVal PropertyName as String, ByVal Value as Object) as Object

Dim result as Object

Dim MyType as Type = Classinstance.gettype

Dim Myfieldinfo as FieldInfo = Mytype.getfield ("_" & ControlName, BindingFlags.NonPublic _

or bindingflags.instance or BindingFlags.Public or bindingflags.instance) ' Plus ' _ ' This is a special matter.

If not myfieldinfo are nothing Then

Dim properties as PropertyDescriptorCollection = Typedescriptor.getproperties (MyType)

Dim MyProperty as PropertyDescriptor = properties. Find (PropertyName, False) ' Set to true here is not case-sensitive

If not MyProperty are nothing Then

Dim Ctr as Object

CTR = Myfieldinfo.getvalue (classinstance) ' Gets control instance

Try

Myproperty.setvalue (CTR, Value)

result = Ctr

Catch ex as Exception

MsgBox (ex. Message)

End Try

End If

End If

return result

End Function

' Call

' Following implementation Label1.text=textbox1.text,label2.text=textbox2

Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click

Dim I as Integer

For i = 1 to 2

Me.setvaluecontrolproperty (Me, "Label" & i.ToString, "Text", Getvaluecontrolproperty (Me, TextBox & i). ToString, "Text"))

Next I

End Sub



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.