Solved

Listen for Events from another Form

Posted on 2004-08-10
10
369 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

14 Experts available now in Live!

Get 1:1 Help Now