Results 1 to 4 of 4

Thread: problems creating events...

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    problems creating events...

    i put this code in a class...

    VB Code:
    1. Public Event DownloadComplete(ByVal Status As Integer, ByVal ReturnValue As String, ByVal URL As String, ByVal FileName As String)
    2.  
    3. '~~~~~~~~~~~~~~~~~~
    4.  
    5.     Sub DownloadPicture()
    6.         Dim MyWebClient As WebClient = New WebClient()
    7.  
    8.         Try
    9.             MyWebClient.DownloadFile(URLName, LocalFileName)
    10.         Catch ex As Exception
    11.             RaiseEvent DownloadComplete(0, ex.Message, "", "")
    12.         Finally
    13.             RaiseEvent DownloadComplete(1, "Imagem carregada com sucesso no disco rigido!", "", "")
    14.             Msgbox("download complete!")
    15.         End Try
    16.     End Sub

    and then in form i have

    VB Code:
    1. Private WithEvents Dev As New Deviations()
    2.  
    3.     Private Sub Dev_DownloadComplete(ByVal Status As Integer, ByVal ReturnValue As String, ByVal URL As String, ByVal FileName As String) Handles Dev.DownloadComplete
    4.         MsgBox(Status)
    5.     End Sub

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

    what am i missing..?

  2. #2

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    sorry about posting twice this was really lagged and i clicked twice

  3. #3
    hellswraith
    Guest
    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:
    1. 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.

  4. #4
    hellswraith
    Guest
    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:
    1. Try
    2.             MyWebClient.DownloadFile(URLName, LocalFileName)
    3.         Catch ex As Exception
    4.             RaiseEvent DownloadComplete(0, ex.Message, "", "")
    5.         Finally
    6.            '**This will always fire, even if the catch block is used.  This
    7.            '**means that you will get two events fired if there is an error
    8.            '**One to show the error, the other to show a success even
    9.            '**though there was an error.
    10.             RaiseEvent DownloadComplete(1, "Imagem carregada com sucesso no disco rigido!", "", "")
    11.             Msgbox("download complete!")
    12.         End Try
    13.  
    14. 'Change the above code to something like this:
    15.  
    16.         Try
    17.             MyWebClient.DownloadFile(URLName, LocalFileName)
    18.             RaiseEvent DownloadComplete(1, "Imagem carregada com sucesso no disco rigido!", "", "")
    19.             Msgbox("download complete!")
    20.         Catch ex As Exception
    21.             RaiseEvent DownloadComplete(0, ex.Message, "", "")
    22.         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
  •  



Click Here to Expand Forum to Full Width