Please help me understand how Events and Delegates work together.

N

Nicky Smith

Hello,

I'm reading Mike Gunderloy's Mcad Vb.net book, and I've also read the
MS press Mcad book for the same topic ".. Windows based applications
with VB.net" for exam 70-306.

In the sections in both books that try to teach the use of delagates
and events, I'm really lost, and to make matters worse, I've written a
user-control that fires events for the host form, and this works
without delegates!

A little background:

I've created and compiled a user-control DLL that has 3 vertical track
bars that each represent a color (r,g,b). The min and max values for
each track bar are 0 and 255. The goal is to move the track bars in
the usercontrol and the background color of a label on the host form
is changed according to the new values.

This is the code from the Usercontrol (with the three trackbars):

'Function that returns a color defined by the values of the 3
trackbars:
Private Function fColorChanged() As System.Drawing.Color
fColorChanged = Color.FromArgb(Me.tbRed.Value,
Me.tbGreen.Value, Me.tbBlue.Value)
End Function


'public event that is visible to the host app, and has a color as
an arguement
Public Event eColorChanged(ByVal newColor As System.Drawing.Color)

'Each trackbar Rasies the eColorChanged event from its own scroll
event
Private Sub tbBlue_Scroll(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles tbBlue.Scroll
'When the eColorChanged event is raised, it calls the function
fColorChanged to convert
'the 3 integer values of the trackbar to a color and this is
made available to the host
'recieving the event:
RaiseEvent eColorChanged(fColorChanged)
End Sub


=== The following code is from the host form that houses the user
control and a Label.

'Declare an event handler for the ColorMixer1_eColorChanged Event
'(ColorMixer1 is the usercontrol)
Private Sub ColorMixer1_eColorChanged(ByVal newColor As
System.Drawing.Color) Handles ColorMixer1.eColorChanged
Me.Label1.BackColor = newColor
End Sub

All this code works like a charm, and I haven't used the Delegate or
AddressOf statements at all.

So please, can someone explain to me why this is (probably) not the
correct way to create and handle events, where delegates come in to
the picture, and what's the point of complicating matters with
delegates, when the above code seems to do the job.

-Nick
 
H

Herfried K. Wagner [MVP]

Nicky,

Nicky Smith said:
'public event that is visible to the host app, and has a color as
an arguement
Public Event eColorChanged(ByVal newColor As System.Drawing.Color)

'Each trackbar Rasies the eColorChanged event from its own scroll
event
Private Sub tbBlue_Scroll(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles tbBlue.Scroll
'When the eColorChanged event is raised, it calls the function
fColorChanged to convert
'the 3 integer values of the trackbar to a color and this is
made available to the host
'recieving the event:
RaiseEvent eColorChanged(fColorChanged)
End Sub


=== The following code is from the host form that houses the user
control and a Label.

'Declare an event handler for the ColorMixer1_eColorChanged Event
'(ColorMixer1 is the usercontrol)
Private Sub ColorMixer1_eColorChanged(ByVal newColor As
System.Drawing.Color) Handles ColorMixer1.eColorChanged
Me.Label1.BackColor = newColor
End Sub

All this code works like a charm, and I haven't used the Delegate or
AddressOf statements at all.

So please, can someone explain to me why this is (probably) not the
correct way to create and handle events, where delegates come in to
the picture, and what's the point of complicating matters with
delegates, when the above code seems to do the job.

In most cases, you don't have to deal with delegates in VB.NET when
raising/handling events. VB.NET will do the stuff for you. If you declare
an event in VB.NET, a delegate type will be generated automatically, and
using
'Handles...' will add a delegate to the list of delegates invoked by the
array automatically.

Visual Basic Language Concepts -- Events and Delegates
<URL:http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconEventsDelegates
Inheritance.asp>
 
C

Cor Ligthert

Herfried,

I see more and more broken links from you.

Just to make you attent on it

:)

Cor
 
H

Herfried K. Wagner [MVP]

Cor,

Cor Ligthert said:
I see more and more broken links from you.

Just to make you attent on it

:) -- I know that I am currently posting wrapped links. OE6 SP1 has
problems with links enclosed in '<URL:'...'>' (even with the Mondo patch
installed), and on my machine SP2 cannot be installed because of lack of
free space on the system disk ;-))).
 
L

Larry Serflaten

Herfried K. Wagner said:
:) -- I know that I am currently posting wrapped links. OE6 SP1 has
problems with links enclosed in '<URL:'...'>' (even with the Mondo patch
installed), and on my machine SP2 cannot be installed because of lack of
free space on the system disk ;-))).

Is your Plain Text Settings set to allow long lines?

Ex: Tools > Options > Send > News Sending Format :

Select the Plain Text option and set your Plain Text Settings to wrap
at some large number, like 128....

HTH
LFS
 
H

Herfried K. Wagner [MVP]

Larry Serflaten said:
Is your Plain Text Settings set to allow long lines?

Ex: Tools > Options > Send > News Sending Format :

Select the Plain Text option and set your Plain Text Settings
to wrap at some large number, like 128....

That would work, but then I'll have to wrap longer lines by hand...
 
L

Larry Serflaten

Herfried K. Wagner said:
That would work, but then I'll have to wrap longer lines by hand...


Do you mean you'd have to press the Enter key?

<g>
LFS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top