c# - Re-ask dialog when false -


i'm trying create dialog user gets choose between buttons, problem i'm experiencing when user closes window (not choosing button using x on top right corner), application shows message after crashes. know doing wrong here?

mainwindow.xaml.cs

public partial class mainwindow : window {     string[,] suppliers = new string[3,2] {{"xxx", "xxx"}, {"yyy", "yyy"}, {"zzz" , "zzz"}};       public mainwindow()     {         initializecomponent();          buttonprompt buttonprompt = new buttonprompt(suppliers, "select supplier.");          while (buttonprompt.showdialog() != true)         {             messagebox.show("please choose 1 of suppliers!");         }      } } 

buttonprompt.xaml.cs:

public partial class buttonprompt : window {     public buttonprompt(string[,] buttons, string question)     {         initializecomponent();          buttonstack.children.clear();          textblock questionblock = new textblock();         questionblock.text = question;          buttonstack.children.add(questionblock);          (int = 0; < buttons.getlength(0); i++)         {             button inputbutton = new button();              inputbutton.name = buttons[i, 0];             inputbutton.content = buttons[i, 1];              inputbutton.width = 200;             inputbutton.height = 60;              inputbutton.click += inputbutton_click;              buttonstack.children.add(inputbutton);              if (i == 0)             {                 inputbutton.focus();             }         }       }      private void inputbutton_click(object sender, routedeventargs e)     {         button inputbutton = (button)sender;         this.dialogresult = true;     }      private void window_closed(object sender, eventargs e)     {         this.dialogresult = false;     } } 

thanks in advance!

the buttonprompt.showdialog() returns true when window closed. documentation says window_closed

once event raised, window cannot prevented closing.

this means cannot set dialogresult because it's true , while doesn't work.

you have 3 possibilities:

  1. override onclosing method in how override default window close operation? prevent window closed gui button.
  2. (my favourite) override onclosing event in http://msdn.microsoft.com/it-it/library/system.windows.window.closing.aspx checking own conditions , adding this.dialogresult = false
  3. hide close button of dialog window xaml setting windowstyle=none

update: on other side, put while check out of main window initialization, try loaded handler you're sure main component doesn't have troubles while coming up.


Comments