Solved

VB.NET Event Handling between 2 forms.

Posted on 2004-04-26
8
1,093 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now