快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

反射应用之一:根据控件名、属性名进行取值和赋值

\\\'必须引用命名空间System.Reflection,System.ComponentModel

\\\'以下根据控件名和属性名取值

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 Is Nothing Then

Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(myType)

Dim myProperty As PropertyDescriptor = properties.Find(PropertyName, False)

If Not myProperty Is 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

\\\'以下根据控件名和属性名赋值

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) \\\'加"_"这个是特要紧的

If Not myFieldInfo Is Nothing Then

Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(myType)

Dim myProperty As PropertyDescriptor = properties.Find(PropertyName, False) \\\'这里设为True就不用区分大小写了

If Not myProperty Is Nothing Then

Dim ctr As Object

ctr = myFieldInfo.GetValue(ClassInstance) \\\'取得控件实例

Try

myProperty.SetValue(ctr, Value)

Result = ctr

Catch ex As Exception

MsgBox(ex.Message)

End Try

End If

End If

Return Result

End Function

\\\'调用

\\\'以下实现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





返回类别: 教程
上一教程: .net探针
下一教程: .net辅助工具列表

您可以阅读与"反射应用之一:根据控件名、属性名进行取值和赋值"相关的教程:
· 使用反射实现根据名称动态创建窗体的几种方式
· 在.NET上如何根据字符串动态创建控件
· 字符串根据多个字符进行分割的一种方式
· 一个功能完善的专栏治理的程序->这是asp.net的第二个应用(一)
· 使用 ASP+ 列表绑定控件(中)
    微笑服务 优质保证 索取样品