C# wpf window issue












-1














Currently running into an issue with opening a new wpf window. The current program sets a boolean called confirmed to false, the program then opens a new window and passes an ID to the new window where the user will be prompted to click a yes button if they can confirm the ID is their ID(if so the boolean is set to true) or no if the ID is not their ID(if so the boolean is set to false again). The new window will then close and return to the normal window with the new value of the boolean. An if statement then runs to check the value of the boolean, if false then a message is displayed to the user, if true the customer is then moved onto a new window.



The issue I am having is the program seems to open up the new window and prompt the user as it should, however at the same time the program steps into the next if statement checking the value of boolean before the user has a change to click yes or no. So the new window will open and then the next if statement will run even though I want to wait for user input, how do I prevent this from happen, code is listed below



Code for opening new window and boolean check



bool confirmed == false;
int id = 1;
promptWindow = new promptWindow(id, confirmed);


code for new window



public(int id, bool confirmed)
{
InitializeComponent();
}

private void btnYes_Click(object sender, RoutedEventArgs e)
{
//confirms the user wants to book and returns value
bool confirmation = true;
return confirmation;
this.Close;
}

private void btnNo_Click(object sender, RoutedEventArgs e)
{
//confirms the user doesn't want to book and returns value
bool confirmation = false;
this.Close();
}


Code for checking boolean value



if (confirmation == true)
{
//adds new customer to customer as they have confirmed booking
add.list(id);
}
else
{
MessageBox.Show("Booking not added");
}









share|improve this question
























  • "...The issue I am having...", are you sure this even compiles? I see return condition; from a void function (and if it wasn't void then this.Close() won't even be executed. Question: why aren't you properly using a VM here? Also note that (from what you're showing, at least) a plain MessageBox will do the job.
    – Adriano Repetti
    Nov 19 '18 at 13:39










  • Use ShowDialog() instead of Show() when you display the window. ShowDialog() won't return until the window has been closed.
    – mm8
    Nov 19 '18 at 13:39


















-1














Currently running into an issue with opening a new wpf window. The current program sets a boolean called confirmed to false, the program then opens a new window and passes an ID to the new window where the user will be prompted to click a yes button if they can confirm the ID is their ID(if so the boolean is set to true) or no if the ID is not their ID(if so the boolean is set to false again). The new window will then close and return to the normal window with the new value of the boolean. An if statement then runs to check the value of the boolean, if false then a message is displayed to the user, if true the customer is then moved onto a new window.



The issue I am having is the program seems to open up the new window and prompt the user as it should, however at the same time the program steps into the next if statement checking the value of boolean before the user has a change to click yes or no. So the new window will open and then the next if statement will run even though I want to wait for user input, how do I prevent this from happen, code is listed below



Code for opening new window and boolean check



bool confirmed == false;
int id = 1;
promptWindow = new promptWindow(id, confirmed);


code for new window



public(int id, bool confirmed)
{
InitializeComponent();
}

private void btnYes_Click(object sender, RoutedEventArgs e)
{
//confirms the user wants to book and returns value
bool confirmation = true;
return confirmation;
this.Close;
}

private void btnNo_Click(object sender, RoutedEventArgs e)
{
//confirms the user doesn't want to book and returns value
bool confirmation = false;
this.Close();
}


Code for checking boolean value



if (confirmation == true)
{
//adds new customer to customer as they have confirmed booking
add.list(id);
}
else
{
MessageBox.Show("Booking not added");
}









share|improve this question
























  • "...The issue I am having...", are you sure this even compiles? I see return condition; from a void function (and if it wasn't void then this.Close() won't even be executed. Question: why aren't you properly using a VM here? Also note that (from what you're showing, at least) a plain MessageBox will do the job.
    – Adriano Repetti
    Nov 19 '18 at 13:39










  • Use ShowDialog() instead of Show() when you display the window. ShowDialog() won't return until the window has been closed.
    – mm8
    Nov 19 '18 at 13:39
















-1












-1








-1







Currently running into an issue with opening a new wpf window. The current program sets a boolean called confirmed to false, the program then opens a new window and passes an ID to the new window where the user will be prompted to click a yes button if they can confirm the ID is their ID(if so the boolean is set to true) or no if the ID is not their ID(if so the boolean is set to false again). The new window will then close and return to the normal window with the new value of the boolean. An if statement then runs to check the value of the boolean, if false then a message is displayed to the user, if true the customer is then moved onto a new window.



