vb 自定义函数中 可以包含事件吗

来源:百度知道 编辑:UC知道 时间:2024/05/14 06:48:34
我想实现下面的功能
一行文本框分别是text1(0),text1(1)...text1(12),当文本框得到焦点时底色改变,失去焦点时底色恢复原样。
不用函数这样写可以实现:
Private Sub Text1_GotFocus(Index As Integer)
Text1(Index).BackColor = vbred
End Sub

Private Sub Text1_LostFocus(Index As Integer)
Text1(Index).BackColor = vbWhite
End Sub

但还有text2,text3,我想自己编个函数,编写如下
Public Function changecolor(text As String, Index As Integer) '自定义函数-得焦点变色,失焦点恢复
If text_gotfocus Then text(Index).BackColor = vbred
If text_lostfocus Then text(Index).BackColor = vbWhite
End Function

changecolor (Text1)

不能成功,请问各位应该怎么修改呢?

只能用前面的方法,如果一定要用函数判断可以这样

dim i as integer

Private Sub Text1_GotFocus(Index As Integer)
i=index
End Sub

Private Sub Text1_LostFocus(Index As Integer)
i=-1
End Sub

Public Function changecolor(text As textbox) '自定义函数-得焦点变色,失焦点恢复
if text.index=i then
text.BackColor = vbred
else
text.BackColor = vbWhite
endif

End Function

changecolor (Text1(1))

If text_gotfocus Then text(Index).BackColor = vbred
If text_lostfocus Then text(Index).BackColor = vbWhite

这种写法是不行的!

vb是事件驱动语言。
所以,你应该写作(当然你必须的有一个控件数组叫做text):
private sub text_gotfocus(Index as integer)
text(Index).BackColor = vbred
end sub
其余依次类推

既然是事件
那就用SUB