首先要理解事件的本质,事件是在组件接收到某消息时,触发的一个过程,这个过程是写在客户端的,由客户编写。是为了方便客户根据不同情况做相应的处理而设置的。例如Commnad的Click事件就是为了在我们点击Commnad时让我们的程序可以做出反应。 自定义的控件中事件的触发位置可以由我们自己决定,例如: 定义一个变量 private NowValue as long 定义一个事件 event ValueChange(byval OldValue as long ,byval NewValue as long)在控件中有一个CmdADD按钮,在click时会为这个值加一,在CmdDec按钮,在Click时会使这个值减一. 那可以这样: private sub CmdADD_Click() NowValue=NowValue+1 raiseevent ValueChange(NowValue-1,NowValue) end sub private sub CmdDec_Click() NowValue=NowValue-1 raiseevent ValueChange(NowValue+1,NowValue) end sub下面再说你写的代码,那是一个属性过程,属性过程共有三种: let,get,set ,分别是简单类型写入时,读取时,对象类型写入时执行。 使用属性过程的好处是,可以在改变属时做出相应的处理,例如上面的例子, 在改变NowValue时我们还想把控件上一个label的caption同步改变,那这时属性过程就非常好用只要这样: Public Proerty Let sNowValue(byval vData as Long) dim oldValue as long oldValue=NowValue NowValue=Vdata raiseevent ValueChange(oldValue,NowValue) '上面的代码是为了和前面的统一。 lable1.Caption=NowValue end Proerty 另外在读取和对象写入过程中也可以加入一些处理代码,完善程序。
摘自MSDN的例子:Use the following steps on the server machine: Start Visual Basic 5.0 and create a new ActiveX EXE project. From the Project menu, selectProject properties and change the Project name to "Projtest." Add a Class Module and change its name to "Testclass." In Testclass' Code window, input following code: Public Event propchange(newv As String) Private propt As String Public Sub showfrm() MsgBox "asdfl" End Sub Public Property Get testp() As String testp = propt End Property Public Property Let testp(ByVal vNewValue As String) propt = vNewValue RaiseEvent propchange(propt) End Property On the File menu, click "Make ProjTest.exe." Copy the corresponding .exe, .tlb, and .vbr files to the client machine. Start the Automation Manager and the RemAuto Connection Manager to set necessary server-side configuration for using Projtest.testclass for Remote Automation. Use the following steps on the client machine: Run Projtest.exe, which was copied from the Server side in the above steps. Start the RemAuto Connection Manager to set necessary Client-side configuration for using Projtest.testclass. Launch Visual Basic 5.0 and create a new Standard EXE project with a standard form. From the Project menu, click References and select the "Projtest" box. In Form1's Code window, input following code: Public WithEvents eventp As testclass Private Sub eventp_propchange(newv As String) MsgBox newv End Sub Private Sub Form_Load() Dim a As New testclass a.showfrm Set eventp = a a.testp = "test string" End Sub Run the project and check if the Remote Automation connection is correct, the message box should pop up on the server-side machine. Click OK to close the message box on the server-side machine.
自定义的控件中事件的触发位置可以由我们自己决定,例如:
定义一个变量
private NowValue as long
定义一个事件
event ValueChange(byval OldValue as long ,byval NewValue as long)在控件中有一个CmdADD按钮,在click时会为这个值加一,在CmdDec按钮,在Click时会使这个值减一.
那可以这样:
private sub CmdADD_Click()
NowValue=NowValue+1
raiseevent ValueChange(NowValue-1,NowValue)
end sub
private sub CmdDec_Click()
NowValue=NowValue-1
raiseevent ValueChange(NowValue+1,NowValue)
end sub下面再说你写的代码,那是一个属性过程,属性过程共有三种:
let,get,set ,分别是简单类型写入时,读取时,对象类型写入时执行。
使用属性过程的好处是,可以在改变属时做出相应的处理,例如上面的例子,
在改变NowValue时我们还想把控件上一个label的caption同步改变,那这时属性过程就非常好用只要这样:
Public Proerty Let sNowValue(byval vData as Long)
dim oldValue as long
oldValue=NowValue
NowValue=Vdata
raiseevent ValueChange(oldValue,NowValue)
'上面的代码是为了和前面的统一。
lable1.Caption=NowValue
end Proerty
另外在读取和对象写入过程中也可以加入一些处理代码,完善程序。
这个上面说在事件源中编写事件代码,用什么WithEvents 是什么意思啊?
From the Project menu, selectProject properties and change the Project name to "Projtest."
Add a Class Module and change its name to "Testclass."
In Testclass' Code window, input following code:
Public Event propchange(newv As String)
Private propt As String Public Sub showfrm()
MsgBox "asdfl" End Sub Public Property Get testp() As String
testp = propt
End Property Public Property Let testp(ByVal vNewValue As String)
propt = vNewValue
RaiseEvent propchange(propt)
End Property
On the File menu, click "Make ProjTest.exe."
Copy the corresponding .exe, .tlb, and .vbr files to the client machine.
Start the Automation Manager and the RemAuto Connection Manager to set necessary server-side configuration for using Projtest.testclass for Remote Automation.
Use the following steps on the client machine: Run Projtest.exe, which was copied from the Server side in the above steps.
Start the RemAuto Connection Manager to set necessary Client-side configuration for using Projtest.testclass.
Launch Visual Basic 5.0 and create a new Standard EXE project with a standard form.
From the Project menu, click References and select the "Projtest" box.
In Form1's Code window, input following code:
Public WithEvents eventp As testclass Private Sub eventp_propchange(newv As String)
MsgBox newv
End Sub Private Sub Form_Load()
Dim a As New testclass
a.showfrm
Set eventp = a
a.testp = "test string"
End Sub
Run the project and check if the Remote Automation connection is correct, the message box should pop up on the server-side machine.
Click OK to close the message box on the server-side machine.