The issue I am having is the program seems to open up the new window and prompt the user as it should, however at the same time the program steps into the next if statement checking the value of boolean before the user has a change to click yes or no. So the new window will open and then the next if statement will run even though I want to wait for user input, how do I prevent this from happen, code is listed below



Code for opening new window and boolean check



bool confirmed == false;
int id = 1;
promptWindow = new promptWindow(id, confirmed);


code for new window



public(int id, bool confirmed)
{
InitializeComponent();
}

private void btnYes_Click(object sender, RoutedEventArgs e)
{
//confirms the user wants to book and returns value
bool confirmation = true;
return confirmation;
this.Close;
}

private void btnNo_Click(object sender, RoutedEventArgs e)
{
//confirms the user doesn't want to book and returns value
bool confirmation = false;
this.Close();
}


Code for checking boolean value



if (confirmation == true)
{
//adds new customer to customer as they have confirmed booking
add.list(id);
}
else
{
MessageBox.Show("Booking not added");
}









share|improve this question















Currently running into an issue with opening a new wpf window. The current program sets a boolean called confirmed to false, the program then opens a new window and passes an ID to the new window where the user will be prompted to click a yes button if they can confirm the ID is their ID(if so the boolean is set to true) or no if the ID is not their ID(if so the boolean is set to false again). The new window will then close and return to the normal window with the new value of the boolean. An if statement then runs to check the value of the boolean, if false then a message is displayed to the user, if true the customer is then moved onto a new window.



The issue I am having is the program seems to open up the new window and prompt the user as it should, however at the same time the program steps into the next if statement checking the value of boolean before the user has a change to click yes or no. So the new window will open and then the next if statement will run even though I want to wait for user input, how do I prevent this from happen, code is listed below



Code for opening new window and boolean check



bool confirmed == false;
int id = 1;
promptWindow = new promptWindow(id, confirmed);


code for new window



public(int id, bool confirmed)
{
InitializeComponent();
}

private void btnYes_Click(object sender, RoutedEventArgs e)
{
//confirms the user wants to book and returns value
bool confirmation = true;
return confirmation;
this.Close;
}

private void btnNo_Click(object sender, RoutedEventArgs e)
{
//confirms the user doesn't want to book and returns value
bool confirmation = false;
this.Close();
}


Code for checking boolean value



if (confirmation == true)
{
//adds new customer to customer as they have confirmed booking
add.list(id);
}
else
{
MessageBox.Show("Booking not added");
}






c# wpf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 13:37









Kevin Wallis

3,07031533




3,07031533










asked Nov 19 '18 at 13:34









Callum

286




286












  • "...The issue I am having...", are you sure this even compiles? I see return condition; from a void function (and if it wasn't void then this.Close() won't even be executed. Question: why aren't you properly using a VM here? Also note that (from what you're showing, at least) a plain MessageBox will do the job.
    – Adriano Repetti
    Nov 19 '18 at 13:39










  • Use ShowDialog() instead of Show() when you display the window. ShowDialog() won't return until the window has been closed.
    – mm8
    Nov 19 '18 at 13:39




















  • "...The issue I am having...", are you sure this even compiles? I see return condition; from a void function (and if it wasn't void then this.Close() won't even be executed. Question: why aren't you properly using a VM here? Also note that (from what you're showing, at least) a plain MessageBox will do the job.
    – Adriano Repetti
    Nov 19 '18 at 13:39










  • Use ShowDialog() instead of Show() when you display the window. ShowDialog() won't return until the window has been closed.
    – mm8
    Nov 19 '18 at 13:39


















"...The issue I am having...", are you sure this even compiles? I see return condition; from a void function (and if it wasn't void then this.Close() won't even be executed. Question: why aren't you properly using a VM here? Also note that (from what you're showing, at least) a plain MessageBox will do the job.
– Adriano Repetti
Nov 19 '18 at 13:39




"...The issue I am having...", are you sure this even compiles? I see return condition; from a void function (and if it wasn't void then this.Close() won't even be executed. Question: why aren't you properly using a VM here? Also note that (from what you're showing, at least) a plain MessageBox will do the job.
– Adriano Repetti
Nov 19 '18 at 13:39












Use ShowDialog() instead of Show() when you display the window. ShowDialog() won't return until the window has been closed.
– mm8
Nov 19 '18 at 13:39






Use ShowDialog() instead of Show() when you display the window. ShowDialog() won't return until the window has been closed.
– mm8
Nov 19 '18 at 13:39














1 Answer
1






active

oldest

votes


















2














You should use events or ShowDialog().



bool confirmed = false;
int id = 1;
promptWindow = new promptWindow(id, confirmed);
bool result = promptWindow.ShowDialog();

if (result)
{
// confirm
}
else
{
// not confirmed
}


And your window



public(int id, bool confirmed)
{
InitializeComponent();
}

private void btnYes_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}

private void btnNo_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
}


The function Window.ShowDialog() will wait until the window has been closed. That means your code will wait in ShowDialog() line and you can check for result.
Function Window.Show() open the window and continue your code.



Another way is events: declare something like that on your Window



public event EventHandler<int> OnConfirmButton;
public event EventHandler<int> OnCancelButton;


On your buttons:



private void btnYes_Click(object sender, RoutedEventArgs e)
{
OnConfirmButton?.Invoke(this, this.id);
}

private void btnNo_Click(object sender, RoutedEventArgs e)
{
OnCancelButton?.Invoke(this, this.id);
}


And you can subscribe on your call:



bool confirmed = false;
int id = 1;
promptWindow = new promptWindow(id, confirmed);
promptWindow.OnConfirmButton += (sender, id) =>
{
// do something when confirm
};

promptWindow.OnCancelButton += (sender, id) =>
{
// do something when cancel
}

promptWindow.Show();


The id on arg is equal the id opened the window. This is just an example. Actually You do not need events on that case because you just need the result of window. But you can use on other cases, just follow the example.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375800%2fc-sharp-wpf-window-issue%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You should use events or ShowDialog().



    bool confirmed = false;
    int id = 1;
    promptWindow = new promptWindow(id, confirmed);
    bool result = promptWindow.ShowDialog();

    if (result)
    {
    // confirm
    }
    else
    {
    // not confirmed
    }


    And your window



    public(int id, bool confirmed)
    {
    InitializeComponent();
    }

    private void btnYes_Click(object sender, RoutedEventArgs e)
    {
    DialogResult = true;
    }

    private void btnNo_Click(object sender, RoutedEventArgs e)
    {
    DialogResult = false;
    }


    The function Window.ShowDialog() will wait until the window has been closed. That means your code will wait in ShowDialog() line and you can check for result.
    Function Window.Show() open the window and continue your code.



    Another way is events: declare something like that on your Window



    public event EventHandler<int> OnConfirmButton;
    public event EventHandler<int> OnCancelButton;


    On your buttons:



    private void btnYes_Click(object sender, RoutedEventArgs e)
    {
    OnConfirmButton?.Invoke(this, this.id);
    }

    private void btnNo_Click(object sender, RoutedEventArgs e)
    {
    OnCancelButton?.Invoke(this, this.id);
    }


    And you can subscribe on your call:



    bool confirmed = false;
    int id = 1;
    promptWindow = new promptWindow(id, confirmed);
    promptWindow.OnConfirmButton += (sender, id) =>
    {
    // do something when confirm
    };

    promptWindow.OnCancelButton += (sender, id) =>
    {
    // do something when cancel
    }

    promptWindow.Show();


    The id on arg is equal the id opened the window. This is just an example. Actually You do not need events on that case because you just need the result of window. But you can use on other cases, just follow the example.






    share|improve this answer




























      2














      You should use events or ShowDialog().



      bool confirmed = false;
      int id = 1;
      promptWindow = new promptWindow(id, confirmed);
      bool result = promptWindow.ShowDialog();

      if (result)
      {
      // confirm
      }
      else
      {
      // not confirmed
      }


      And your window



      public(int id, bool confirmed)
      {
      InitializeComponent();
      }

      private void btnYes_Click(object sender, RoutedEventArgs e)
      {
      DialogResult = true;
      }

      private void btnNo_Click(object sender, RoutedEventArgs e)
      {
      DialogResult = false;
      }


      The function Window.ShowDialog() will wait until the window has been closed. That means your code will wait in ShowDialog() line and you can check for result.
      Function Window.Show() open the window and continue your code.



      Another way is events: declare something like that on your Window



      public event EventHandler<int> OnConfirmButton;
      public event EventHandler<int> OnCancelButton;


      On your buttons:



      private void btnYes_Click(object sender, RoutedEventArgs e)
      {
      OnConfirmButton?.Invoke(this, this.id);
      }

      private void btnNo_Click(object sender, RoutedEventArgs e)
      {
      OnCancelButton?.Invoke(this, this.id);
      }


      And you can subscribe on your call:



      bool confirmed = false;
      int id = 1;
      promptWindow = new promptWindow(id, confirmed);
      promptWindow.OnConfirmButton += (sender, id) =>
      {
      // do something when confirm
      };

      promptWindow.OnCancelButton += (sender, id) =>
      {
      // do something when cancel
      }

      promptWindow.Show();


      The id on arg is equal the id opened the window. This is just an example. Actually You do not need events on that case because you just need the result of window. But you can use on other cases, just follow the example.






      share|improve this answer


























        2












        2








        2






        You should use events or ShowDialog().



        bool confirmed = false;
        int id = 1;
        promptWindow = new promptWindow(id, confirmed);
        bool result = promptWindow.ShowDialog();

        if (result)
        {
        // confirm
        }
        else
        {
        // not confirmed
        }


        And your window



        public(int id, bool confirmed)
        {
        InitializeComponent();
        }

        private void btnYes_Click(object sender, RoutedEventArgs e)
        {
        DialogResult = true;
        }

        private void btnNo_Click(object sender, RoutedEventArgs e)
        {
        DialogResult = false;
        }


        The function Window.ShowDialog() will wait until the window has been closed. That means your code will wait in ShowDialog() line and you can check for result.
        Function Window.Show() open the window and continue your code.



        Another way is events: declare something like that on your Window



        public event EventHandler<int> OnConfirmButton;
        public event EventHandler<int> OnCancelButton;


        On your buttons:



        private void btnYes_Click(object sender, RoutedEventArgs e)
        {
        OnConfirmButton?.Invoke(this, this.id);
        }

        private void btnNo_Click(object sender, RoutedEventArgs e)
        {
        OnCancelButton?.Invoke(this, this.id);
        }


        And you can subscribe on your call:



        bool confirmed = false;
        int id = 1;
        promptWindow = new promptWindow(id, confirmed);
        promptWindow.OnConfirmButton += (sender, id) =>
        {
        // do something when confirm
        };

        promptWindow.OnCancelButton += (sender, id) =>
        {
        // do something when cancel
        }

        promptWindow.Show();


        The id on arg is equal the id opened the window. This is just an example. Actually You do not need events on that case because you just need the result of window. But you can use on other cases, just follow the example.






        share|improve this answer














        You should use events or ShowDialog().



        bool confirmed = false;
        int id = 1;
        promptWindow = new promptWindow(id, confirmed);
        bool result = promptWindow.ShowDialog();

        if (result)
        {
        // confirm
        }
        else
        {
        // not confirmed
        }


        And your window



        public(int id, bool confirmed)
        {
        InitializeComponent();
        }

        private void btnYes_Click(object sender, RoutedEventArgs e)
        {
        DialogResult = true;
        }

        private void btnNo_Click(object sender, RoutedEventArgs e)
        {
        DialogResult = false;
        }


        The function Window.ShowDialog() will wait until the window has been closed. That means your code will wait in ShowDialog() line and you can check for result.
        Function Window.Show() open the window and continue your code.



        Another way is events: declare something like that on your Window



        public event EventHandler<int> OnConfirmButton;
        public event EventHandler<int> OnCancelButton;


        On your buttons:



        private void btnYes_Click(object sender, RoutedEventArgs e)
        {
        OnConfirmButton?.Invoke(this, this.id);
        }

        private void btnNo_Click(object sender, RoutedEventArgs e)
        {
        OnCancelButton?.Invoke(this, this.id);
        }


        And you can subscribe on your call:



        bool confirmed = false;
        int id = 1;
        promptWindow = new promptWindow(id, confirmed);
        promptWindow.OnConfirmButton += (sender, id) =>
        {
        // do something when confirm
        };

        promptWindow.OnCancelButton += (sender, id) =>
        {
        // do something when cancel
        }

        promptWindow.Show();


        The id on arg is equal the id opened the window. This is just an example. Actually You do not need events on that case because you just need the result of window. But you can use on other cases, just follow the example.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 '18 at 13:49

























        answered Nov 19 '18 at 13:43









        Kevin Kouketsu

        363112




        363112






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375800%2fc-sharp-wpf-window-issue%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$