Home All Groups Group Topic Archive Search About

Handling Events in VB.Net

Author
10 Jan 2006 11:03 PM
Harry Strybos
Hi All

I have a project called WFL which contains a calss called Company. I have a
second project called UI which contains a form called frmCompany. I have set
a reference at UI to WFL.Company.

In WFL.Company I have:    'skeleton code

Public Class Company

    Public Event Refresh

    Public Sub Fetch

        'do some stuff
        RaiseEvent Refresh

    End Sub

End Class

In UI.frmCompany I have:

Public Class frmCompany 'skeleton code

    Private Withevents oCompany As New WFL.Company

     Private Sub frmCompany_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
          oCompany.Fetch

     End Sub

    Private Sub oCompany_Refresh() Handles oCompany.Refresh

        Msgbox "I got an event"

        End Try

    End Sub

End Class

This is essentially how it was done in VB6. The above, however, does not
work. The event is not fired. No errors occur (option strict On)

What am I doing wrong here?

Thanks for any help

Author
11 Jan 2006 1:03 AM
AMercer
> This is essentially how it was done in VB6. The above, however, does not
> work. The event is not fired. No errors occur (option strict On)
> What am I doing wrong here?

In a test program of mine, I put:

    Dim z As New frmCompany
    z.Test()

I added the code below which is almost yours, and it worked fine.

Public Class Company
  Public Event Refresh()
  Public Sub Fetch()
    RaiseEvent Refresh()
  End Sub
End Class

Public Class frmCompany
  Private WithEvents oCompany As New Company
  Public Sub Test()
    oCompany.Fetch()
  End Sub
  Private Sub oCompany_Refresh() Handles oCompany.Refresh
    MsgBox("I got an event")
  End Sub
End Class

So, you have your objects and events essentially correct.  Put some
breakpoints at places in your code to see what if anything is actually being
executed.