[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1332
  • Last Modified:

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



0
rahul_milo
Asked:
rahul_milo
2 Solutions
 
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
0
 
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

0
 
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
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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


0
 
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.


0
 
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...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now