Solved

VB.NET Event Handling between 2 forms.

Posted on 2004-04-26
8
1,165 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
[X]
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
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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

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

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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