Hi every body
i have devoloped a custom MessageBox Class to use it in other application,as below

# 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();
#         }
#}

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

# 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(

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 (MessageBoxCustomized.Show("Save Changes", "Attention",MessageBoxButton.YesNoCancel))

and terminate the application and i don't know why and so pls advise

Recommended Answers

All 6 Replies

Two problems. Line 14 won't compile. Second problem is in lines 57-66. You return _msgboxresult but don't set it in the method so it will always return the default value you set in line 8. You need to get the returned value in line 64.

Momerath
pls note that line 14 " public void MessageBoxManager(MessageBoxButtonbutsta)" is compiled (i don't know why u said it won't !)
also when line 64 executed " msgBox.ShowDialog();" it supposed to set the "_msgboxresult" depending on the pressed button, but the problem is line 64 not executed

The reason I said line 14 won't compile is that there is no type "MessageBoxButtonbusta" or no variable in that statement. VS says "Identifier Expected". I suggest you look at your code again.

sorry Momerath it's writing mistake the right Fn signature is
public void MessageBoxManager(MessageBoxButton butsta)
and i don't know how to edit my original post

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

void exitCmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {

            Terminate();
           
        }

Terminate Fn

/// <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:
                     
                        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!

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."

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.