Window_Closing problem in WPF_C#
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Window_Closing problem in WPF_C#

  1. #1
    Join Date
    Mar 2006
    Posts
    38

    Window_Closing problem in WPF_C#

    Hi every body
    i devoloped a custom MessageBox Class to use it in other application, and i made the application
    and it work fine except in one place and it's Window_Closing handler.
    If i replce the custom MessageBox with the standard MessageBox it works when i use my custom message box it doesn't and i don't know why and here's asnapshot of my code
    [code]
    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {

    if (savingRequest == true)
    {

    switch (MessageBoxCustomized.Show("Save Changes", "Attention", MessageBoxButton.YesNoCancel))
    {
    case MessageBoxResult.Yes:
    {
    notifyIcon.Dispose();
    SaveTreeView();
    }
    break;

    case MessageBoxResult.No: notifyIcon.Dispose();
    break;

    case MessageBoxResult.Cancel:e.Cancel = true;

    break;

    }
    }
    //MessageBox.Show(
    [code/]

    so pls advise

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Window_Closing problem in WPF_C#

    Please define "Doesn't work" for us.

  3. #3
    Join Date
    Mar 2006
    Posts
    38

    Re: Window_Closing problem in WPF_C#

    sorry what u mean by "Please define "Doesn't work" for us. "

  4. #4
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,247

    Re: Window_Closing problem in WPF_C#

    Quote Originally Posted by Falcon Eyes View Post
    sorry what u mean by "Please define "Doesn't work" for us. "
    It's not clear to us what the problem is.

    Please state what is not working and what the proper behavior you expect.

  5. #5
    Join Date
    Mar 2006
    Posts
    38

    Re: Window_Closing problem in WPF_C#

    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)
    # {
    #             //The problem is here in the statement below  
    # switch (MessageBoxCustomized.Show("Save Changes", "Attention", MessageBoxButton.YesNoCancel))
    # {
    # case MessageBoxResult.Yes:
    # {
    # notifyIcon.Dispose();
    # SaveTreeView();
    # }
    # break;
    #  
    # case MessageBoxResult.No: notifyIcon.Dispose();
    # break;
    #  
    # case MessageBoxResult.Cancel:e.Cancel = true;
    #  
    # break;
    #  
    # }
    # }
    If i replce the custom MessageBox with the standard windows MessageBox object it works fine as i want but when i use my custom message box object the compiler skip all the code from the statement

    switch(MessageBoxCustomized.Show("SaveChanges","Attention",MessageBoxButton.YesNoCancel))

    and terminate the application and i don't know why and

  6. #6
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,247

    Re: Window_Closing problem in WPF_C#

    Step through the code and debug it.

    But first, change your code temporarily so you can see what the Show() method returns.

    rather than
    Code:
    switch( MessageBoxCustomized.Show("SaveChanges","Attention",MessageBoxButton.YesNoCancel))
    write
    Code:
    var dlgResult = MessageBoxCustomized.Show("SaveChanges","Attention",MessageBoxButton.YesNoCancel);
    
    switch( dlgResult )
    {
    ...
    }
    One problem I see is that your static Show( ) method doesn't return anything. I'm surprise your code compiles. It should return a DialogResult enum.

  7. #7
    Join Date
    Mar 2006
    Posts
    38

    Re: Window_Closing problem in WPF_C#

    i had stepped my code as u suggested and i made a break at the Fn
    public static MessageBoxResult Show(string msg, string title, MessageBoxButton butsta)
    if i made butsta=YesNo then every thing is fine and it work,but if i made butsta=YesNoCancel the step work fine untill reach the statement
    msgBox.ShowDialog();
    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

    then

    the value for dlgResult was "None"

  8. #8
    Join Date
    Mar 2006
    Posts
    38

    Re: Window_Closing problem in WPF_C#

    Hey guys
    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 but i want to know why "The MessgeBoxCustomized window doesn't work only in window closing event handler" is there any technical problem!

  9. #9
    Join Date
    Mar 2006
    Posts
    38

    Talking Re: Window_Closing problem in WPF_C#

    Hey people i have found the answer why "The MessgeBoxCustomized 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.Current.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."

  10. #10
    Join Date
    Mar 2006
    Posts
    38

    Re: Window_Closing problem in WPF_C#

    and i was using Application.Current.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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center