Dim RTB As RichTextBox If RTB Is Nothing Then MsgBox "have no richtextbox" Else MsgBox "have richtextbox" End If
如果不存在的话,编译会出错的。 或者,你可以长是对他进行操作,通过错误处理来判断是否存在。 on error go to e mycontrol.top=1 .... e: if err.number=xxxxx then ishavecontrol=false resume next else .....
GetObject([PathName] as string,[ClassName] as String)Returns a reference to an object provided by an ActiveX component
Returns a reference to an object provided by an ActiveX component.SyntaxGetObject([pathname] [, class])
请看: Private Function FindRtext() As Boolean Dim CtlX As Control For Each CtlX In Controls If TypeName(CtlX) = "RichTextBox" Then FindRtext = True End If Next End Function
Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hwndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long 可以按類名 或 控件的標題找
for i=1 to me.Controls.Count if me.Controls(i-1).name="RichTextBox1" then ControlExit = True exit for end if next
If RTB Is Nothing Then
MsgBox "have no richtextbox"
Else
MsgBox "have richtextbox"
End If
或者,你可以长是对他进行操作,通过错误处理来判断是否存在。
on error go to e
mycontrol.top=1
....
e:
if err.number=xxxxx then
ishavecontrol=false
resume next
else
.....
Private Function FindRtext() As Boolean
Dim CtlX As Control
For Each CtlX In Controls
If TypeName(CtlX) = "RichTextBox" Then
FindRtext = True
End If
Next
End Function
可以按類名 或 控件的標題找
if me.Controls(i-1).name="RichTextBox1" then
ControlExit = True
exit for
end if
next
这只能看当前窗口中有没有指定控件,如果程序发行时没有带上这个控件,而程序中使用了这个控件,则程序可能无法执行到这一步。
for each rechtextbox1 in me.controls
if typeof rechtextbox1 is richtextbox then
msgbox "richtextbox is exists"
endif
next
For Each X In Form1.Controls
If X.Name <> Richtextbox Then
MsgBox "ok"
End If
Next X
我动态的加载一个richtextbox,用你的代码可以判断存在,可是但当又将其,卸掉,再判断是否存在,为何还判断它存在呢?