Simulate a button click in an application (VB.NET)

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.

I would like to simulate a click on the button above in my other sub (SMS receiving detector) when receiving SMS.

How can I do it? I know that this is not the best way to do something, but only for the sake of learning. Thanks.

+8
source share
4 answers

You can call the Button.PerformClick method:

 btnSendSMS.PerformClick() 
+21
source share

You can call the function directly.

 btnSendSMS_Click(Me, EventArgs.Empty) 
+4
source share

Why don't you put the code in a separate method

 Private Sub SendSMS() ' do your thing End Sub 

and in your button event handler call this 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 doing something scared, for example, by mimicking button presses.

+4
source share

Or if using VB in WPF:

 Dim peer As New ButtonAutomationPeer(btnSendSMS) Dim invokeProv As IInvokeProvider = TryCast(peer.GetPattern(PatternInterface.Invoke), IInvokeProvider) invokeProv.Invoke() 
+1
source share

All Articles