国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 婺源县| 延庆县| 太和县| 田东县| 元氏县| 枝江市| 崇礼县| 汶川县| 白玉县| 腾冲县| 罗源县| 仙桃市| 柳州市| 汶川县| 太湖县| 陈巴尔虎旗| 苏尼特左旗| 华亭县| 云阳县| 韶山市| 桃园市| 新建县| 临猗县| 合肥市| 息烽县| 庄河市| 道孚县| 施秉县| 贵阳市| 沙田区| 阿拉尔市| 密云县| 昔阳县| 新昌县| 宿州市| 鹿泉市| 屏山县| 柳河县| 东乡族自治县| 绵阳市| 布尔津县|