473,396 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Window_Closing problem in WPF_C#

Hi every body
i have devoloped a custom MessageBox Class to use it in other application,as below
Expand|Select|Wrap|Line Numbers
  1.  public partial class MessageBoxCustomized : Window
  2.     {
  3.  
  4.        static MessageBoxResult _msgboxresult;
  5.         public MessageBoxCustomized()
  6.         {
  7.             InitializeComponent();
  8.             _msgboxresult = MessageBoxResult.None;
  9.             Owner = Application.Current.MainWindow;
  10.  
  11.  
  12.         }
  13.  
  14.         public void MessageBoxManager(MessageBoxButtonbutsta)
  15.         { 
  16.             switch(butsta)
  17.             {
  18.                 case MessageBoxButton.OK:
  19.                     agreeButt.Visibility = System.Windows.Visibility.Collapsed;
  20.                     cancelButt.Visibility = System.Windows.Visibility.Collapsed;
  21.                     disagreeButt.Content = "Ok";
  22.                     break;
  23.  
  24.                 case MessageBoxButton.OKCancel:
  25.                     agreeButt.Visibility = System.Windows.Visibility.Collapsed;
  26.                     midcol.Width = new GridLength(0);
  27.                     disagreeButt.Content = "Ok";
  28.                     break;
  29.                 case MessageBoxButton.YesNo:
  30.                     cancelButt.Visibility = System.Windows.Visibility.Collapsed;
  31.                     mbgnd.Width = new GridLength(0);
  32.                     break;
  33.             }
  34.         }
  35.  
  36.         public static void Show(string msg)
  37.         {
  38.             MessageBoxCustomized msgBox = new MessageBoxCustomized();
  39.             msgBox.msgTxt.Content = msg;
  40.             msgBox.MessageBoxManager(MessageBoxButton.OK);
  41.              System.Media.SystemSounds.Exclamation.Play();
  42.             msgBox.ShowDialog();
  43.  
  44.         }
  45.  
  46.         public static void Show(string msg, string title)
  47.         {
  48.             MessageBoxCustomized msgBox = new MessageBoxCustomized();
  49.             msgBox.msgTxt.Content = msg;
  50.             msgBox.Title = title;
  51.             msgBox.MessageBoxManager(MessageBoxButton.OK);
  52.               System.Media.SystemSounds.Exclamation.Play();
  53.             msgBox.ShowDialog();
  54.  
  55.        }
  56.  
  57.         public static MessageBoxResult Show(string msg, string title, MessageBoxButton butsta)
  58.         {
  59.             MessageBoxCustomized msgBox = new MessageBoxCustomized();
  60.             msgBox.msgTxt.Content = msg;
  61.             msgBox.Title = title;
  62.             msgBox.MessageBoxManager(butsta);
  63.             System.Media.SystemSounds.Exclamation.Play(); 
  64.             msgBox.ShowDialog();
  65.  
  66.             return _msgboxresult;
  67.         }
  68.  
  69.  
  70.  
  71.         private void agreeButt_Click(object sender, RoutedEventArgs e)
  72.         {
  73.           _msgboxresult=MessageBoxResult.Yes;
  74.           Close();
  75.         }
  76.  
  77.         private void disagreeButtutt_Click(object sender, RoutedEventArgs e)
  78.         {
  79.             if ((string)disagreeButt.Content == "Ok")
  80.               _msgboxresult=MessageBoxResult.OK;
  81.           else
  82.               _msgboxresult=MessageBoxResult.No;
  83.           Close();
  84.  
  85.         }
  86.  
  87.         private void cancelButt_Click(object sender, RoutedEventArgs e)
  88.         {
  89.           _msgboxresult=MessageBoxResult.Cancel;
  90.         }
  91.  
  92.         private void Activate_Title_Icons(object sender, MouseEventArgs e)
  93.         {
  94.             Close_btn.Fill = (Brush)App.Current.Resources["Close_act"];
  95.         }
  96.  
  97.         private void Deactivate_Title_Icons(object sender, MouseEventArgs e)
  98.         {
  99.             Close_btn.Fill = (Brush)App.Current.Resources["Close_inact"];
  100.         }
  101.  
  102.         private void Close_pressing(object sender, MouseButtonEventArgs e)
  103.         {
  104.             Close_btn.Fill = (Brush)App.Current.Resources["Close_pr"];
  105.         }
  106.  
  107.         private void EXIT(object sender, MouseButtonEventArgs e)
  108.         {
  109.             this.Close();// Environment.Exit(0);
  110.         }
  111.  
  112.         private void titLab_MouseMove(object sender, MouseEventArgs e)
  113.         {
  114.             if (e.LeftButton == MouseButtonState.Pressed)
  115.                 this.DragMove();
  116.         }
  117.  
  118.         private void msgboxMainWnd_Unloaded(object sender, RoutedEventArgs e)
  119.         {
  120.  
  121.         }
  122.  
  123.         }
  124.  
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
Expand|Select|Wrap|Line Numbers
  1. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  2. {
  3.  
  4. if (savingRequest == true)
  5. {
  6.  
  7. switch (MessageBoxCustomized.Show("Save Changes", "Attention", MessageBoxButton.YesNoCancel))
  8. {
  9. case MessageBoxResult.Yes:
  10. {
  11. notifyIcon.Dispose();
  12. SaveTreeView();
  13. }
  14. break;
  15.  
  16. case MessageBoxResult.No: notifyIcon.Dispose();
  17. break;
  18.  
  19. case MessageBoxResult.Cancel:e.Cancel = true;
  20.  
  21. break;
  22.  
  23. }
  24. }
  25. //MessageBox.Show(
  26.  
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
Feb 24 '11 #1
7 3681
GaryTexmo
1,501 Expert 1GB
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...

Expand|Select|Wrap|Line Numbers
  1. MessageBoxResult mbr = MessageBoxCustomized.Show(...);
  2. Console.WriteLine(mbr.ToString()); // or whatever the WPF equivalent is
  3. ...
See what the result of that call is.
Feb 24 '11 #2
I did what u suggested and the result of mbr.tostring()was 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"
Feb 25 '11 #3
GaryTexmo
1,501 Expert 1GB
The case you're looking to hit is MessageBoxResult.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?
Feb 25 '11 #4
GaryTexmo
the MessageBoxResult.Cancel never reached cause the MessagBoxCustomized window doesn't appear in case of MessageBoxButton.YesNoCancel

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
Feb 26 '11 #5
GaryTexmo
1,501 Expert 1GB
Oh, sorry for not understanding the issue right away. It looks like your MessageBoxManager 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, InitializeMessageBox 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.
Feb 28 '11 #6
I'm stepped through the code and the same result "The MessgeBoxCustomized 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
Expand|Select|Wrap|Line Numbers
  1. void exitCmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
  2.         {
  3.  
  4.             Terminate();
  5.  
  6.         }
  7.  
Terminate Fn
Expand|Select|Wrap|Line Numbers
  1.  /// <summary>
  2.         /// Fn to confirm the user if data saving required befor closing the application
  3.         /// </summary>
  4.         private void Terminate(/*System.ComponentModel.CancelEventArgs ee*/)
  5.         {
  6.             if (savingRequest == true)
  7.                 switch (MessageBoxCustomized.Show("Do you want to save changes", "Saving Attention", MessageBoxButton.YesNoCancel))
  8.                 {
  9.                     case MessageBoxResult.Yes:
  10.  
  11.                             notifyIcon.Dispose();
  12.                             SaveTreeView();
  13.                             Application.Current.Shutdown();
  14.                             break;
  15.  
  16.                     case MessageBoxResult.No:
  17.                         notifyIcon.Dispose();
  18.                         Application.Current.Shutdown();
  19.                         break;
  20.  
  21.                     case MessageBoxResult.Cancel:
  22.                         //  ee.Cancel = true;
  23.                         break;
  24.  
  25.                 }
  26.             else //if no saving required
  27.             {
  28.                 switch (MessageBoxCustomized.Show("Are you Sure to exit ?", "Exit Application", MessageBoxButton.YesNo))
  29.                 {
  30.                     case MessageBoxResult.Yes: Application.Current.Shutdown(); break;
  31.                     case MessageBoxResult.No: break;
  32.                 }
  33.             }
  34.         }
  35.  
and it works perfect
Mar 2 '11 #7
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
Mar 3 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: raysaun | last post by:
I am trying to set a TableCellEditor for a JTable, however, the editor never seems to get called. I can not even get a simplified version (below) to work. When I put a breakpoint on the return...
5
by: Ron L | last post by:
I have an MDI application with a number of child windows. In each child window I am catching the Closing event and having the child window decide if it should set cancel to true. The intent here...
2
by: shonend | last post by:
**** sorry about the length of the message. If you can't read the whole thing and still willing to help, read the last 2 paragraphs where the main problem is described. The introduction story is...
2
by: minouparhizkar | last post by:
hi could anyone helping me to finish this i have no idea how to implement the program in java im trying to grabbing the pixel from image and then convert it to the txt file .i did that but it didnt...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.