PDA

Click to See Complete Forum and Search --> : problems creating events...


PT Exorcist
Jul 17th, 2002, 01:19 PM
i put this code in a class...


Public Event DownloadComplete(ByVal Status As Integer, ByVal ReturnValue As String, ByVal URL As String, ByVal FileName As String)

'~~~~~~~~~~~~~~~~~~

Sub DownloadPicture()
Dim MyWebClient As WebClient = New WebClient()

Try
MyWebClient.DownloadFile(URLName, LocalFileName)
Catch ex As Exception
RaiseEvent DownloadComplete(0, ex.Message, "", "")
Finally
RaiseEvent DownloadComplete(1, "Imagem carregada com sucesso no disco rigido!", "", "")
Msgbox("download complete!")
End Try
End Sub


and then in form i have


Private WithEvents Dev As New Deviations()

Private Sub Dev_DownloadComplete(ByVal Status As Integer, ByVal ReturnValue As String, ByVal URL As String, ByVal FileName As String) Handles Dev.DownloadComplete
MsgBox(Status)
End Sub


but the Dev_DownloadComplete is never fired although the msgbox saying "download complete" is fired!!! grrrrr

what am i missing..?

PT Exorcist
Jul 17th, 2002, 01:43 PM
sorry about posting twice this was really lagged and i clicked twice :o

hellswraith
Jul 17th, 2002, 09:12 PM
Try using this for the even declaration (I am not to sure on the use of the shared keyword, but I think this is what you need.


Public Shared Event DownloadComplete(ByVal Status As Integer, ByVal ReturnValue As String, ByVal URL As String, ByVal FileName As String)


The reason I think you need the Shared keyword in there is because you are trying to raise an event from an instance of a class which means that you haven't subscribed to that event. The other way to do it would probably be to use the AddHandler statement in the form load event.

I am sorry if I don't make much sense, I am getting pretty tired now. Tell me what happens, if it doesn't work I will spend more time figuring it out.

hellswraith
Jul 17th, 2002, 09:14 PM
Oh ya, if you are still having problems, post more code (like the whole class), or a zip file so I can just drop it in and start messing around to help you figure it out.

opps, adding this part:
I forgot to mention that you have the order wrong on your try/catch/finally block, check this out:


Try
MyWebClient.DownloadFile(URLName, LocalFileName)
Catch ex As Exception
RaiseEvent DownloadComplete(0, ex.Message, "", "")
Finally
'**This will always fire, even if the catch block is used. This
'**means that you will get two events fired if there is an error
'**One to show the error, the other to show a success even
'**though there was an error.
RaiseEvent DownloadComplete(1, "Imagem carregada com sucesso no disco rigido!", "", "")
Msgbox("download complete!")
End Try

'Change the above code to something like this:

Try
MyWebClient.DownloadFile(URLName, LocalFileName)
RaiseEvent DownloadComplete(1, "Imagem carregada com sucesso no disco rigido!", "", "")
Msgbox("download complete!")
Catch ex As Exception
RaiseEvent DownloadComplete(0, ex.Message, "", "")
End Try