Events/Custom Events

shawne

Well-known member
Joined
Feb 22, 2006
Messages
103
Location
Pennsylvania
Programming Experience
10+
Ok..I have a custom browser control which I am using in a windows application. I need to be able to catch the newwindow2 event of the AxWebbrowser control in order to keep popups running with-in my application, not IE. How do I let my windows form know when the AxWebbrowser newwindow2 event fired? Thanks for any help!
 
In code view, select first the object in the object combobox, then select the event in the events combobox. See screenshot. The event handler method is then inserted in code.
 

Attachments

  • events.jpg
    events.jpg
    9.5 KB · Views: 94
Ya, but the problem is, my app. does not see the underlying AxWebBrowser control. It sees the "MyCustomBrowser" control which houses the Axbrowser. The custombrowser does not have the NewWindow2 event because it is a custom control. How do I either create my own event to handle this event or pass the event through to my app.?
 
Alright, creating an event is easy, for instance "Public Event myEvent", and you raise it also very easy "RaiseEvent myEvent". When do I raise this event you ask? You raise it when your embedded browser raised its NewWindow2 event.

But you want to be a little clever by adding arguments to the event like what instance of your usercontrol class that raised it and other arguments. Basically its best if you follow the .Net framework convention with a sender parameter and an e parameter for the compounds of all other arguments. Since what you are talking about here is a cancelable event it is natural for your e parameter to specify a class instance that inherits from CancelEventArgs class found in System.ComponentModel namespace.

The thing about cancel is that those who subscribe to this event can set the cancel property when they receive the event to stop the event action, so after you have raised the event in usercontrol you must have a look at the passed event arguments to see if the catcher did cancel. In this special case an eventual cancel must be passed along to the embedded webbrowser NewWindow2 events cancel parameter. Its all a long series of events, nothing happens at the same time in a computer.

The code below should do it, your usercontrol would then expose the myNewWindow2 event:
VB.NET:
Public Event myNewWindow2(ByVal sender As Object, ByVal e As myBrowserEventArgs)
 
Private Sub AxWebBrowser1_NewWindow2(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_NewWindow2Event) _
Handles AxWebBrowser1.NewWindow2
  Dim x As New myBrowserEventArgs(e.cancel, e.ppDisp)
  RaiseEvent myNewWindow2(Me, x)
  e.cancel = x.Cancel
[LEFT][SIZE=2]  e.ppDisp = x.ppDisp[/SIZE]
End Sub[/LEFT]
 
 
Public Class myBrowserEventArgs
Inherits System.ComponentModel.CancelEventArgs
 
Private _ppDisp As Object
 
Public Property ppDisp() As Object
Get
  Return _ppDisp
End Get
Set(ByVal Value As Object)
  _ppDisp = Value
End Set
End Property
 
Public Sub New(ByRef cancel As Boolean, ByRef ppDisp As Object)
  Me.Cancel = cancel
  Me._ppDisp = ppDisp
End Sub
 
End Class
 
Last edited:
Super! Thats exactly what I figured, I just didn't know how to go about putting it all together. I'll give your snippet a shot and let you know, but i'm sure you got me right where I need to go.
 
Ok, It's been a short bit since I worked on this project, but here is what I got right now. I'm getting "RegisterasBrowser" and "Application" is not a member of VbCustomWebbrowser.VbCustomeWebbrowser.Mybrowser

Here is my sub which handels the mynewwindow event:

Private Sub MyWebBrowser1_myNewWindow2(ByVal sender As Object, ByVal e As VbCustomWebBrowser.myBrowserEventArgs)

Dim frmWB As Form2

frmWB = New Form2
frmWB.MyWebBrowser1.RegisterAsBrowser = True'----Problem line
e.ppDisp = frmWB.MyWebBrowser1.Application '----Problem line

poptrack = poptrack + 1

frmWB.Location = New System.Drawing.Point(f2left + (10 * poptrack), f2top + (10 * poptrack))
frmWB.Size = New System.Drawing.Size(f2width, f2height)
frmWB.Visible = True

End Sub
I'm guessing my custom control is missing something from the AxWebBrowser.
 
They are properties of AxWebBrowser, so you have to relay these as similar properties of your custom webbrowser control too.
 
Ok, I got the properties sorted, but i'm still not catching the "NewWindow2" event properly. The myNewWindow2 custom event spawns a popup of my browser, but instead of opening the site on the browser in that window, it still trys to launch it into IE.
 
I finally got to try this myself with a axWebbrowser inside a usercontrol and figured out what was missing. You see the NewWindow2 handler in my example where cancel is passed back to the 'internal' event (e.cancel = x.Cancel)? You must add a line to also pass the ppDisp back to the event:
VB.NET:
[SIZE=2]e.ppDisp = x.ppDisp[/SIZE]
I forgot to add this since it was Cancel you asked about first, it's easy to get the mind set on one problem alone..
 
Dude! your worth a million bucks! I should've seen that myself, thanks for pointing it out though, that was it. My app. works great now! Kudos!
 
Great! I also tested the same with multiple tabs of TabControl instead of multiple windows/mdi and noticed that the new tab and browser control had to be visible before performing the registerasbrowser/ppDisp, else IE popped.. Just a tip if you want to experiment with different types of multi-browser UI. :)
 
Actually the browser I wrote is a tabbed browser which creates the tabs based on the users security group memberships via an LDAP query. We've been moving more and more to thin client, web-based applications. So instead of having all these IE shortcuts (which I do not want the users to use anyway) I created this custom browser which prevents them from navigating outside of the application. And also keeps them out of some other settings which could generate work orders. Also, because it's based in AD, rolling out new webapplications is as simple as creating a new security group and adding users to it. I had thought of storing users and information in an SQL db, but I think this is just as easy and effective for now.

I had already rolled out one version of it, but it did not support autocomplete. It wasn't a big deal, but I really wanted to get this functionality added in for the users and I was really interested in the knowledge itself. After a TON of digging on the internet and picking everone and there brothers brains I finally got autocomplete working, but then the popups stopped working. Thats where you came in.

Thanks again for all your help! /bow :D
 
Back
Top