473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Please help me understand how Events and Delegates work together.


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
Nov 21 '05 #1
8 1735
Nicky,

There is written a lot about your question in this newsgroup.

In this link you find that
http://tinyurl.com/6m6qu

I hope this helps

Cor

"Nicky Smith" <ni***@yahoo.dk>
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

Nov 21 '05 #2
Nicky,

"Nicky Smith" <ni***@yahoo.dk> schrieb:
'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...ventsDelegates
Inheritance.asp>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #3
Herfried,

I see more and more broken links from you.

Just to make you attent on it

:-)

Cor
Nov 21 '05 #4
Cor,

"Cor Ligthert" <no************@planet.nl> schrieb:
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 ;-))).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #5

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote
I see more and more broken links from you.


:-) -- 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
Nov 21 '05 #6
"Larry Serflaten" <se*******@usinternet.com> schrieb:
:-) -- 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....


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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #7

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote
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...

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

<g>
LFS
Nov 21 '05 #8
"Larry Serflaten" <se*******@usinternet.com> schrieb:
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...


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


Counting characters...

<g>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Darshan Mehta | last post by:
Hello, I have created a class with custom events. The delegate for the event is created before the class is declared. This event works fine when deployed locally. I convertied the Class into a...
2
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
11
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
2
by: kristian.freed | last post by:
Hi, I currently work in a project written fully in C# where we make extensive use of delegates and events. We have a model where a "state", an object holding data but not much code but which...
3
by: Jose Fernandez | last post by:
Hello. I would like to know how could i get all the subscriptions that my form has with the events of their controls. For example. I have a form with a textbox, a button and a dropdown. I...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.