Question: I’m using SHDocVw.InternetExplorer APIs in my Vb.Net WinForms application to capture requests sent from IE in my application.

As given on [this](http://msdn.microsoft.com/en-us/library/aa768337%28v=vs.85%29.aspx) link

> 
> "The NewWindow3 event is only fired when a new instance of Internet
>  Explorer is about to be created. Calling showModalDialog or
>  showModelessDialog does not trigger an event because they are not new
>  instances of Internet Explorer"
> 
> 
> 

I am trying to automate user actions in Internet Explorer for automated testing. So I want to know that due to some user action a modal dialog is opened or some alert window is opened.

So my question is:

* How do I know that a modal dialog window is opened in Internet Explorer?
* Is there any other event raised by Internet Explorer in this case?
* If it's not possible directly then is there some other way to handle this scenario?

Answer: 
The only thing I was able to find is the WindowStateChanged event <http://msdn.microsoft.com/en-us/library/aa768307(v=vs.85).aspx> which mentions: 

> 
> The WindowStateChanged event is raised when the state of a content
>  window, such as the browser window or a tab, might have changed. The
>  following actions raise this event.
> 
> 
> * The browser window is minimized or restored.
> * An active tab becomes inactive.
> * An inactive tab becomes active.
> * **The browser window is enabled or disabled due to a modal dialog box.**
> 
> 
> 

Here is a VB.NET code example of how to use the WindowStateChanged event to check the enabled/disabled and visible flags:

```
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Code to instantiate the IE COM object'
        Dim IE As New SHDocVw.InternetExplorer()
        AddHandler IE.WindowStateChanged, AddressOf OnWindowStateChanged
        'do stuff here'
        IE.Quit()
    End Sub

    Public Sub OnWindowStateChanged(ByVal dwWindowStateFlags As UInteger, ByVal dwValidFlagsMask As UInteger)
        Const OLECMDIDF_WINDOWSTATE_USERVISIBLE As UInteger = 1
        Const OLECMDIDF_WINDOWSTATE_ENABLED As UInteger = 2

        'Check if the window is disabled'
        If dwValidFlagsMask And OLECMDIDF_WINDOWSTATE_ENABLED = OLECMDIDF_WINDOWSTATE_ENABLED Then
            If dwWindowStateFlags And OLECMDIDF_WINDOWSTATE_ENABLED <> OLECMDIDF_WINDOWSTATE_ENABLED Then
                'The browser window has been disabled.... possibly by a modal popup'
            Else
                'The browser is enabled.'
            End If
        Else
            'The enabled/disabled flag has not been initialized, so we cannot check the state during this event handler execution'
        End If

        'Check if window is visible: '
        If dwValidFlagsMask And OLECMDIDF_WINDOWSTATE_USERVISIBLE = OLECMDIDF_WINDOWSTATE_USERVISIBLE Then
            If dwWindowStateFlags And OLECMDIDF_WINDOWSTATE_USERVISIBLE <> OLECMDIDF_WINDOWSTATE_USERVISIBLE Then
                'The browser window is NOT visible'
            Else
                'The browser window is visible'
            End If
        Else
            'The visible flag has not been initialized, so we cannot check the state during this event handler execution'
        End If

    End Sub
End Class

```
The event you're looking for probably doesn't exist, however there's a number of workarounds for detecting if a modal is open. 

You can use jquery to add/ remove classes on the modal: [Can I check if Bootstrap Modal Shown / Hidden?](https://stackoverflow.com/questions/19674701/can-i-check-if-bootstrap-modal-shown-hidden)

You can check the console: [check if jquery reveal modal is active, do something](https://stackoverflow.com/questions/19822597/check-if-jquery-reveal-modal-is-active-do-something)

I don't recommend the console approach, there's more to it like when the console's not open. 

I can think of several ways to solve your problem without caring about the event at all however, couldn't the modal talk to the IE page and then use the API to talk to your app? You can use show() & hide() to change the state of the modal and when that state changes fire additional code to do what you need to. 

Another option is to save values to a database and read them back in the app (doesn't require the API obviously). 

If none of these sound good, perhaps you should tell us exactly what you're trying to accomplish from a functional standpoint and we'll be able to architect / give you a solution.