How to show Notification Message on Shutdown of Application WPF - c#

I made application in which when user will click on close button of MainWindow whole application will shutdown. I want to show a Notification after closing of application. How to show a toast message as application shuts'down?
Here Code is :
private void Close(object sender, EventArgs e)
{
base.OnClosed(e);
Application.Current.Shutdown();
}
Can any one answer my question?
Feel Free to ask if my question is not clear!

Try implementing a handler for the Window.Closing event:
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("Hi, I'm closing!");
}
This will occur before the Window.Closed event.
UPDATE >>>
#Andy and Tameen, please take a look at the Window.Closing Event page at MSDN to see when this event really occurs.
Occurs directly after Close is called, and can be handled to cancel window closure.
UPDATE 2 >>>
Your question does not state that you want to cancel the Close event. However, that is exactly what the Closing event is for:
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("Hi, I'm closing!");
e.Cancel = true;
}

Your application shutdowns by default when the mainwindow closes. Allow the button to signal the window close and handle the toast in the Closed event.
<Window ... Closed="Window_Closed" />
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void Window_Closed(object sender, EventArgs e)
{
MessageBox.Show("Cya");
}

Related

C# Is it possible to convert EventArgs to FormClosingEventArgs when calling method in a method?

I've made a form closing event when X is pressed, but I also want the 'Exit' button to call this same method yet it draws me error every time I change stuff.
--- This code below is the form closing event ---
// if user pressed 'Exit' button or red cross
private void TempConverterForm1_FormClosing(object sender, FormClosingEventArgs e) {
DialogResult exitdialog = MessageBox.Show("Are you sure you want to quit?", "Quit?", MessageBoxButtons.YesNoCancel);
if (exitdialog == DialogResult.Yes) {
e.Cancel = false;
}
else {
e.Cancel = true;
}
}
--- This code below is the code I'm trying to solve ---
// if the 'exit' button is pressed
private void btn_Exit_Click(object sender, EventArgs e) {
TempConverterForm1_FormClosing(sender, (FormClosingEventArgs) e);
}
I've tried without FormClosingEventArgs first but on itself it says that EventArgs can't be converted to closing event. I put FormClosingEventArgs but now it tries to convert from MouseEventArgs to FormClosingEventArgs even though I relate to button click and not mouse click.
I tried to do research but the problem repeats and builds up with different error messages and I got lost and decided I need help with this.
Just do this.Close() in btn_Exit_Click. This will fire Form_Closing correctly with the right arguments, and your cancel will still work.
private void btn_Exit_Click(object sender, EventArgs e)
{
this.Close();
}

Action when Form is closed c#

I'm in College and this is my first (major) project.
I'm trying to perform an action when a form is closed. I don't seem to be getting the terminology right when searching online, or the answer given doesn't match what I want to do.
At the moment i'm declaring a Class and displaying the from -
private void createuser_Click(object sender, EventArgs e)
{
User_Modification mod = new User_Modification("Create", "Create");
mod.ShowDialog();
}
What I want to do is this -
WHEN mod IS CLOSED {
// Do stuff
}
You're using ShowDialog, so the code following it is not executed until after the dialog box is closed. mod.ShowDialog(); doStuff(); will work pretty well.
You need to create a handler to capture the FormClosed event:
In your constructor do:
this.FormClosed += Form_Closed;
Then in the body of your form, add this method.
private void Form_Closed(object sender, FormClosedEventArgs e)
{
// Do stuff
}
You should attach handler to FormClosed event:
private void createuser_Click(object sender, EventArgs e)
{
User_Modification mod = new User_Modification("Create", "Create");
mod.FormClosed += new FormClosedEventHandler(FormClosed);
mod.ShowDialog();
}
void FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show("Closed");
}
if you're using WinForms you can override OnFormClosing event:
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
// your code...
}
You'll want to take a look at two events:
Form.FormClosing : https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing(v=vs.110).aspx
Form.FormClosed : https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosed%28v=vs.110%29.aspx
First one will allow you to perform actions prior to the form being closed completely, such as canceling the closing procedure. The second one is what you would use if you want to perform actions after the form is closed (perhaps to clean up resources, as an example).
So, as an example, let's say that you want to perform an action when the form is in fact closed:
// Somewhere in your code where you create the form object.
form.FormClosed += Form_FormClosed;
// Somewhere else in your code.
private void Form_FormClosed(Object sender, FormClosedEventArgs e)
{
MessageBox.Show("Form closed");
}

C# mousedown event continously press key

okay so what I want to do, it when the mouse is HELD DOWN I want it to continously press a key. it should continously press this key until I let off.
Imagine if you will, a left and right button on a windows form,
then by clicking and holding the right button, the letter "R" displays on a textbox continously until you release the button. What I am doing has very little to do with that scenario, but you get what I'm trying to do here.
What exactly do I put in the mouse down to keep the sendkeys going forever without locking up the application?
I hope my question makes sense. lol.
Thanks
RT
private void pictureBoxKeyboard_MouseDown(object sender, MouseEventArgs e)
{
//something goes here
}
This is worth a read...
http://msdn.microsoft.com/en-us/library/ms171548.aspx
SendKeys.Send("r")
This might just fire once, you may want to attach a timer that starts on the MouseDown event and stops on the MouseUp event. Then you could put the SendKeys in the Timer.Tick event.
private void Form1_Load(object sender, EventArgs e)
{
this.timer1.Interval = 500;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop();
this.Text = "moose-Up";
}
private void button1_MouseDown(object sender, EventArgs e)
{
timer1.Start();
this.Text = "moose-Down";
this.richTextBox1.Select();
}
private void timer1_Tick(object sender, EventArgs e)
{
SendKeys.Send("r");
Debug.Print("tickling");
}
Select the control that you wish to receive the SendKeys value...

FormClosed event is not firing

private void GameForm_FormClosed(object sender, FormClosedEventArgs e)
{
infform.Show();
}
GameForm has another form, infform. The form won't show when it closes. Something wrong here?
Could this help?
private void GameForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
infform.ShowDialog();
}
One thing to keep in mind... when your main form closes, the entire application exits. If you want to catch that event, you can registre to Application.ApplicationExit.
This event is fired whenever the Application exits.

ShowDialog, PropertyGrid and Timer problem

I have a strange bug, please, let me know if you have any clues about the reason.
I have a Timer (System.Windows.Forms.Timer) on my main form, which fires some updates, which also eventually update the main form UI. Then I have an editor, which is opened from the main form using the ShowDialog() method. On this editor I have a PropertyGrid (System.Windows.Forms.PropertyGrid).
I am unable to reproduce it everytime, but pretty often, when I use dropdowns on that property grid in editor it gets stuck, that is OK/Cancel buttons don't close the form, property grid becomes not usable, Close button in the form header doesn't work.
There are no exceptions in the background, and if I break the process I see that the app is doing some calculations related to the updates I mentioned in the beginning.
What can you recommend? Any ideas are welcome.
What's happening is that the thread timer's Tick method doesn't execute on a different thread, so it's locking everything else until it's done. I made a test winforms app that had a timer and 2 buttons on it whose events did this:
private void timer1_Tick(object sender, EventArgs e)
{
Thread.Sleep(6000);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
frmShow show = new frmShow();
show.ShowDialog(); // frmShow just has some controls on it to fiddle with
}
and indeed it blocked as you described. The following solved it:
private void timer1_Tick(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(DoStuff);
}
private void DoStuff(object something)
{
Thread.Sleep(6000);
}

Categories