OnClosing

Z

Zach

Could someone please give me a code example, including the text of the
handler, for the method containing an instruction when the form is closed?

I have found references on the internet to code for the method, but I
get errors. So please be kind enough to answer both questions i.e. about
the handler and the method per se, rather than having a discussion about
the error messages.

Many thanks,
Zach
 
P

Patrice

Could someone please give me a code example, including the text of the
handler, for the method containing an instruction when the form is closed?

Always try the doc that comes generally with samples :
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onclosing.aspx
or
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx

(not sure if you are looking for the method or the event it raises).
I have found references on the internet to code for the method, but I get
errors. So please be kind enough to answer both questions i.e. about the
handler and the method per se, rather than having a discussion about the
error messages.

This is almost always a bad strategy. For example in the doc mentionned
above you'll see that the method is obsoleted and should be replaced by
OnFormClosing/FormClosing. If this is the case whatever the sample you'll
use, you'll always have this warning until you use the new method/event....
 
Z

Zach

Always try the doc that comes generally with samples :
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onclosing.aspx

or
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx


(not sure if you are looking for the method or the event it raises).


This is almost always a bad strategy. For example in the doc mentionned
above you'll see that the method is obsoleted and should be replaced by
OnFormClosing/FormClosing. If this is the case whatever the sample
you'll use, you'll always have this warning until you use the new
method/event....
I found references but got into problems applying them. Hence my
questions for sample code, and not for a reference, i.e., for:

(1.) the handling line
(2.) the method

Simply just that.

Both applicable to C#.

Could you please oblige?

Many thanks,
Zach.
 
P

Patrice

I found references but got into problems applying them. Hence my questions
for sample code, and not for a reference, i.e., for:

(1.) the handling line
(2.) the method

Simply just that.

Both applicable to C#.

Could you please oblige?

As I said earlier you have sample code in the doc. So based on the sample
from
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx
it would give something like :

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
FormClosing += Form1_FormClosing;
}
private void Form1_FormClosing(Object sender, FormClosingEventArgs
e)
{
System.Text.StringBuilder messageBoxCS = new
System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "CloseReason",
e.CloseReason);
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Cancel", e.Cancel);
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "FormClosing Event");
}
}
}


If this is not what you are llking for, please be a bit more explicit... If
you still have a problem, you'll need to tell us what it is...
 
Z

Zach

As I said earlier you have sample code in the doc. So based on the
sample from
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx
it would give something like :

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
FormClosing += Form1_FormClosing;
}
private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{
System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "CloseReason", e.CloseReason);
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Cancel", e.Cancel);
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "FormClosing Event");
}
}
}


If this is not what you are llking for, please be a bit more explicit...
If you still have a problem, you'll need to tell us what it is...
Very good, many thanks.
Regards,
Zach
 
H

harborsparrow

Here's some boilerplate that I use in Winapps:

636: /// <summary>
637: /// Saves ContainerForm location when it is about to
close.
638: /// </summary>
639: /// <param name="sender"></param>
640: /// <param name="e"></param>
641: private void ContainerForm_FormClosing(object sender,
FormClosingEventArgs e)
642: {
643: String stringMe = "ContainerForm_FormClosing: ";
644: try
645: {
646: LogFile.WriteLine(stringMe + "starting");
647: if
(this.WindowState.Equals(FormWindowState.Maximized))
648: {
649: Model.FormContainerMaximized = true;
650: }
651: else
652: {
653: Model.CurrentCode =
this.mySearchPane.CurrentCode;
654: Model.FormContainerMaximized = false;
655: Model.FormContainerTop = this.Top;
656: Model.FormContainerLeft = this.Left;
657: Model.FormContainerWidth = this.Width;
658: Model.FormContainerHeight = this.Height;
659: }
660: }
661: catch (Exception ex)
662: {
663: LogFile.WriteLine(stringMe + "ERROR - " +
ex.Message);
664: throw;
665: }
666: }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top