Definition and Use of Events
Visual J# enables you to register for events published by .NET Framework classes. You can register event handlers using the AddValueChanged and RemoveValueChanged event accessors as shown in the following code example.
import System.Windows.Forms.*;
public class MyClass
{
public static void main(String [] args)
{
System.Windows.Forms.Button button =
new System.Windows.Forms.Button();
MyClass myClass = new MyClass();
System.EventHandler eventhandler =
new System.EventHandler(myClass.OnDoubleClick);
button.add_DoubleClick(eventhandler);
// OnDoubleClick is a method defined in the current class
// that has the same signature as the EventHandler delegate.
// The code above demonstrates how to use .NET Framework
// delegates in Visual J#.
// Similarly, you can unsubscribe to the event as follows:
button.remove_DoubleClick(eventhandler);
}
private void OnDoubleClick(System.Object sender, System.EventArgs e)
{
}
}
There is support for defining .NET Framework events in Visual J#.
The format of an event declaration is as follows:
/** @event */
void add_EventName(event_type event)
/** @event */
void remove_EventName(event_type event)
In this example, event_type
is a parameter whose type defines the type of the event and that must be derived from Delegate. The add
and remove
methods for an event must both either be present or absent and have the same access modifiers.
It is possible to override the event accessor methods in a derived class, as follows:
import System.*;
import System.Collections.ArrayList;
/** @delegate */
public delegate void MyDelegate(); // delegate declaration
public interface I
{
/** @event */
public void add_MyEvent(MyDelegate listener);
/** @event */
public void remove_MyEvent(MyDelegate listener);
void FireAway();
}
public class MyClass implements I
{
private ArrayList list = new ArrayList(10);
private int no = 0;
/** @event */
public void add_MyEvent(MyDelegate listener)
{
list.Add(listener);
}
/** @event */
public void remove_MyEvent(MyDelegate listener)
{
list.Remove(listener);
}
public void FireAway()
{
System.Object [] toArray = list.ToArray();
int len = toArray.length;
for (int i = 0;i < len ;i++)
{
((MyDelegate)(toArray[i])).Invoke();
}
}
}
public class MainClass
{
static public void main (String [] args)
{
new MainClass();
}
public void f1()
{
Console.WriteLine("This is called when the event fires.");
}
public MainClass()
{
I i = new MyClass();
i.add_MyEvent(new MyDelegate(this.f1));
i.FireAway();
}
}
The previous code example produces the following output:
This is called when the event fires.
If the previous program were compiled as a DLL, its event could be accessed from a Visual C# program in the following example:
Example 1
In this example, MainClass
enables MyDelegate
to run the private method f()
.
// vjc_events3.jsl
// compile with: /t:library /out:VJC_Events3.dll
import System.*;
import System.Collections.ArrayList;
/** @delegate */
public delegate void MyDelegate(); // delegate declaration
public interface I
{
/** @event */
public void add_MyEvent(MyDelegate listener);
/** @event */
public void remove_MyEvent(MyDelegate listener);
void FireAway();
}
public class MyClass implements I
{
private ArrayList list = new ArrayList(10);
private int no = 0;
/** @event */
public void add_MyEvent(MyDelegate listener)
{
list.Add(listener);
}
/** @event */
public void remove_MyEvent(MyDelegate listener)
{
list.Remove(listener);
}
public void FireAway()
{
System.Object[] toArray = list.ToArray();
int len = toArray.length;
for (int i = 0; i < len; i++)
{
((MyDelegate)(toArray[i])).Invoke();
}
}
}
// vjc_events4.cs
// compile with: /reference:vjc_events3.dll
using System;
public class MainClass
{
static private void f()
{
Console.WriteLine("This is called when the event fires.");
}
static public void Main ()
{
I i = new MyClass();
i.MyEvent += new MyDelegate(f);
i.FireAway();
}
}
Output
This is called when the event fires.
See Also
Reference
Syntax for Targeting the .NET Framework
Property and Event Exposure