Jpeh Noynay
Jpeh Noynay

Reputation: 173

Simulate a button click within the 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 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.

Upvotes: 8

Views: 65296

Answers (4)

Omidoo
Omidoo

Reputation: 513

Or in case of using VB in WPF:

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

Upvotes: 1

Steven Liekens
Steven Liekens

Reputation: 14088

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.

Upvotes: 4

Jay Riggs
Jay Riggs

Reputation: 53593

You can call the Button.PerformClick method:

btnSendSMS.PerformClick()

Upvotes: 22

jarchuleta
jarchuleta

Reputation: 1241

You can call the function directly.

btnSendSMS_Click(Me, EventArgs.Empty) 

Upvotes: 5

Related Questions