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();
-
}
-
-
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 -
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
7 3681
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... - MessageBoxResult mbr = MessageBoxCustomized.Show(...);
-
Console.WriteLine(mbr.ToString()); // or whatever the WPF equivalent is
-
...
See what the result of that call is.
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"
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?
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
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.
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 -
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:
-
// 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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |