Solved

Listen for Events from another Form

Posted on 2004-08-10
10
414 Views
Last Modified: 2008-08-13
Hello

I have a Form 1 which is the main form (VB.Net 2003)

Form 1 opens another form (Form 2) with the following code:

        Dim frm As New frmBrowser
        frm.Show()

Now in Form 2 I've placed a Browser Control.

The question is: how can monitor the AxWebBrowser1.DocumentComplete Event (that happens in Form 2) from Form 1?

Eg. when the Webpage in Form 2 has completed loading some code in Form 1 has to be executed.

Thanks for answers...
0
Comment
Question by:netslt
[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
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 27

Expert Comment

by:planocz
ID: 11761918
Why are you forcing yourself to using two forms?
You can run and hide the axwebbrowser on the same form.
0
 

Author Comment

by:netslt
ID: 11762026
Form 1 should take a screenshot of Form 2 when the Website has completed loading (Fullscreen in Form 2), so Form 1 has to know when the Site in Form 2 has completed.

So I guess it is obvious that I cant use the Browser Control in Form 1.

But the question is also of general nature, I'm interested to know if and how it is possible to handle Events from Form 2 in Form 1.
0
 
LVL 27

Expert Comment

by:planocz
ID: 11762110
But you can. I have use it by hiding the browser behind the picturebox and then read the browser code and add
the info to my form1 screen.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 27

Expert Comment

by:planocz
ID: 11762299
Sample of two textboxes...

Form1:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xx As New Form2
        xx.Owner = Me
        xx.Show()
        'xx.ShowDialog(Me)
   End Sub

and then on Form2:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CType(Me.Owner, Form1).TextBox1.Text = "test"
        Me.Close()
    End Sub
0
 
LVL 27

Expert Comment

by:planocz
ID: 11762349
0
 

Author Comment

by:netslt
ID: 11762443
Sorry, the question is not about copying content from one form to another.

I wonder wether it is possible to listen to an event that is happening in another form.

So Form 1 reacts to the DocumentComplete Event that happens in Form 2.

Is that possible?
0
 
LVL 14

Accepted Solution

by:
ptakja earned 75 total points
ID: 11762625
What you can do is handle the event in form2 and then raise a new custom event that form1 will handle.

In Form2:

Public Event BrowserDone()


In Form1, make sure you declare form2 with events as:

Dim WithEvents Frm2 As Form2  << Needs class-level scope

Then you instantiate the form as usual in your button click routine

frm2 = New Form2
frm2.Show()    ' NOTE ShowDialog WON'T work here since form processing is halted until the child form closes!


Now in Form2 you handle the browsercomplete event and raise your custom event:

Private Sub AxAxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As EventArgs) Handles AxWebBrowser1.DocumentComplete
    RaiseEvent BrowserDone()    -  This will tell form1 that the browser doc is done loading
End Sub


Back to Form1  add an event handler for the new form2 event:

Private Sub Frm2_BrowserDone() Handles Frm2.BrowserDone)

      'You code goes here to capture the screenshot or whatever.

End Sub
0
 
LVL 8

Expert Comment

by:wguerram
ID: 11763908
Add this code to form2

'I am not sure of wich parameters this event receives

Public event DocumentComplete ()

Private Sub AxWebBrowser1.DocumentComplete() Handles AxWebBrowser1.DocumentComplete

     RaiseEvent DocumentComplete()

end sub
--------------------
Add this code to form 1

dim f as new form2

Private Sub DocumentComplete() Handles f.DocumentComplete
    'Write you code here
end sub
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 50 total points
ID: 11764072
ptakja's method is the proper way of doing it but you don't acutally have to create a custom event to achieve what you are looking for.

You can simply declare a variable as WithEvents of the type of control that you wish to trap events for.  Then after instantiating Form2, set the local variable on Form1 to the control on Form2.  The example below uses a button on Form2:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private WithEvents btn As Button

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f2 As New Form2
        btn = f2.Button1
        f2.Show()
    End Sub

    Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
        Static clicked As Integer
        clicked = clicked + 1
        Label1.Text = "Form2 Button clicks: " & clicked
    End Sub
End Class

The disadvantage to using WithEvents is that the variable can only reference one control at a time, so if you had many Form2's opened at once, you would only receive events from the button on the last Form2 opened.  To receive events from multiple Form2's at the same time you could use the AddHandler() function like this:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f As New Form2
        AddHandler f.Button1.Click, AddressOf btn_Click
        f.Show()
    End Sub

    Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Static clicked As Integer
        clicked = clicked + 1
        Label1.Text = "Form2 Button clicks: " & clicked
    End Sub
End Class

Regards,

Idle_Mind
0
 
LVL 8

Expert Comment

by:wguerram
ID: 11764180
It looks like i post the same code as ptakja

I really did not check the replies, i just wrote code to answer netslt question.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

632 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