vba Visual Basic - 如何强制事件触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4500335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:01:46  来源:igfitidea点击:

Visual Basic - how to force event to fire

vbaevent-handling

提问by twpc

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.

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

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

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!

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

回答by RolandTumble

AFAIK, there is noway to "fire an event manually" in VB(A). What you cando is call the event handlermanually, and for this, rdkleine has given you the answer already:

AFAIK,没有办法在 VB(A) 中“手动触发事件”。您可以做的是手动调用事件处理程序,为此,rdkleine 已经给了您答案:

Call txtInvDate_AfterUpdate()

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

这将有完全一样的效果,如果事件发射了(尽管它不会给你事件的整个链条也可以与它一起开火-除非你Call自己的处理程序以及)。

IgorM has another valid point, in comments on his answer--it's "cleaner" to write a differentSub 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:

IgorM 有另一个有效的观点,在对他的回答的评论中——写一个不同的Sub 来完成你想做的工作是“更干净的” ,然后从事件处理程序和你现在尝试做的任何地方调用它(按钮点击?)。所以:

Private Sub txtInvDate_AfterUpdate()
    DoWhatever
End Sub

Private Sub button_Click()
    DoWhatever
End Sub

Private Sub DoWhatever
    'your desired effect
End Sub

You could even make DoWhatevera Public Sub in a Module.

你甚至可以DoWhatever在模块中创建一个公共子。

Edit

编辑

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

不,在 VB(A) 中,定义子(或函数)例程的顺序无关紧要。

回答by Ralf de Kleine

Call

称呼

txtInvDate_AfterUpdate()

txtInvDate_AfterUpdate()

回答by Igor

Try to use something like this:

尝试使用这样的东西:

Private Sub TextBox1_LostFocus()
  TextBox1.Value = "5"
End Sub

Use LostFocus event if you change value manually.

如果您手动更改值,请使用 LostFocus 事件。

Or you can use another way:

或者您可以使用另一种方式:

Private Sub CommandButton1_Click()
   TextBox1.Value = "new value"
End Sub

Private Sub TextBox1_Change()
   MsgBox TextBox1.Value
End Sub

Try to use Change event.

尝试使用 Change 事件。

回答by xstack

Some code apparently will (regretfully) force events to fire:

一些代码显然会(遗憾地)强制事件触发:

    Me.Dirty = False

forces BeforeUpdate and AfterUpdate to fire, as I understand it. There are probably more cases too. Any secret hooks which attach to your code in that manner reflect bad design in my view and will inevitably lead to inadvertant & frustrating loops being created.

据我了解,强制 BeforeUpdate 和 AfterUpdate 触发。可能还有更多的案例。在我看来,以这种方式附加到您的代码的任何秘密挂钩都反映了糟糕的设计,并且将不可避免地导致不经意和令人沮丧的循环被创建。

回答by roldy

Is there a way to keep a textboxes event firing until I click on another text box? I'm needing to stay "in" the text box which has Do While loop for a selection. Once the selection is made, I want the user to be able to replace the selection without having to click on the text box again. If the user is satisfied with the select, they can click on a different text box.

有没有办法让文本框事件触发,直到我点击另一个文本框?我需要留在“在”具有 Do While 循环的文本框中进行选择。一旦做出选择,我希望用户能够替换选择而无需再次单击文本框。如果用户对选择感到满意,他们可以单击不同的文本框。