Reputation: 5349
I have a scenario in which I have to implement a UserControl , On Clicking a button "Process" a background worker works.
I want to trigger a Function/Method of my parent form where I am going to add that UserControl upon "RunWorkerCompleted" Event.
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
Try
bgwFetchGiftCard.RunWorkerAsync()
Catch ex As Exception
WriteLog(ex)
End Try
End Sub
Private Sub bgwFetchCard_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwFetchCard.DoWork
Try
ds = New DataSet()
'Call ApI Service for Denomination
ds = APICALL()
Catch ex As Exception
WriteLog(ex)
End Try
End Sub
Private Sub bgwFetchCard_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwFetchCard.RunWorkerCompleted
If ds IsNot Nothing Then
Result = True
'At This Point I Want To Raise Event
'RaiseEvent
Else
'ShowMessage("FAIL")
End If
End Sub
How may i achieve this?anybody kindly suggest
Upvotes: 3
Views: 3279
Reputation: 38875
Assuming the code shown is in the UserControl, you just need to declare an event and raise it (there is nothing special to this regarding a UserControl):
Public Event SomethingIsDone (sender As Object, e as EventArgs)
...
Private Sub bgwFetchCard_RunWorkerCompleted(sender As Object,
e As RunWorkerCompletedEventArgs) Handles bgwFetchCard.RunWorkerCompleted
If ds IsNot Nothing Then
Result = True
RaiseEvent SomethingIsDone(Me, New EventsArgs())
Else
'ShowMessage("FAIL")
End If
End Sub
In cases where you need to pass information in the event, define a custom EventArgs
class which inherits from EventArgs
add the properties you need:
Public Class SomethingDoneEventArgs
Inherits EventArgs
Public Property SomethingName As String
' convenience:
Public Sub New(name As String)
SomethingName = name
End Sub
End Class
Then pass an instance of that class rather than the the generic EventArgs
:
...
RaiseEvent SomethingIsDone(Me,
New SomethingDoneEventArgs("foo"))
Upvotes: 3