473,698 Members | 2,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Valu e, Me.tbBlue.Value )
End Function
'public event that is visible to the host app, and has a color as
an arguement
Public Event eColorChanged(B yVal newColor As System.Drawing. Color)

'Each trackbar Rasies the eColorChanged event from its own scroll
event
Private Sub tbBlue_Scroll(B yVal sender As System.Object, ByVal e
As System.EventArg s) 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(f ColorChanged)
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_eCo lorChanged Event
'(ColorMixer1 is the usercontrol)
Private Sub ColorMixer1_eCo lorChanged(ByVa l newColor As
System.Drawing. Color) Handles ColorMixer1.eCo lorChanged
Me.Label1.BackC olor = 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 1768
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.Valu e, Me.tbBlue.Value )
End Function
'public event that is visible to the host app, and has a color as
an arguement
Public Event eColorChanged(B yVal newColor As System.Drawing. Color)

'Each trackbar Rasies the eColorChanged event from its own scroll
event
Private Sub tbBlue_Scroll(B yVal sender As System.Object, ByVal e
As System.EventArg s) 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(f ColorChanged)
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_eCo lorChanged Event
'(ColorMixer1 is the usercontrol)
Private Sub ColorMixer1_eCo lorChanged(ByVa l newColor As
System.Drawing. Color) Handles ColorMixer1.eCo lorChanged
Me.Label1.BackC olor = 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(B yVal newColor As System.Drawing. Color)

'Each trackbar Rasies the eColorChanged event from its own scroll
event
Private Sub tbBlue_Scroll(B yVal sender As System.Object, ByVal e
As System.EventArg s) 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(f ColorChanged)
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_eCo lorChanged Event
'(ColorMixer1 is the usercontrol)
Private Sub ColorMixer1_eCo lorChanged(ByVa l newColor As
System.Drawing. Color) Handles ColorMixer1.eCo lorChanged
Me.Label1.BackC olor = 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*******@usin ternet.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*******@usin ternet.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
1517
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 Web Service, by adding the appropriate and attributes. When I try to build the Project, I get an error stating the the delegate has no constructor.
2
9181
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 for those objects. In fact, all the programmer has to worry about is the total sum of objects loaded into RAM at any known point. Memory leaks are not a problem. .... So one would like to think. The reality is that delegates and event...
3
2609
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
11
1869
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 mean, for one, delegates point to subs, so when you call a delegate, why not just call the sub dierectly and not bother adding the extra code involved adding the delegate?
1
9634
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 and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
30
3638
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 it seems to me that Microsoft has developed similar functionality via two keywords. I do understand that an event offers better encapsulation as the underlying delegate is private, but is that all ? -- Regards
2
2340
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 fires events when the data changes, is often the central part. Connected to these states are various observers that act on changes in data, by altering the information presented to the user, executing code and so on, each observer with its own...
3
2461
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 create a subscription to the Click event of the Button and another to the SelectedIndexChanged del dropDown.
7
3418
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 uses the "WithEvents" and events. There is another example on page 124 that shows how to use delegates to sort an array. I don't understand the difference between events and delegates. Are they redundant features? How do I decide which to use? ...
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9027
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.