ok I wrote an extended sample for you
make a new project, add 2 buttons to it. Copy this inside the Form1 class:
PHP Code:
private MySampleClass test = new MySampleClass();
private void Form1_Load(object sender, System.EventArgs e)
{
// Add event handlers
test.CoolChanged += new MySampleClass.IsCoolChangedEventHandler(test_CoolChanged);
test.FooWasCalled += new EventHandler(test_FooWasCalled);
}
// has to have the signature of a System.EventHandler delegate
private void test_FooWasCalled (object sender, EventArgs e)
{
MessageBox.Show ("function Foo of the test class was called!!!");
}
// has to have the signature of a MySampleClass.IsCoolChangedEventHandler delegate
private void test_CoolChanged (MySampleClass sender, bool newCoolValue, object dummy)
{
MessageBox.Show ("Field IsCool of the test class was called and the new value is: " +
newCoolValue.ToString() );
}
this is a test class with events. paste this to a new file or outside the form1 class:
PHP Code:
public class MySampleClass
{
// An event that uses a generic event handler (therefore we use System.EventHandler
/// <summary>
/// Raised when the Foo() function of this class is called
/// </summary>
public event EventHandler FooWasCalled;
// We need a custom delegate to handle out special event
// This events parameters are customized! yay :D
public delegate void IsCoolChangedEventHandler (MySampleClass sender, bool newCoolValue, object dummy);
/// <summary>
/// Raised when the IsCool field of the class is changed.
/// </summary>
public event IsCoolChangedEventHandler CoolChanged;
private bool isCool = false;
public bool IsCool
{
get
{
return isCool;
}
set
{
isCool = value;
// value changed, raise the event
if (CoolChanged!=null)
CoolChanged (this, value, null);
}
}
/// <summary>
/// Does some stuff then raises the FooWasCalled event
/// </summary>
public void Foo()
{
/*
* do stuff
*/
// If there are any event handlers attached to this event, then
// the value will NOT be null, that's why we have to check for
// a null reference first
if (this.FooWasCalled != null)
{
// the sytax for a System.EventHandler delegate is this:
// (object sender, EventArgs e)
// So I'm passing the class as the sender, and I'm not
// passing anything as the eventargs
FooWasCalled (this, null);
}
}
}
also note that if you wanna get really fancy in C#, you can use events as properties (most people dont know this). For example
//do other stuff you want
}
// remove event handler
remove
{
myEvent -= value;
//do other stuff you want
}
}
let me know if you need explanations and rate if it helps
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB ) VB.NET to C# conversion tips!!
Just note, Bombdrop, that using the EventHandler<TEventArgs> doesn't mean you don't have to USE a delegate. It means you don't have to DECLARE a delegate. EventHandler<TEventArgs> is itself a delegate.