Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Listen for Events from another Form

Posted on 2004-08-10
10
Medium Priority
?
429 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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 225 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 150 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

926 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