c# - How to show messagebox one time only before closing window -


i have made standalone application engineering analyses research purpose. made show graph displaying results in window (i don't know right word it. let me call subwindow) linked main window. remind end users save input files before closing main window, added code behind notification shown below:

    private void window_closing(object sender, canceleventargs e)     {         messageboxresult result = messagebox.show("please sure input & output files saved.  want close program?", "confirmation", messageboxbutton.yesno, messageboximage.warning);         if (result == messageboxresult.yes)         {             application.current.shutdown();         }         else         {             e.cancel = true;         }     }  

xaml code is:

<window x:class="gmgen.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     icon="icon1.ico" title="gmgen" windowstate="maximized" closing="window_closing" > <dockpanel x:name="rootwindow">     <contentcontrol content="{binding currentpage}" />     <grid >     </grid > </dockpanel> 

when close main window without opening subwindows showing graphs, works fine. notification window pops , clicking "yes", program terminated. however, if open & close subwindows before closing main window, things go little weird. notification pops up. click yes, program not terminated. instead, notification pops up. click yes 1 pops up. thing happens same times opened , closed subwindows. i.e. if opened , closed subwindows 4 times, notification appears 4 5 times. don't know causes problem. want show messagebox 1 time. if know solution, please let me know. appreciate help.

most subscribing closing event on every window, firing n number of times.

without seeing actual implementation, hard best option resolve it. here 1 way can handle having static flag confirmation. once display confirmation, flag prevent subsequent popup.

private static bool _isconfirmed = false; private void window_closing(object sender, canceleventargs e) {      if (!_isconfirmed)     {         messageboxresult result = messagebox.show("please sure input & output files saved.  want close program?", "confirmation", messageboxbutton.yesno, messageboximage.warning);         if (result == messageboxresult.yes)         {             application.current.shutdown();         }         else         {             e.cancel = true;         }         _isconfirmed = true;     } } 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -