locked
Wait till other window is close RRS feed

  • Question

  • My game display a welcome window asking the number of players and there name is there is more than one player, is there a way to make the main window until a button is click in the welcome window. At the moment I have made a constructor for a main window which is different to one with no inputs which does work but is this the best way. 
    • Moved by CoolDadTx Monday, March 23, 2015 1:49 PM WPF related
    Monday, March 23, 2015 12:13 PM

Answers

  • When you open a form or a window by calling the ShowDialog() method, the call won't return until the window is closed (note that this is not necessarily the same thing as "stop any code running in your main window" though):

    WelcomeWindow win = new WelcomeWindow();
    win.ShowDialog(); //instead of Show();

    This effectively means that the user won't be able to interact with the other window until WelcomeWindow has been closed. The ShowDialog method works the same regardless of which constructor overload you are using to create an instance of the window or form.

    Hope that helps.

    Please remember to close your threads by marking all helpful posts as answer and please start a new thread if you have a new question.

     

    • Marked as answer by Dijkstra_ Monday, March 30, 2015 2:22 PM
    Monday, March 23, 2015 1:47 PM

All replies

  • Hi,

    i think you should use Modal Dialogs.

    With modal dialogs you can open one Form in "top most" of other forms and wait for result in your app.

    Have a look at this:  https://msdn.microsoft.com/en-us/library/c7ykbedk.aspx

    Best regards.

    Monday, March 23, 2015 12:19 PM
  • I agree with Cristian.

    Use Showdialog instead of Show is probably the simplest approach.

    It will stop any code running in your main window.

    If that is a bad thing, for whatever reason, you could use show but set the mainwindow or some part of it enabled=false. 

    You would then need to re enable the main window as you close your welcome window.

    Another option would be to show your welcome screen before the main window.

    Only open  the "main" window once you have captured your input.

    Then there would be no main window there for the user to input anything.

    That does rather depend on the flow of your application though.  Maybe you can input stuff in welcome, use main window then return to Welcome.


    Hope that helps.
    Recent Technet articles: Property List Editing; Dynamic XAML

    Monday, March 23, 2015 12:49 PM
  • You can set the welcome window as the main window of the application, later, when the user click to the button, use this code:

    PrincipalViewModel myPrincipalViewModel = new PrincipalViewModel();
    PrincipalView myPrincipalView = new PrincipalView();
    miPrincipalView.DataContext = miPrincipalViewModel;
    
    App.Current.MainWindow.DataContext = null;
    App.Current.MainWindow.Close();
    App.Current.MainWindow = myPrincipalView;
    myPrincipalView.Show();
    
    this.Dispose();

    This Works for MVVM pattern. The idea is to create the view model of the main window,  create the main window and then link the  view model with the view. Also, set as main window of the application the new view that is create.

    Monday, March 23, 2015 12:50 PM
  • Those two links give good information:

    General Info about Splash Screens:

    http://stackoverflow.com/questions/7955663/how-to-build-splash-screen-in-windows-forms-application

    and this second link gives you a time based splash screen, which shows a progress bar while game initializes in the background. Your code will be easier and no need to use Task to setup your game elements in the background but it is a useful video:

    https://www.youtube.com/watch?v=J9azEeLwymU

    Monday, March 23, 2015 1:27 PM
  • "You can set the welcome window as the main window of the application, later...."

    In wpf apps, by default when you close the mainwindow the application closes.

    Setting the welcome window as the main window is therefore a complication if you're going to close it.

    You can open the mainwindow without showing it initially.

    If you do:

    public partial class App : Application
    {
        void App_Startup(object sender, StartupEventArgs e)
        {
            MainWindow mw = new MainWindow();
            Application.Current.Resources.Add("mw", mw);
            
            WelcomeWindow ww = new WelcomeWindow();
             
            ww.Show();
        }
    }

    Then you are instantiating MainWindow first and making it the main window.

    https://msdn.microsoft.com/en-us/library/vstudio/aa350491%28v=vs.100%29.aspx?f=255&MSPPError=-2147217396

    Caching it in resources.

    You can then go grab it out resource and .show() mw later.


    Hope that helps.
    Recent Technet articles: Property List Editing; Dynamic XAML

    Monday, March 23, 2015 1:44 PM
  • When you open a form or a window by calling the ShowDialog() method, the call won't return until the window is closed (note that this is not necessarily the same thing as "stop any code running in your main window" though):

    WelcomeWindow win = new WelcomeWindow();
    win.ShowDialog(); //instead of Show();

    This effectively means that the user won't be able to interact with the other window until WelcomeWindow has been closed. The ShowDialog method works the same regardless of which constructor overload you are using to create an instance of the window or form.

    Hope that helps.

    Please remember to close your threads by marking all helpful posts as answer and please start a new thread if you have a new question.

     

    • Marked as answer by Dijkstra_ Monday, March 30, 2015 2:22 PM
    Monday, March 23, 2015 1:47 PM
  • these dialogues can i pot coxcombs/button etc. on them can i design like a window in xmal or do i need code it all in c#
    Monday, March 23, 2015 1:57 PM
  • these dialogues can i pot coxcombs/button etc. on them can i design like a window in xmal or do i need code it all in c#

    There is no difference between a dialog and a window. A window becomes a dialog when you open it by calling the ShowDialog method. So you can implement and define the layout of the dialog in either XAML or code exactly as you can with any other window.

    But please don't ask several questions in the same thread. Please close your threads by marking all helpful posts as answer once your orignal question has been answered and then start a new thread if you have a new question.

     

    Monday, March 23, 2015 2:03 PM