|
-
Jul 17th, 2002, 01:19 PM
#1
Thread Starter
yay gay
problems creating events...
i put this code in a class...
VB Code:
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
VB Code:
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..?
-
Jul 17th, 2002, 01:43 PM
#2
Thread Starter
yay gay
sorry about posting twice this was really lagged and i clicked twice
-
Jul 17th, 2002, 09:12 PM
#3
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.
VB Code:
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.
-
Jul 17th, 2002, 09:14 PM
#4
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:
VB Code:
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|