模拟应用程序中的按钮单击 (VB.NET)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10423079/
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
Simulate a button click within the application (VB.NET)
提问by Jpeh Noynay
Good day,
再会,
In my application, I have Private Sub btnSendSMS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendMessage.Click
in my program.
在我的应用程序中,我有Private Sub btnSendSMS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendMessage.Click
我的程序。
I would like to simulate a button click of the above within my other sub (an SMS receive detector) when an SMS is received.
当收到短信时,我想在我的另一个子(短信接收检测器)中模拟上面的按钮点击。
How do I do this? I know this is not the best way of doing things but just for the sake of learning. Thanks.
我该怎么做呢?我知道这不是最好的做事方式,而只是为了学习。谢谢。
回答by Jay Riggs
回答by jarchuleta
You can call the function directly.
您可以直接调用该函数。
btnSendSMS_Click(Me, EventArgs.Empty)
回答by Steven Liekens
Why don't you put the code in a seperate method
你为什么不把代码放在一个单独的方法中
Private Sub SendSMS()
' do your thing
End Sub
and in your button event handler, call that method.
并在您的按钮事件处理程序中,调用该方法。
Private Sub btnSendSMS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendMessage.Click
Call SendSMS()
End Sub
Now you can call SendSMS()
anywhere in your class without having to do something funky like simulating button clicks.
现在,您可以SendSMS()
在班级中的任何地方进行呼叫,而无需执行诸如模拟按钮点击之类的时髦操作。
回答by Omidoo
Or in case of using VB in WPF:
或者在 WPF 中使用 VB 的情况下:
Dim peer As New ButtonAutomationPeer(btnSendSMS)
Dim invokeProv As IInvokeProvider = TryCast(peer.GetPattern(PatternInterface.Invoke), IInvokeProvider)
invokeProv.Invoke()