Window_Closing problem in WPF_C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • falcon eyes
    New Member
    • Dec 2008
    • 9

    Window_Closing problem in WPF_C#

    Hi every body
    i have devoloped a custom MessageBox Class to use it in other application,as below
    Code:
     public partial class MessageBoxCustomized : Window
        {
    
           static MessageBoxResult _msgboxresult;
            public MessageBoxCustomized()
            {
                InitializeComponent();
                _msgboxresult = MessageBoxResult.None;
                Owner = Application.Current.MainWindow;
              
                
            }
    
            public void MessageBoxManager(MessageBoxButtonbutsta)
            { 
                switch(butsta)
                {
                    case MessageBoxButton.OK:
                        agreeButt.Visibility = System.Windows.Visibility.Collapsed;
                        cancelButt.Visibility = System.Windows.Visibility.Collapsed;
                        disagreeButt.Content = "Ok";
                        break;
    
                    case MessageBoxButton.OKCancel:
                        agreeButt.Visibility = System.Windows.Visibility.Collapsed;
                        midcol.Width = new GridLength(0);
                        disagreeButt.Content = "Ok";
                        break;
                    case MessageBoxButton.YesNo:
                        cancelButt.Visibility = System.Windows.Visibility.Collapsed;
                        mbgnd.Width = new GridLength(0);
                        break;
                }
            }
    
            public static void Show(string msg)
            {
                MessageBoxCustomized msgBox = new MessageBoxCustomized();
                msgBox.msgTxt.Content = msg;
                msgBox.MessageBoxManager(MessageBoxButton.OK);
                 System.Media.SystemSounds.Exclamation.Play();
                msgBox.ShowDialog();
               
            }
    
            public static void Show(string msg, string title)
            {
                MessageBoxCustomized msgBox = new MessageBoxCustomized();
                msgBox.msgTxt.Content = msg;
                msgBox.Title = title;
                msgBox.MessageBoxManager(MessageBoxButton.OK);
                  System.Media.SystemSounds.Exclamation.Play();
                msgBox.ShowDialog();
              
           }
    
            public static MessageBoxResult Show(string msg, string title, MessageBoxButton butsta)
            {
                MessageBoxCustomized msgBox = new MessageBoxCustomized();
                msgBox.msgTxt.Content = msg;
                msgBox.Title = title;
                msgBox.MessageBoxManager(butsta);
                System.Media.SystemSounds.Exclamation.Play(); 
                msgBox.ShowDialog();
               
                return _msgboxresult;
            }
    
    
    
            private void agreeButt_Click(object sender, RoutedEventArgs e)
            {
              _msgboxresult=MessageBoxResult.Yes;
              Close();
            }
    
            private void disagreeButtutt_Click(object sender, RoutedEventArgs e)
            {
                if ((string)disagreeButt.Content == "Ok")
                  _msgboxresult=MessageBoxResult.OK;
              else
                  _msgboxresult=MessageBoxResult.No;
              Close();
    
            }
    
            private void cancelButt_Click(object sender, RoutedEventArgs e)
            {
              _msgboxresult=MessageBoxResult.Cancel;
            }
    
            private void Activate_Title_Icons(object sender, MouseEventArgs e)
            {
                Close_btn.Fill = (Brush)App.Current.Resources["Close_act"];
            }
    
            private void Deactivate_Title_Icons(object sender, MouseEventArgs e)
            {
                Close_btn.Fill = (Brush)App.Current.Resources["Close_inact"];
            }
    
            private void Close_pressing(object sender, MouseButtonEventArgs e)
            {
                Close_btn.Fill = (Brush)App.Current.Resources["Close_pr"];
            }
    
            private void EXIT(object sender, MouseButtonEventArgs e)
            {
                this.Close();// Environment.Exit(0);
            }
    
            private void titLab_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                    this.DragMove();
            }
    
            private void msgboxMainWnd_Unloaded(object sender, RoutedEventArgs e)
            {
    
            }
    
            }
    and i made the application which will use it
    and it work fine except in one place and it's Window_Closing handler.

    here's a snapshot of my code
    Code:
    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    
    if (savingRequest == true)
    {
    
    switch ([B]MessageBoxCustomized.Show("Save Changes", "Attention", MessageBoxButton.YesNoCancel)[/B])
    {
    case MessageBoxResult.Yes:
    {
    notifyIcon.Dispose();
    SaveTreeView();
    }
    break;
    
    case MessageBoxResult.No: notifyIcon.Dispose();
    break;
    
    case MessageBoxResult.Cancel:e.Cancel = true;
    
    break;
    
    }
    }
    //MessageBox.Show(
    If i replce the custom MessageBox with the standard windows MessageBox it works fine as i want but when i use my custom message box the compiler skip all the code from the statement

    switch (MessageBoxCust omized.Show("Sa ve Changes", "Attention",Mes sageBoxButton.Y esNoCancel))

    and terminate the application and i don't know why and
    so pls advise
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I suspect I know what might be going on, but lets verify. Instead of putting the Show call right in the switch statement, do this...

    Code:
    MessageBoxResult mbr = MessageBoxCustomized.Show(...);
    Console.WriteLine(mbr.ToString()); // or whatever the WPF equivalent is
    ...
    See what the result of that call is.

    Comment

    • falcon eyes
      New Member
      • Dec 2008
      • 9

      #3
      I did what u suggested and the result of mbr.tostring()w as the name of the button i pressed i.e if i press button yes then mbr is "yes", if i pressed No Button mbr is "No"

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        The case you're looking to hit is MessageBoxResul t.Cancel, isn't it? That sets the event's cancel property to true.

        It looks like the property is coming back correctly, so can you verify that program flow is correct? Put a MessageBox.Show ("blah"); in the cancel case and see if it shows up when you click cancel.

        Give that a try?

        Comment

        • falcon eyes
          New Member
          • Dec 2008
          • 9

          #5
          GaryTexmo
          the MessageBoxResul t.Cancel never reached cause the MessagBoxCustom ized window doesn't appear in case of MessageBoxButto n.YesNoCancel

          i had stepped my code as u suggested and i made a break at the Fn
          public static MessageBoxResul t Show(string msg, string title, MessageBoxButto n butsta)
          if i made butsta=YesNo then every thing is fine and it work,but if i made butsta=YesNoCan cel the step work fine untill reach the statement
          msgBox.ShowDial og();
          it execute it but doesn't show the dialog and immediately go to the next statement

          return _msgboxresult; //the default value for _msgboxresult is None

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            Oh, sorry for not understanding the issue right away. It looks like your MessageBoxManag er property doesn't do anything for a YesNoCancel enum value. Now I'm looking at the code you've posted and I don't really understand why, but maybe that's got something to do with it.

            You say it works fine with YesNo, but YesNoCancel causes different behaviour. That's all I can really think of to try. Can you step through those code paths and see what might causing this? I wonder if ShowDialog is actually working, it's just that something is causing it to close right away. Maybe put a breakpoint in the close methods and see if you can find anything out.

            Finally, I notice you've got a bunch of overloads for Show which all do basically the same thing, they just assigns a different value. It might be worthwhile to break some of that code out into another method, InitializeMessa geBox perhaps, and then have all the methods call that, making their individual changes after, then calling the show. It's not vital, but it's something to help reduce debugging complexity.

            Comment

            • falcon eyes
              New Member
              • Dec 2008
              • 9

              #7
              I'm stepped through the code and the same result "The MessgeBoxCustom ized window doesn't work only in window closing event handler" and really don't know why and
              I'm fraud about the problem by canceling window closing event handler and created a new fun called Terminate and created a custom command "Exit" and call the Terminate fun from within it
              Code:
              void exitCmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
                      {
              
                          Terminate();
                         
                      }
              Terminate Fn
              Code:
               /// <summary>
                      /// Fn to confirm the user if data saving required befor closing the application
                      /// </summary>
                      private void Terminate(/*System.ComponentModel.CancelEventArgs ee*/)
                      {
                          if (savingRequest == true)
                              switch (MessageBoxCustomized.Show("Do you want to save changes", "Saving Attention", MessageBoxButton.YesNoCancel))
                              {
                                  case MessageBoxResult.Yes:
                                     
                                          notifyIcon.Dispose();
                                          SaveTreeView();
                                          Application.Current.Shutdown();
                                          break;
              
                                  case MessageBoxResult.No:
                                      notifyIcon.Dispose();
                                      Application.Current.Shutdown();
                                      break;
              
                                  case MessageBoxResult.Cancel:
                                      //  ee.Cancel = true;
                                      break;
              
                              }
                          else //if no saving required
                          {
                              switch (MessageBoxCustomized.Show("Are you Sure to exit ?", "Exit Application", MessageBoxButton.YesNo))
                              {
                                  case MessageBoxResult.Yes: Application.Current.Shutdown(); break;
                                  case MessageBoxResult.No: break;
                              }
                          }
                      }
              and it works perfect

              Comment

              • falcon eyes
                New Member
                • Dec 2008
                • 9

                #8
                Hey people i have found the answer why "The MessgeBoxCustom ized window doesn't work only in window closing event handler" in stackoverflow forum and here's the context literal

                "The Closing event cannot be canceled if you call Application.Cur rent.Shutdown() . Just call the Window.Close() method instead, which will give you a chance to veto the close operation. Once all your program's windows have closed the application will shutdown automatically."

                and i was using Application.Cur rent.Shutdown() to exit my application and when i used mainWnd.Close() ; since my main window name is "mainWnd" then every thing is working fine thanks for all of you

                Comment

                Working...