Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VB.NET Event Handling between 2 forms.

Posted on 2004-04-26
8
1,154 Views
Last Modified: 2012-08-13
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
Comment
Question by:rahul_milo
8 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 10918100
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
 

Author Comment

by:rahul_milo
ID: 10918593
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
 
LVL 4

Expert Comment

by:Jarodtweiss
ID: 10918954
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 4

Accepted Solution

by:
Jarodtweiss earned 50 total points
ID: 10919014
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
 

Author Comment

by:rahul_milo
ID: 10920577
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
 
LVL 1

Assisted Solution

by:siobi
siobi earned 50 total points
ID: 10924687
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question