怎样判断一个控件是否存在?
在Form上,有一个Label1(i)数组。
是否有办法可以判断Label1(i)是否存在?
其中,Label1(i)是动态Load的。
别告诉我可以通过i来判断啊。
:)
问题点数:20、回复次数:10Top
1 楼gump2000(阿甘)回复于 2002-04-11 13:50:25 得分 10
错误捕捉了:)
function isexists(aaa as control) as boolean
dim tmp as string
aaa=true
on error goto err1
tmp=aaa.name
exit sub
err1:
aaa=false
end functionTop
2 楼gump2000(阿甘)回复于 2002-04-11 13:51:10 得分 0
function isexists(aaa as control) as boolean
dim tmp as string
isexists=true
on error goto err1
tmp=aaa.name
exit sub
err1:
isexists=false
end function
Top
3 楼lihonggen0(李洪根,MS MVP,标准答案来了)回复于 2002-04-11 14:00:08 得分 0
If obj Is Nothing Then
MsgBox "obj 不存在"
End If
Top
4 楼wangsitao(飞猪)回复于 2002-04-11 14:27:41 得分 0
agree with gump2000(阿甘)
错误捕捉了:)
Top
5 楼Chimae(David)回复于 2002-04-11 14:36:36 得分 0
同意lihonggen0(用VB)!Top
6 楼enmity(灵感之源)回复于 2002-04-11 15:07:32 得分 10
to: lihonggen0(用VB) & Chimae(齐藤)
使用该方法是行不通的,请做测试。
例子代码:判断控件(特别是控件数组)是否存在(
http://www.csdn.net/Expert/TopicView1.asp?id=574473
Private Sub Command1_Click()
MsgBox DoesControlExist(Option1(1))
End Sub
Private Function DoesControlExist(ByRef ctl As Control) As Boolean
On Error GoTo handleError
DoesControlExist = (ctl.Name <> vbNullString)
Exit Function
handleError:
DoesControlExist = False
End Function
Top
7 楼rushing(勇敢的心)回复于 2002-04-11 15:20:55 得分 0
好像不灵啊。
Top
8 楼gump2000(阿甘)回复于 2002-04-11 15:24:47 得分 0
Function isexists(aaa As Control) As Boolean
Dim tmp As String
isexists = True
On Error GoTo err1
tmp = aaa.Name
Exit Function
err1:
isexists = False
End Function
Private Sub Command4_Click()
MsgBox isexists(Option1(1))
End Sub
还可以啊:)
Top
9 楼rushing(勇敢的心)回复于 2002-04-11 15:28:30 得分 0
因为要循环判断许多控件是否存在,所以用错误机制不可取。
Top
10 楼enmity(灵感之源)回复于 2002-04-11 17:50:48 得分 0
to:rushing(勇敢的心)
利用历遍所有控件,判断名字和Index(如果是控件数组),这个方法是可以的,不过,时间上,比利用错误捕捉来直接判断,要稍差点。
而且,利用错误捕捉,通过测试,是可行的!Top




