WPF - C# - open parent window only once -


i´m programming simple thing in c# , wpf. have mainwindow button. if trigger button opens secon window:

private void btnf4_click(object sender, routedeventargs e) {      secondwindow second = new secondwindow();      second.show(); } 

naturally if trigger button 3 or 4 times, have 3 or 4 windows open. don´t want use showdialog(), want open second window once. mean if trigger button , window open, should nothing happen.

thank you!

make second instance variable parent window class , create new window if hasn't been created.

of course need make sure null instance variable when second window closed.

public class parentwindow ... {     private secondwindow m_secondwindow = null;      ....      private void btnf4_click(object sender, routedeventargs e)     {         if (m_secondwindow == null)         {             m_secondwindow = new secondwindow();             m_secondwindow.closed += secondwindowclosed;             m_secondwindow.show();         }     }       public void secondwindowclosed(object sender, system.eventargs e)     {         m_secondwindow = null;     } }     

this might shortened following:

public class parentwindow ... {     private secondwindow m_secondwindow = null;      ....      private void btnf4_click(object sender, routedeventargs e)     {         if (m_secondwindow == null)         {             m_secondwindow = new secondwindow();         }          m_secondwindow.show();                 } }     

however, i'm never sure whether can "reopen" window closed before. if need initialize window on upon reopening, use first code. if can live window starting showing previous content, use second.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -