VB.NET Event Handling between 2 forms.

How can i handle an event within Two forms in VB .NET. Particularly event being raised from the Global module and being handled by a Form. I've successfully handled an event within the same form. but failed while handling the same in a different form.

Global Module code -
-----------------------------
Public WithEvents url_event_handler As _Events

Public Class _Events
        Public Event Url_Event()
        Public Sub raise_url_event()
            RaiseEvent Url_Event()
        End Sub
End Class


Public Class LibWrap
   Public Shared Function callback(ByRef ap As NYAPPEVENT) As Integer
        If (ap.type = BROWSER) Then
                        Dim url_event_handler As New _Events()
                        url_name = "goole.com"
                        url_event_handler.raise_url_event()
                End Select
        End If
   End Function
End Class
---------------------------------------------

Form's Caode ---

Friend WithEvents url_event_handler As _Events
''(Why is the public declaration of the same in global module is not reflected on this form. I've to declare this as Friend here.)

Public Sub url_event_handler_Url_Event() Handles url_event_handler.Url_Event
        MsgBox(url_name)
End Sub

-----------------------------------------------

This piece of code compiles and the control just reaches upto the mark of Raising the event, and doesn't get forwarded to the form. Is this thing possible.

Regards

Rahul



rahul_miloAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Arthur_WoodCommented:
Rahul, is you _Events class declared in a MODULE (using Project/Add Module) or in a Class Moduel (using Project/Add Class)?

If it is a MODULE, then you CANNOT created true classes, or Objects from those classes, in a MODULE.  You must use a Class Module, instead.  The two entities are DIFFERENT in VB.NET.

AW
rahul_miloAuthor Commented:
This seems to be a pretty correct one. not 100%, as the code is not running. I've tested it with two forms. There is no error, but when event is raised nothing happens.  

---------Form1 Code ---------------------

Public Delegate Sub RemoteEventHandler(ByVal SomeString As String)

Public Class RemoteClass

    Public Event RemoteEvent As RemoteEventHandler

    Public Sub DoSomething()
        RaiseEvent RemoteEvent("My event")
    End Sub

End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objRemote As RemoteClass
        objRemote = New RemoteClass()
        objRemote.DoSomething()
End Sub

--------------Form2  code --------------
Private WithEvents objRemote As RemoteClass

Public Sub objRemote_RemoteEvent(ByVal Data As String) Handles objRemote.RemoteEvent
        MessageBox.Show(Data)
End Sub
-------------------------------------

Help Needed.

Regards

Rahul

JarodtweissCommented:
Coorect like that :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        objRemote = New RemoteClass()
        objRemote.DoSomething()
End Sub
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

JarodtweissCommented:
I explain :

In your handler
  Public Sub objRemote_RemoteEvent(ByVal Data As String) Handles objRemote.RemoteEvent
        MessageBox.Show(Data)
  End Sub

You were handling the event raised by your objRemote which is declared here :
  Private WithEvents objRemote As RemoteClass

This object is never initialized and so never raise any event.
With the correction, you instantiate a new object on the click event, so everything is ok.

Notice that it is not logical to handle an event raised by an object of form1 in form2. You should better do :
- ObjRemote is raising an event
- That event is handled in Form1
- Form1 raise a custom event
- Form2 (that would have a reference to Form1) handles the event

Also notice that every event you use should follow the .NET convention :
  Event myEvent As myEventHandler
with
  Delegate sub myEventHandler(sender as object, e as myEventArgs)
with
  myEventArgs a class inheriting from System.EventArgs
So in your acse :

public class RemoteEventArgs
  inherits System.EventArgs

  public RemoteString as string

  'Some logic, constructor and encapsulation omitted !!!
end class


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rahul_miloAuthor Commented:
Jarodtweiss,

I removed the dim st. from the code as -

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        objRemote = New RemoteClass()
        objRemote.DoSomething()
End Sub

but there comes a compiler error. saying  objRemote is not declared.

Also i've to code in a way that my global module raises an event and that a form handles it.
I've tried stuff like - (Slightly different from the first one.)

Public Class LibWrap
   Public Event Url_data()                           ------------Event
   Public Shared Function callback(ByRef ap As NYAPPEVENT) As Integer
        If (ap.type = BROWSER) Then
                        url_name = "goole.com"
                        RaiseEvent Url_data()          -----------Raising Event
        End If
   End Function
End Class

This code gives an error, bcoz of Shared function. I've to have the callback as shared. this is giving a compile time error.

The Form simply handles the event.


siobiCommented:
just to clearify, I'm not aim for Jarodtweiss points but just to bring up some issue...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dim objRemote as  New RemoteClass() 'must declare...
        objRemote.DoSomething()
End Sub


--------------Form2  code --------------
Private WithEvents objRemote As RemoteClass

Public Sub objRemote_RemoteEvent(ByVal Data As String) Handles objRemote.RemoteEvent
        MessageBox.Show(Data)
End Sub
-------------------------------------
the form 2 is out!!!

in OOP form2 is another class and u had not well declare ur event and ur form2 didnt create the runtime object or inanother words, u didnt throw out any event object in form2 tobe capture!!!

so compile no error but no event object created during runtime and there is no way u can catch the event and for sure there is no result gonna happend then...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.