473,778 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delegates and Events - Newb Help

JaD
I am trying to understand events and delegates which is confusing me.

Okay I have a user control that derives from a GroupBox called (MyPanel)

I added this:
public delegate void Collapsed();
public event Collapsed OnCollapsed;

public void CollapseIt()
{
if (OnCollapsed!=n ull)
{
this.Height = this.Height - 10;
}
}

If someone calls CollapseIt() from a form (i.e. on the click event of a
button) the height of the GroupBox decreases correctly...
i.e:
private void button1_Click(o bject sender, System.EventArg s e)
{
c.CollapseIt();
}

I add "MyPanel" at run time and attach the Handler

private void Form1_Load(obje ct sender, System.EventArg s e)
{
c = new MyPanel();
c.OnCollapsed+= new WindowsApplicat ion2.MyPanel.Co llapsed(c_OnCol lapsed);
this.Controls.A dd(c);
}
private void c_OnCollapsed()
{
MessageBox.Show ("Collapsed Fired!") //<------------- THIS NEVER
FIRES!!!!!!! ? WHY?
}

Why does my MessageBox not show even if I called CollapseIt?
Nov 16 '05 #1
5 1151
On Wed, 26 Jan 2005 12:00:43 -0500, JaD wrote:
I am trying to understand events and delegates which is confusing me.

Okay I have a user control that derives from a GroupBox called (MyPanel)

I added this:
public delegate void Collapsed();
public event Collapsed OnCollapsed;

public void CollapseIt()
{
if (OnCollapsed!=n ull)
{
this.Height = this.Height - 10;
}
}

If someone calls CollapseIt() from a form (i.e. on the click event of a
button) the height of the GroupBox decreases correctly...
i.e:
private void button1_Click(o bject sender, System.EventArg s e)
{
c.CollapseIt();
}

I add "MyPanel" at run time and attach the Handler

private void Form1_Load(obje ct sender, System.EventArg s e)
{
c = new MyPanel();
c.OnCollapsed+= new WindowsApplicat ion2.MyPanel.Co llapsed(c_OnCol lapsed);
this.Controls.A dd(c);
}
private void c_OnCollapsed()
{
MessageBox.Show ("Collapsed Fired!") //<------------- THIS NEVER
FIRES!!!!!!! ? WHY?
}

Why does my MessageBox not show even if I called CollapseIt?


Because you are never firing the event. You have created a delegate and an
event, and you even check to see if someone has added an event handler, so
now you just need to fire the event. I would change the CollapseIt method
as follows:

public void CollapseIt()
{
this.Height = this.Height - 10;
if (OnCollapsed!=n ull)
{
OnCollapsed(); //this will fire the event handler.
}
}

This change means that the control will collapse in all situations, and if
an event handler has been added for the OnCollapsed event.
--
Tom Porterfield
Nov 16 '05 #2
JaD
Thank you Tom, I think I just had an "aha" moment.

"Tom Porterfield" <tp******@mvps. org> wrote in message
news:1i******** *******@tpporte rmvps.org...
On Wed, 26 Jan 2005 12:00:43 -0500, JaD wrote:
I am trying to understand events and delegates which is confusing me.

Okay I have a user control that derives from a GroupBox called (MyPanel)

I added this:
public delegate void Collapsed();
public event Collapsed OnCollapsed;

public void CollapseIt()
{
if (OnCollapsed!=n ull)
{
this.Height = this.Height - 10;
}
}

If someone calls CollapseIt() from a form (i.e. on the click event of a
button) the height of the GroupBox decreases correctly...
i.e:
private void button1_Click(o bject sender, System.EventArg s e)
{
c.CollapseIt();
}

I add "MyPanel" at run time and attach the Handler

private void Form1_Load(obje ct sender, System.EventArg s e)
{
c = new MyPanel();
c.OnCollapsed+= new WindowsApplicat ion2.MyPanel.Co llapsed(c_OnCol lapsed);
this.Controls.A dd(c);
}
private void c_OnCollapsed()
{
MessageBox.Show ("Collapsed Fired!") //<------------- THIS NEVER
FIRES!!!!!!! ? WHY?
}

Why does my MessageBox not show even if I called CollapseIt?


Because you are never firing the event. You have created a delegate and
an
event, and you even check to see if someone has added an event handler, so
now you just need to fire the event. I would change the CollapseIt method
as follows:

public void CollapseIt()
{
this.Height = this.Height - 10;
if (OnCollapsed!=n ull)
{
OnCollapsed(); //this will fire the event handler.
}
}

This change means that the control will collapse in all situations, and if
an event handler has been added for the OnCollapsed event.
--
Tom Porterfield

