Visual Basic-如何强制触发事件 [英] Visual Basic - how to force event to fire

查看:137
本文介绍了Visual Basic-如何强制触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VB的新手(实际上是使用VBA),我想强制事件触发.具体来说,我正在更新文本框中的值,我想调用该文本框的AfterUpdate()事件.

I'm new to VB (using VBA, actually) and I would like to force an event to fire. Specifically, I am updating the value in a textbox and I would like to call that textbox's AfterUpdate() event.

Private Sub cmdCreateInvoice_Click()
  Me.txtInvDate = '11/01/10'
  Me.txtInvDate.AfterUpdate
End Sub

在运行时,出现一个错误,提示编译错误:属性的无效使用".如果尝试类似的操作,但是单击事件(例如:cmdCreateInvoice.Click)没有共享名称的属性,则会收到错误消息,提示编译错误:未找到方法或数据成员".
我知道必须有一种方法可以触发另一个事件.但是正确的语法是什么? 谢谢!

During run time, I get an error that says "Compile Error: Invalid Use of Property". If I try something similar, but with the click event (ex: cmdCreateInvoice.Click), which does NOT have a property that shares the name, I get an error that says "Compile Error: Method or Data member not found".
I know there must be a way to fire one event from another. But what is the proper syntax? Thanks!

推荐答案

AFAIK,VB(A)中没有没有方式来手动触发事件".您可以 手动调用 event handler ,为此,rdkleine已经为您提供了答案:

AFAIK, there is no way to "fire an event manually" in VB(A). What you can do is call the event handler manually, and for this, rdkleine has given you the answer already:

Call txtInvDate_AfterUpdate()

这将具有与事件已触发完全相同的效果(尽管它不会不会为您提供可能也随之触发的整个事件链,除非您Call处理程序).

This will have exactly the same effect as if the event had fired (though it does not give you the whole chain of events that may also fire along with it--unless you Call their handlers as well).

IgorM还有一个有效的观点,在对他的回答的评论中-编写一个 different Sub做您想要完成的工作,然后从事件处理程序&无论您现在尝试在哪里进行操作(单击按钮?).所以:

IgorM has another valid point, in comments on his answer--it's "cleaner" to write a different Sub to do the work you want done, then call it from both the event handler & wherever you're trying to do it now (button click?). So:

Private Sub txtInvDate_AfterUpdate()
    DoWhatever
End Sub

Private Sub button_Click()
    DoWhatever
End Sub

Private Sub DoWhatever
    'your desired effect
End Sub

您甚至可以将DoWhatever设置为模块中的公共子.

You could even make DoWhatever a Public Sub in a Module.

修改

不,在VB(A)中,定义Sub(或Function)例程的顺序无关紧要.

And no, in VB(A) it doesn't matter what order you define your Sub (or Function) routines.

这篇关于Visual Basic-如何强制触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