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

首頁 > 開發 > 綜合 > 正文

按控件實例名稱及其屬性名稱實現動態賦值

2024-07-21 02:24:10
字體:
來源:轉載
供稿:網友

'按控件實例名稱及其屬性名稱實現動態賦值

'入口參數:classinstance 控件所在的類實例

' controlname 控件實例名稱,區分大小寫

' propertyname 要設值的控件屬性名稱,區分大小寫(其實這里可以不必區分大小寫的,只是為了養成習慣,我這樣要求自己的)

' value 新值,類型是一個object,這倒是要注意的

'出口參數: true則重設成功,false不成功

'需要 imports system.reflection和imports system.componentmodel

public function setvaluecontrolproperty(byval classinstance as object, byval controlname as string, byval propertyname as string, byval value as object) as boolean

dim result as boolean = false '返回值。雖然默認是flase,但我還是喜歡這樣設它,主要是看著明了

'下面我不注釋了

dim mytype as type = classinstance.gettype

dim myfieldinfo as fieldinfo = mytype.getfield("_" & controlname, bindingflags.nonpublic or _

bindingflags.instance or bindingflags.public) '加"_"這個是特要緊的



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 = true

catch ex as exception

msgbox(ex.message)

end try

end if

end if



return result

end function



'測試

private sub test()

setvaluecontrolproperty(me, "button1", "text", "hello")

setvaluecontrolproperty(me, "button2", "visible", false)

dim frm as new form2

setvaluecontrolproperty(frm, "mytextbox", "text", "應該還行吧?")

frm.show()

end sub


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万山特区| 洛浦县| 深泽县| 曲水县| 江北区| 太仆寺旗| 涞水县| 南阳市| 南华县| 锡林浩特市| 抚顺市| 克山县| 如皋市| 健康| 左贡县| 齐齐哈尔市| 永嘉县| 化德县| 八宿县| 兴化市| 濮阳市| 清徐县| 确山县| 清镇市| 嫩江县| 神农架林区| 淅川县| 东乡县| 天津市| 三明市| 石首市| 陇南市| 古浪县| 会理县| 辛集市| 永福县| 祥云县| 红原县| 玉门市| 安宁市| 临清市|