Nov 16 '05 #3
Just a note about conventions in event naming.

Don't call your events "On...", such as "OnCollapse d". You should call
it "Collapsed" or "Collapsing ". Now, an event can, syntactically, have
any name you like, but there are some conventions within .NET to which
you might want to adhere:

1. Use the past tense for events that fire after the thing has taken
place. For example, the "Collapsed" event would happen after the
panel's height changed, just as Tom wrote it above.

2. Use the gerund for events that fire before the thing has taken
place. For example, you could also (or instead) create an event called
"Collapsing " that would happen just before the panel's height changed.

3. Use "On" plus the event name to write a _protected virtual void_
method that checks the event queue and fires the event if there are any
listeners:

protected virtual void OnCollapsed()
{
if (Collapsed != null)
{
Collapsed(this, System.EventArg s.Empty);
}
}

this method is useful because in here you can put any code that should
happen just after the panel is collapsed, any child classes inheriting
from your class can override it to do the same, and you don't have to
put "if (Collapsed != null)" tests everwhere you want to fire the
event.

If you look at the .NET Framework classes they all use this naming
convention and the convention of having a protected virtual method for
every event so that child classes can intercept it.

Nov 16 '05 #4
On 26 Jan 2005 10:03:02 -0800, Bruce Wood wrote:
2. Use the gerund for events that fire before the thing has taken
place. For example, you could also (or instead) create an event called
"Collapsing " that would happen just before the panel's height changed.


Yes, all good suggestions. In addition, on this one you might want to
accept as input a CancelEventArgs object, allowing someone to cancel the
event if necessary. See the common Validating and Validated events found
in System.Windows. Forms.Control as example.

Typically as well, especially for events on Controls, the sender is passed
in as input, allowing a handler to know exactly who fired the event. The
sender would be the control firing the event.
--
Tom Porterfield
Nov 16 '05 #5
JaD
Thank you, I will keep these in mind.
"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
Just a note about conventions in event naming.

Don't call your events "On...", such as "OnCollapse d". You should call
it "Collapsed" or "Collapsing ". Now, an event can, syntactically, have
any name you like, but there are some conventions within .NET to which
you might want to adhere:

1. Use the past tense for events that fire after the thing has taken
place. For example, the "Collapsed" event would happen after the
panel's height changed, just as Tom wrote it above.

2. Use the gerund for events that fire before the thing has taken
place. For example, you could also (or instead) create an event called
"Collapsing " that would happen just before the panel's height changed.

3. Use "On" plus the event name to write a _protected virtual void_
method that checks the event queue and fires the event if there are any
listeners:

protected virtual void OnCollapsed()
{
if (Collapsed != null)
{
Collapsed(this, System.EventArg s.Empty);
}
}

this method is useful because in here you can put any code that should
happen just after the panel is collapsed, any child classes inheriting
from your class can override it to do the same, and you don't have to
put "if (Collapsed != null)" tests everwhere you want to fire the
event.

If you look at the .NET Framework classes they all use this naming
convention and the convention of having a protected virtual method for
every event so that child classes can intercept it.

Nov 16 '05 #6

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

Similar topics

4
1784
by: Marty McDonald | last post by:
It is still unclear to me why we would use events when delegates seem to do just fine. People say that events make it so the publisher doesn't need to know about the listeners. What does that mean? Why are events better than delegates? Thanks
0
375
by: Steven Brown | last post by:
I'm trying to figure out how to safely use .NET events/delegates in a thread-safe class. There are a couple problems. One is that the standard "if(EventName != null) EventName(...);" call can fail if the event is emptied of all methods between the two statements, implying that some sort of synchronization between this and removals from EventName is needed. The other problem is that if an event with a set of delegates is in the process...
4
22891
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on when to use one over another? If anyone could provide any additional info, your comments, best practices, any good articles, specific examples, etc. Thank you
3
1596
by: Chris | last post by:
Hi, what is the difference between using events and delegates (apart from the syntax) ? have a look at following (working) programs please (you can just copy/paste and build it) : First program uses delegates, the second events but both do inherently the same :
2
1275
by: al | last post by:
Greetings, Can someone please tell me the what is the defference betweeen delegates and events handler??? MTIA, Grawsha
8
1776
by: Nicky Smith | last post by:
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!
2
2346
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...
7
3422
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? ...
9
1895
by: Scott Gifford | last post by:
I have a question about concurrency and delegates. Say I have a class roughly like this: public class Class1 { public delegate void MyEventHandler(); private event MyEventHandler onMyEvent; public event OnMyEvent
0
9465
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10296
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
10127
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
10068
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,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6723
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
5370
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...
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.