Category: events

Aug 04 2011

To create an event log entry in windows from the command line

I don’t know how often this would be helpful but today I needed it to solve a problem.  If you ever need to create an Windows Event Log entry you can do it from an ELEVATED Command Prompt as follows:

eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO “My Event Source” /D “Actual Log Entry”

Seth

Jun 01 2010

Is there a way to know in VB.NET if a handler has been registered for an event?

In C# I can test for this...

public event EventHandler Trigger;
protected void OnTrigger(EventArgs e)
{
    if (Trigger != null)
        Trigger(this, e);
}

Is there a way to do this in VB.NET? Test for null I mean?

MORE INFO

I forgot to mention. I have classes written in C# but I am writing my unit tests in VB.NET.

I am trying this in the unit test...

If myObject.Trigger IsNot Nothing Then  
    ''#do something
End If

This is causing a compile time error which says ... "Public Event Trigger is an Event and cannot be called directly. Use the RaiseEvent statement to raise an event."

Seth

Jun 01 2010

Is there a way to know in VB.NET if a handler has been registered for an event?

In C# I can test for this...

public event EventHandler Trigger;
protected void OnTrigger(EventArgs e)
{
    if (Trigger != null)
        Trigger(this, e);
}

Is there a way to do this in VB.NET? Test for null I mean?

MORE INFO

I forgot to mention. I have classes written in C# but I am writing my unit tests in VB.NET.

I am trying this in the unit test...

If myObject.Trigger IsNot Nothing Then  
    ''#do something
End If

This is causing a compile time error which says ... "Public Event Trigger is an Event and cannot be called directly. Use the RaiseEvent statement to raise an event."

Seth

Jun 01 2010

Is there a way to know in VB.NET if a handler has been registered for an event?

In C# I can test for this...

public event EventHandler Trigger;
protected void OnTrigger(EventArgs e)
{
    if (Trigger != null)
        Trigger(this, e);
}

Is there a way to do this in VB.NET? Test for null I mean?

MORE INFO

I forgot to mention. I have classes written in C# but I am writing my unit tests in VB.NET.

I am trying this in the unit test...

If myObject.Trigger IsNot Nothing Then  
    ''#do something
End If

This is causing a compile time error which says ... "Public Event Trigger is an Event and cannot be called directly. Use the RaiseEvent statement to raise an event."

Seth

Aug 27 2009

How do you create a cancelable event in vb.net

IN VB.NET (not c#)...

I want to create an event than can be canceled by the listener. Just like you can cancel the closing event of a winforms form in which case the form won't close.

I have already implemented a derived class from EventArgs that has a settable Cancel property as follows:

Public Class AnnounceNavigateEventArgs
    Inherits EventArgs

    Private _cancel As Boolean = False

    ''' <summary>
    ''' Initializes a new instance of the AnnounceNavigateEventArgs class.
    ''' </summary>
    Public Sub New(ByRef cancel As Boolean)
        _cancel = cancel
    End Sub
    Public Property Cancel() As Boolean
        Get
            Return _cancel
        End Get
        Set(ByVal value As Boolean)
            _cancel = value
        End Set
    End Property

End Class

Notice that I am passing the cancel argument byRef to the constructor.

The listener I have setup is setting the property to Cancel=True. I thought ByRef meant that both _cancel and cancel would point to the same location on the stack and that setting _cancel=true would therefore make the cancel = true. But this is not the behavior I am getting. _cancel becomes true in the setter but I guess the argument to the constructor remains false.

What is the proper way to do this in vb.net?

Seth

Jul 03 2009

How to expose and raise custom events for a vb.net winforms user control

Hello,

Please read THIS post. I have the same problem as described in this post but I am trying to do in in VB.net rather than c#.

I am pretty sure to do this I have to use a custom event. (I used a code conversion site to get to learn about custom events.) So in the IDE when I type the following:

Public Custom Event AddRemoveAttendees As EventHandler

It expands to the following code snippet.

Public Custom Event AddRemoveAttendees As EventHandler
    AddHandler(ByVal value As EventHandler)

    End AddHandler

    RemoveHandler(ByVal value As EventHandler)

    End RemoveHandler

    RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)

    End RaiseEvent
End Event

But I can’t figure out what to do with it. Until today I had never heard of custom events.

The bottom line of what I want is to have the click event of a button bubble up to the container of the user control. I know that I could wrap my own event but I would at least like to understand custom events before I go farther down that road.

Seth