WPF
I need CLEAN and START again the window SetPathCharger.xaml when the user clic on "Yes" the message box, the problem is the application send a error InvalidOperationException.
public void ExitProgram(string message)
{
var restart = MessageBox.Show("Do you want do it again?",
"Question", MessageBoxButton.YesNo,
MessageBoxImage.Question).ToString();
if (restart == "Yes")
{
_setPathCharger.ShowDialog();
}
if (restart == "No")
{
Environment.Exit(0);
}
}
How can I do this?
You should just create and show a new SetPathCharger window, instead of reusing the current one. Something like:
_setPathCharger = new SetPathCharger();
_setPathCharger.ShowDialog();
Assuming ExitProgram is in some outer scope and is triggered after closing _setPathCharger then I suppose you are trying to ShowDialog() a disposed object.
Try to:
_setPathCharger = new SetPathCharger();
_setPathCharger.ShowDialog();
Related
I want to create a "Are you sure?" system when user clicks the close button of window. Is it possible to catch FormClosing event in Unity ?
Thanks.
Edit:
void OnApplicationQuit()
{
DialogResult result = MessageBox.Show(
"Are you sure you want to cancel ?",
"Question",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Application.Quit();
}
}
I tried this to open a dialog when user clicks (X) button. It works but, looks like it creates a new dialog each frame.
This question needs an updated answer as Application.CancelQuit() is deprecated and MonoBehaviour.OnApplicationQuit() doesn't prevent the application from closing.
To accomplish this exit confirmation method, it's better to use: Application.wantsToQuit.
From the docs:
Unity raises this event when the player application wants to quit.
Add an event handler to this event to receive a notification that application is attempting to quit.
When this event is raised the quit process has started but can be cancelled. This means the player is not guaranteed to quit. For a guaranteed quit event take a look at Application.quitting.
Return true and the quit process will continue. Return false and the quit process will cancel.
Example:
// When the application starts, append a method
// that will run when the user attempts to exit
[RuntimeInitializeOnLoadMethod]
static void RunOnStart() {
Application.wantsToQuit += WantsToQuit;
}
public static bool quitConfirmation = false;
static bool WantsToQuit() {
if(quitConfirmation) {
return true;
} else {
RequestQuitConfirmation();
}
return false;
}
static void RequestQuitConfirmation() {
DialogResult result = MessageBox.Show(
"Are you sure you want to cancel ?",
"Question",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
quitConfirmation = true;
Application.Quit();
}
}
Note: The return value of this event (Application.wantsToQuit) is ignored when exiting play mode in the editor. IMPORTANT: The return has no effect on iPhone. Application can not prevent termination under iPhone OS.
Have a look at MonoBehaviour.OnApplicationQuit() and Application.CancelQuit()
The current problem is that the "mainScreen" shows up, but immediately closes, I have no idea why. This is the piece of code that handles the close and opening of the new form.
Edit: .this refers to Login.cs (sorry)
if(templogin == true && permission.Equals("1"))
{
mainScreen.IsAdmin();
this.Close();
mainScreen.ShowDialog();
}
I think, you should not close the application(as login form seems main form while you started the application) before another form run:
Do like this :
if(templogin == true && permission.Equals("1"))
{
this.Hide();
mainScreen.IsAdmin();
mainScreen.ShowDialog();
this.Close();
}
If you want to run 2 forms (switching from one to another), then you should do it in Main
Instead of
Application.Run(new StartupForm());
you'll have to use
var startup = new StartupForm();
startup.ShowDialog();
if(somecondition) // when StartupForm is closed and return something (or property is set, etc)
{
var main = new MainScreen();
main.ShowDialog();
}
You don't need Application.Run at all when using ShowDialog.
I am alerting the user about the process complete like :
var result = MessageBox.Show("Completed!!", "Upload Status", MessageBoxButton.OK, MessageBoxImage.Information);
if (result == MessageBoxResult.OK)
{
result = MessageBox.Show("Do you want to Quit the application ?", "Quit Application", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
OpenWebSite();
// I want to close the message box and complete application.
}
}
If I write this.Close..It is giving exception, So I wrote it like this :
Dispatcher.BeginInvoke(new Action(() => { this.Close(); }));
Still the page is only closing after two click on [Yes] button, which is making the the OpenWebSite(); to be called two times. ...
Any idea on what is causing this?
Use Application.Current.Shutdown()
protected override void OnBeforeInstall(IDictionary savedState)
{
base.OnBeforeInstall(savedState);
DialogResult result = DialogResult.None;
if (isExcelIsRunning())
{
result = MessageBox.Show("Excel is still running. Please save your work and close Excel, and then click \"Retry\" button to continue with installation.", "", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
}
while (result == DialogResult.Retry)
{
if (!isExcelIsRunning())
{
break;
}
}
}
//check if excel is currently running
private bool isExcelIsRunning()
{
bool flag = false;
Process[] xlProc = Process.GetProcessesByName("excel");
if (xlProc != null)
flag = true;
return flag;
}
The above is the code I am about to use for my Installer Class.
What I want to achieve here is that I need the installer to check whether or not Excel is currently running during the installation. And if it is running, then there should be a pop-up message at the start of installation to alert user to close off Excel and the installer should pause until there's no Excel instance found in the process list any more.
Back to the code, I don't think that while block is quite right, as it could cause an endless loop after user clicks "Retry" button if in the mean time Excel is still running.
So what'd be the better approach? Would anyone here take a look at the above code and see where I can improve?
I think your code would be something like this:
while(isExcelRunning())
{
var result = MessageBox.Show("Excel is still running. Please save your work and close Excel, and then click \"Retry\" button to continue with installation.", "", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
if (result != DialogResult.Retry)
{
//Handle Cancel
break;
}
}
This will keep displaying the alert in a loop, either until Excel exits, or they press cancel, at which you can do whatever you need to do; then we exit the loop (or you could return entirely out of the method).
The key here is display the alert repetitively, either until Excel is gone or they choose to cancel. If you need to do some code after the loop based on the response; perhaps something like this:
var userHasCancelled = false;
while(!userHasCancelled && isExcelRunning())
{
var result = MessageBox.Show("Excel is still running. Please save your work and close Excel, and then click \"Retry\" button to continue with installation.", "", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
if (result != DialogResult.Retry)
{
userHasCancelled = true;
}
}
if (userHasCancelled)
{
//User cancelled...
}
else
{
//Continue.
}
I have a Form_Closing event that prompts the user if the file has been changed to save if changes have been made to the file (standard Yes/No/Cancel options). Cancel is where things don't work as they should.
If I select File -> New and there is an existing file with changes I get prompted as expected, bit when I select Cancel the new form is presented rather than staying on the current form and I end up with two forms open at once.
Here is MainForm (File New) code:
if (editForm != null)
{
// Close existing Editor form
editForm.Close();
// Open new form
editForm = new EditorForm(this);
// Close Form Events
editForm.Closing += new CancelEventHandler(EditorForm_Closing);
editForm.Show();
editForm.Focus();
else
{
// Open new Editor
editForm = new EditorForm(this);
// Close Form Events
editForm.Closing += new CancelEventHandler(EditorForm_Closing);
editForm.Show();
editForm.Focus();
}
Here is my EditForm_Closing:
if (editForm != null)
{
if (editForm.diagramComponent.Model.Modified)
{
DialogResult res = MessageBox.Show(this, "The project has been modified. Save changes?", "Save changes", MessageBoxButtons.YesNoCancel);
if (res == DialogResult.Yes)
{
if (!editForm.HasFileName)
{
if (this.saveEditorDialog1.ShowDialog(this) == DialogResult.OK)
{
this.ActiveDiagram.SaveSoap(this.saveEditorDialog1.FileName);
editForm.FileName = this.saveEditorDialog1.FileName;
}
}
else
{
this.ActiveDiagram.SaveSoap(editForm.FileName);
}
}
else if (res == DialogResult.Cancel)
{
e.Cancel = true;
}
}
Not sure how to make the correlation between the Cancel close event and my File -> New. Any help is greatly appreciated. Thank you.
EDIT: Added my EditForm_Closing Event.
Try replacing your main form's code with the following:
if (editForm != null) {
// try closing existing Editor form
editForm.Close();
if(!editForm.IsDisposed) // close was canceled.
return;
}
// Open new form
editForm = new EditorForm(this);
// Close Form Events
editForm.FormClosing += new FormClosingEventHandler('suitable method here');
editForm.Show();
editForm.Focus();
Your Closing event handler should set the editForm property back to null. So check it like this:
if (editForm != null) {
editForm.Close();
if (editForm != null) return; // Close was cancelled
// etc..
}
Or just use a private boolean member.