Vb6.0 如何将函数/子过程作为事件处理程序绑定到外部事件?

发布于 2024-08-09 17:27:37 字数 1056 浏览 10 评论 0原文

我需要构建一个 VB6.0 ocx,它将用作某些外部 VB6.0 应用程序的插件。

此 ocx 包含几个子过程,这些子过程应该用作某些外部事件(ocx 外部)的事件处理程序。

问题是,使用这个 ocx 的用户只会调用我的 ocx 子程序一次且仅一次。问题是,如何在一次初始化时将所有子过程/函数绑定到各自的外部事件,以便在事件触发时调用我的程序?

我设法在外部应用程序本身中执行此操作,但当我将这些代码移植到 OCX 并将其用作外部 vb 程序中的插件时,

可以说这是外部 vb 应用程序中的原始事件处理程序:

   Private Sub someExternalControl1_someEvent(someParameter as boolean)
         MsgBox ("The original event handler")
   End Sub

并且在该 vb 应用程序中,如果我这样做:

 dim withevents aaa as someExternalControl
 set aaa = someExternalControl1

然后,每次事件触发时,都会调用此自定义事件处理程序以及如上所述的原始事件处理程序,

Private Sub aaa_someEvent(someParameter as boolean)
         MsgBox ("The custom event handler")
   End Sub

但当我将其放入 OCX 中时,我无法执行相同的操作。因为我做不到:

Public WithEvents ocxMyPlugin As VBControlExtender
...

Set ocxMyPlugin = Controls.Add("myprogID.usercontrolname", "somename", Me)
Set ocxMyPlugin.object.someExternalControl2 = someExternalControl   ' this will raise error

I need to construct a VB6.0 ocx that will be used as a plugin for some external VB6.0 applications

This ocx contains several sub procedures that are supposed to work as event-handlers for some of the external events (External to ocx).

The problem is, the users who use this ocx will only call one of my ocx sub procedures once and only once. Question is , how do i bind all my sub-proc/functions to their respective external events upon this one time initialization so that my procedures will get called when their events fire?

I managed to do this within the external app itself, but not when i ported these codes to OCX and use it as plugin in an external vb program

Let say this is the original event handler in the external vb app:

   Private Sub someExternalControl1_someEvent(someParameter as boolean)
         MsgBox ("The original event handler")
   End Sub

and in that vb app if i do:

 dim withevents aaa as someExternalControl
 set aaa = someExternalControl1

then this custom event handler will also be called everytime the event fires along with the original event handler as stated above

Private Sub aaa_someEvent(someParameter as boolean)
         MsgBox ("The custom event handler")
   End Sub

but i couldn't make the same when i put this in OCX. Because i couldn't do:

Public WithEvents ocxMyPlugin As VBControlExtender
...

Set ocxMyPlugin = Controls.Add("myprogID.usercontrolname", "somename", Me)
Set ocxMyPlugin.object.someExternalControl2 = someExternalControl   ' this will raise error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心在旅行 2024-08-16 17:27:38

虽然我从未处理过这种情况,但我记得有关动态添加控件到表单时使用的 VBControlExtender 对象的一些信息。

四处寻找,我发现了这个 文章。然后是关于 VBControlExtender 的 MSDN 文档。

特别是您想查看 ObjectEvent。

了解没有像在 .NET 中那样动态地将方法分配给事件的好方法。 VB6 通过使用 WithEvents 关键字确定变量的大小来处理事件。

然而它是一个变量。因此,虽然您无法更改方法,但您也可以更改变量指向的对象。

如果

Dim WithEvents X as SomeControl
Dim Y as New SomeControl
Dim Z as New SomeControl

Private Sub X_MyEvent(ByVal MyParm as Variant)

'Do Something like display the control name

End if

Public Sub TestY
   Set X = Y
End Sub

Public Sub TestZ
   Set X = Z
End Sub

您在 TestY 之后激活事件,则 X_MyEvent 将处理控件 Y 的事件,如果您在 TestZ 之后激活事件,则 X_MyEvent 将处理控件 Z 的事件。

使用 VBControlExtender,您可以一般处理不同的控件。如果您实例化同一类型的多个控件,那么您需要做很多工作。您不能将 withevent 与数组一起使用。在这种情况下,我将创建一个包含事件的类来帮助我处理同一类型的多个控件。

While I never handled this situation before I remembered something about a VBControlExtender object that is used when you dynamically add controls to a form.

Poking around I found this article. Then this MSDN documention on VBControlExtender.

In particular you want to look at ObjectEvent.

Understand there is no good way to dynamically assign methods to events like in .NET. VB6 handles events by dimensioning a variable with the WithEvents Keyword.

However it is a variable. So while you can't change the method you can change the object the variable points too.

If you have

Dim WithEvents X as SomeControl
Dim Y as New SomeControl
Dim Z as New SomeControl

Private Sub X_MyEvent(ByVal MyParm as Variant)

'Do Something like display the control name

End if

Public Sub TestY
   Set X = Y
End Sub

Public Sub TestZ
   Set X = Z
End Sub

If you activate the event after TestY then X_MyEvent will be handling the events for control Y, If you active the event after TestZ then the X_MyEvent will be handling the events for control Z.

With VBControlExtender you can handle different controls generically. If you instantiating multiple controls of the same type then you have quite a bit of work to do. You can't use withevent with arrays. In that case I would create a class with events to help me handle multiple controls of the same type.

对不⑦ 2024-08-16 17:27:38

我可能需要更多细节来帮助。 ocxMyPlugin.object.someExternalControl2 的类型是什么。如果目标应用程序不知道它,您将遇到有关后期绑定的问题。但是,您可以在第三个(或第四个项目)上创建一个包装器或接口,这可能会解决您的问题。发布您的设计以及您想要实现的目标非常重要。可能还有其他方法可以替代。

I might need more details to help. What is the type of ocxMyPlugin.object.someExternalControl2. If its unknown to the target application, you will have problems about late binding. However, you may create a wrapper or an interface on a third (or fourth project) which may solve your problem. Its important to post your design and what exactly you are trying to achieve. May be there are other methods which can be used instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文