Derive From CancelEventArgs

  • Thread starter Thread starter Gates72
  • Start date Start date
G

Gates72

Hello All,

I am looking for an example showing a custom EventArgs
class that derives from CancelEventsArgs, and a form or
control using that class in a custom event.

The simpler, the better, but I'll take anything...

Thanks,
G72
 
hi

what you want to do?
simple derive it and add your functionality. be aware that the controls will
still use CancelEventsArgs , so you will need to derive the controls you
want to fire the event with the new type.

Also I suggest you to always check the eventargs type in the handler before
doing the cast.

cheers,
 
Hi Gates72,

Here is a simple Windows-Forms example. Please copy and paste.

HTH, günther

begin....

using System;
using System.ComponentModel;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{


#region form1

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components =
null;
public event CancelEventHandler UseLessEventHandler;

public Form1()
{
InitializeComponent();
this.UseLessEventHandler += new
CancelEventHandler(Form1_UseLessEventHandler);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not
modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new
System.Windows.Forms.Button();
this.label1 = new
System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new
System.Drawing.Point(108, 74);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new
System.Drawing.Point(102, 110);
this.label1.Name = "label1";
this.label1.TabIndex = 1;
this.label1.Text = "label1";
this.label1.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// Form1
//
this.AutoScaleBaseSize = new
System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(328,
197);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion
#region MyCancelEventArgs
public class MyCancelEventArgs : CancelEventArgs
{
private object tag = null;
public MyCancelEventArgs()
{
}
public object Tag
{
get
{
return this.tag;
}
set
{
this.tag = value;
}
}
}
#endregion

private void Form1_UseLessEventHandler(object sender,
CancelEventArgs e)
{
if( MessageBox.Show( this, "Do you want to
cancel this?", "?", MessageBoxButtons.YesNo ) == DialogResult.Yes )
{
e.Cancel = true;
}
}
private void button1_Click(object sender,
System.EventArgs e)
{
MyCancelEventArgs ce = null;
if( this.UseLessEventHandler != null )
{
ce = new MyCancelEventArgs();
ce.Tag = this; // or whatever;
this.UseLessEventHandler( this, ce );

if( ce.Cancel )
{
this.label1.Text = "You
cancelled!";
}
else
{
this.label1.Text =
"braveheart";
}
}
else
{
this.label1.Text = "You just Clicked -
nothing more!";
}
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}



}
}


end ....
 
Back
Top