Handling Events in VB.Net

H

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top