3

In visual studio 2010, working with c#;

I open a browser with:

private IE browser;

private void Set_Browser()
{
    string splashUrl = "google.com";
    browser= new IE(splashUrl);
}

If a user(person) closes the browser by accident, then my application will not be able to work anymore.

QUESTIONS:

  1. So how do I check if a user closed the browser manually?
  2. Can I unable a user from closing the browser by adding the browser to my application GUI as a control? [using Windows Forms]
  3. -> How do I do that?

Last question related to this post How to use watin with WebBrowser control? (2 years old, but no decent answer too)

EDIT: The solution in give URL seems to work. Problem is that if I try to send the WebBrowser.ActivateX.. as an object to other class. Then my browser = new IE(..) returns null. It does work when I instantiate it in the form class though. Any solutions?

Community
  • 1
  • 1
dylanmensaert
  • 1,689
  • 5
  • 24
  • 39
  • http://www.codeproject.com/Articles/60179/Web-Browser-in-C – Saw Dec 30 '12 at 14:24
  • ty for the link Sawan, there are mentioned some interesting features. However, noting related to WatiN is mentioned, as well as a way to implement it as a windows controller. – dylanmensaert Dec 30 '12 at 14:43

2 Answers2

3

You can search for the process of internet explorer every x seconds and see if the browser is already running using this code:

    bool isRunning = false;
    foreach (Process clsProcess in Process.GetProcesses()) {
            if (clsProcess.ProcessName.Contains("iexplore"))
            {
                    isRunning = true;
                    break;
            }
    }

You can use this article

Or you can add a browser control to your application using this article

Saw
  • 6,199
  • 11
  • 53
  • 104
  • Does this also cover the case where just the relevant tab is closed and not the browser itself ? – ashutosh raina Dec 30 '12 at 14:34
  • Hmmm, it is not, required a further research :) – Saw Dec 30 '12 at 14:35
  • Ty for reply, but this is not sufficient enough. If I close the browser instantiated by WatiN and open a new IE myself manually, the program will crash. Because it cannot find the particular tab, even if there is an instance of IE running. – dylanmensaert Dec 30 '12 at 14:39
  • Regarding the articles, you can add an instance of the Webbrowser class as a control to your GUI. But I don't know (and not mentioned) how to add a IE (webbrowser) instace of WatiN to my control – dylanmensaert Dec 30 '12 at 14:46
0

One thing you can do is hide the browser to avoid users closing it .. See this SO question.

Hiding Internet Explorer when WatiN is run

Community
  • 1
  • 1
Amitd
  • 4,769
  • 8
  • 56
  • 82