Solved

Listen for Events from another Form

Posted on 2004-08-10
10
353 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

11 Experts available now in Live!

Get 1:1 Help Now