tags:

views:

54

answers:

2

So I am a Visual Basic 6 developer moving over to CLR and need an answer to a real newb question.

In Visual Basic 6, if you had an ActiveX control, you could reference that control and then have a statement essentially saying:

Public WithEvents YourControl (Emphasis on the "WithEvents" keyword)

What happen is that in Visual Basic you would then have a set of event handler functions for that control. You could simply choose them from a drop down list and an empty handler would be generated.

I am looking for the equivalent of that in VS2008 (Winform). How do I access that functionality in the Visual Studio 2008 IDE? I have the CLR DLL project referenced in my Winform project. I just need to know how to get access to the functions/classes of that CLR DLL.

Thanks!

A: 

In event broadcaster class:

public event EventHandler MyEvent;

void OnMyEvent()
{
    if (MyEvent != null)
        MyEvent(this, EventArgs.Empty);
}

In event reciever class:

obj.MyEvent += new EventHandler(obj_MyEvent);

void obj_MyEvent(object sender, EventArgs e)
{
    // Do something as a result of the event here
}

Syntax is in C#, VB.NET is similar

rpetrich
While the syntax for event declaration and invocation is similar, adding a delegate is not.
Adam Robinson
AddHandler ... AddressOf ... looks similar to me, just more verbose. Although, he did ask for "CLR"; maybe that means ldftn ... callvirt instance void BroadcasterClass::add_MyEvent(class System.EventHandler) ? ;)
rpetrich
+1  A: 

The answer will depend on the language you're using.

If you're using Visual Basic .NET

The syntax is actually the same. In all .NET languages, events are simply delegates, which are basically function pointers that can point to zero or more functions of identical signatures.

The WithEvents keyword isn't absolutely necessary to handle events in VB.NET, but it does provide the developer with the ability to use legacy VB-style event handlers (and this is how the designer does it, as well). It can be done just as you're familiar with, like this

Private WithEvents YourControl as Control

Private Sub ClickFunction(ByVal sender as Object, ByVal e as EventArgs) Handles YourControl.Click
    'do things
End Sub

To handle an event on an object without the WithEvents keyword, use AddHandler

AddHandler YourControl.Click, AddressOf ClickFunction

Private Sub ClickFunction(ByVal sender as Object, ByVal e as EventArgs)
    'do things
End Sub

If you're interested in viewing the events in the combobox at the top of the editor, all you need is the variable to be declared with the WithEvents keyword. Just locate the variable you're interested in in the menu on the left, then the events should appear in the menu on the right.

If you're using C# .NET

There isn't any functionality like this in C#. All event handlers must be attached explicitly, like this

private Control YourControl;

private void ClickFunction(Object sender, EventArgs e)
{
    // do stuff
}

// to attach, this goes somewhere in code

//...
YourControl.Click += ClickFunction;
//...
Adam Robinson