detecting close button of browser in C#

Hi all,

i am developing an application in which i want to perform certain action when user clicks on the close(X ) button of the web browser. Please anyone help me out to resolve this issue.Any tutorials or sample application would also be helpful.

Regards.



Answer this question

detecting close button of browser in C#

  • Foxer

    Hi farshad,

    Thanks for reply.But can u suggest me how could i check whether the user have clicked on the close button of the browser or not.Like in my application i want to check whether the user has clicked the close button and then want to update the database accordingly.


  • Ciero

    Hi

    If you are talking about web browser you can use javascript to track wether user has clicked close button or not. Below is the sample code and i think it will help you

    <script language='javascript'>
    function logoutMe(){
    if(window.event.clientX < 0 && window.event.clientY < 0){
    alert("Close Button clicked");
    }
    }
    </script>

    <body onUnload="logoutMe()">

    </body>

    cheers

    Raj Kumar


  • shimshon

    hey farshad,

    this is my launch Browser function that I copied from my friend.

    public void LaunchBrowser()
    {
    Process browserProcess = new Process();
    browserProcess.StartInfo.UseShellExecute = true;
    browserProcess.StartInfo.FileName = loginStr;
    browserProcess.Disposed;
    browserProcess.Start();
    }

    I need a method to detect whether or not the browserProcess has been closed...any ideas

    thanks,

    tonyD


  • brent.xml

    If you start the browser --or any other external app; it's not a hard work. What you need is just waiting for the process to end(this occurs normally when the user clicks on the close button), and then do what you wish.

    --

    cheers

        farshad



  • Ben Hall &amp;#40;UK&amp;#41;

    Hi spshah

    To do it, you've declared a System.Diagnostics.Process object and set it's attributes, then started it using the Start() methd; yes So simply do one of these :

    myprocess.Exited += new EventHandler(myprocess_Exited);

    In this case, your app runs and whenever the process you've started, ends(simply when it is closed by the user, or even if you've terminated it yourself) this event is fired and so forth. or:

    myprocess.WaitForExit();

    This one makes your app wait for the started process to exit(and hence, your app will not respond til that time), and the lines after this line are executed then.

    It is by you to use either of them.

    hope this helps

    --

    cheers

    farshad



  • yanyee

    Hi tony

    Since you've declared the browserProcess inside the LaunchBrowser(), you only can set properties here, so if the workarounds that I've declared above are of your use, you must do all the job in LaunchBrowser() method; or you may declare the Process object outside the method to be able to work with it anywhere in your class. By either of these, you can declare

    browserProcess.Exited += new EventHandler(browserProcess_Exited);

    before browserProcess.Start();

    and then write the code to be executed whenever the process has ended like this:

    private void prc_Exited(object sender, EventArgs e)

    {

    // TODO: Declare any code to execute after the process has exited

    }

    HTH

    --

    cheers, farshad



  • detecting close button of browser in C#