How do you raiseevent ?

When you create a custom control from a canvas, container control or some other base class its not unusual to use raise event to create new events like “BeforeButtonPressed”, “AfterButtonPressed” or to just make it so you;r control can implement normal events like Open, Close, etc and pass then along.

You’ll see code using raiseevent to cause these events to be raised on any instances.

But, you can use raiseevent for much more than this and use it in places you might not expect.

For instance, you might want an event to return a boolean as well as some parameter byref much like the SelectColor method does.

Dim c As Color

If SelectColor(c) Then
  // use the color selected
Else
  // c is still "default"
End If

This isnt so bad and it easy to imagine writing the SelectColor method to do exactly this. But what if SelectColor is an event in a custom control you’re writing ? You might be tempted to write

Dim c As Color

Dim b As Boolean = SelectColor(c)

If b Then
  // use the color selected
Else
  // c is still "default"
End If

But in a way this hides the fact that SelectColor IS an event your code is calling for an instance to implement. Believe it or not you CAN write

Dim c As Color

Dim b As Boolean = RaiseEvent SelectColor(c)

If b Then
  // use the color selected
Else
  // c is still "default"
End If

And this then is more obvious that SelectColor IS an event that an instance might have implemented. But, since you can write that why not

Dim c As Color = &cffffff

If RaiseEvent SelectColor(c) Then
  // use the color selected
Else
  // c is still "default"
End If

And once again this works just as we expect. And it helps make your code clearer that you ARE calling an event that an instance may have implemented. And, code clarity is a good thing.

I think if you try you might be surprised at the spots where putting RaiseEvent works exactly as expected and helps improve code clarity.