国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > 綜合 > 正文

反射應(yīng)用之一:根據(jù)控件名、屬性名進(jìn)行取值和賦值

2024-07-21 02:17:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
'必須引用命名空間system.reflection,system.componentmodel

'以下根據(jù)控件名和屬性名取值

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

'以下根據(jù)控件名和屬性名賦值

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) '加"_"這個(gè)是特要緊的

if not myfieldinfo is nothing then

dim properties as propertydescriptorcollection = typedescriptor.getproperties(mytype)

dim myproperty as propertydescriptor = properties.find(propertyname, false) '這里設(shè)為true就不用區(qū)分大小寫了

if not myproperty is nothing then

dim ctr as object

ctr = myfieldinfo.getvalue(classinstance) '取得控件實(shí)例

try

myproperty.setvalue(ctr, value)

result = ctr

catch ex as exception

msgbox(ex.message)

end try

end if

end if

return result

end function

'調(diào)用

'以下實(shí)現(xiàn)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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 红河县| 崇明县| 盐源县| 屏边| 涟水县| 江都市| 庆城县| 东方市| 保靖县| 和顺县| 柳江县| 永和县| 偏关县| 安徽省| 石阡县| 江永县| 沈丘县| 固镇县| 桃园县| 拉萨市| 南陵县| 合水县| 松江区| 溆浦县| 宜君县| 浦东新区| 衡东县| 德格县| 屏东县| 顺义区| 长乐市| 鸡泽县| 井陉县| 遵化市| 梁平县| 阿尔山市| 札达县| 延安市| 延安市| 集安市| 金堂县|