Solved

Listen for Events from another Form

Posted on 2004-08-10
10
411 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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