473,320 Members | 1,896 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,320 software developers and data experts.

Delegates?

There are 2 ways to hook an event to a method. For example, to hook the
MouseEnter-event, you can use

MouseEnter+=OnMouseEnter;

or

MouseEnter+=new System.EventHandler(OnMouseHandler);

Both work well, as demonstrated by the small program below, where the
MouseEnter-event is hooked the one way, the MouseLeave-event the other. What
is the preferred way to hook events to methods, and why?
//class Formclass Form:System.Windows.Forms.Form{ //data member label
System.Windows.Forms.Label label=new System.Windows.Forms.Label();
//constructor Form() { Controls.Add(label); MouseEnter+=OnMouseEnter;
MouseLeave+=new System.EventHandler(OnMouseLeave); } //OnMouseEnter void
OnMouseEnter(System.Object a,System.EventArgs b) {
label.Text="MouseEntered"; } //OnMouseLeave void OnMouseLeave(System.Object
a,System.EventArgs b) { label.Text="MouseLeft"; } //Main [System.STAThread]
static void Main() { System.Windows.Forms.Application.Run(new Form()); }}
Nov 21 '06 #1
7 1960
óeps, Linux style ;-) Here is the correct program:


//class Form
class Form:System.Windows.Forms.Form
{
//data member label
System.Windows.Forms.Label label=new System.Windows.Forms.Label();
//constructor
Form()
{
Controls.Add(label);
MouseEnter+=OnMouseEnter;
MouseLeave+=new System.EventHandler(OnMouseLeave);
}
//OnMouseEnter
public void OnMouseEnter(System.Object a,System.EventArgs b)
{
label.Text="MouseEntered";
}
//OnMouseLeave
public void OnMouseLeave(System.Object a,System.EventArgs b)
{
label.Text="MouseLeft";
}
//Main
[System.STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}


Nov 21 '06 #2
Hi Martijn,

The compiler simply checks that the signature for the OnMouseEnter method in
your first example matches the signature for the System.EventHandler
delegate required by the MouseEnter event and swaps it with a call to
Delegate.Combine, just like in the second example.

Personally, I like the first syntax better (it's new to the 2.0 framework).

--
Dave Sexton

"Martijn Mulder" <i@mwrote in message
news:45***********************@news.wanadoo.nl...
There are 2 ways to hook an event to a method. For example, to hook the
MouseEnter-event, you can use

MouseEnter+=OnMouseEnter;

or

MouseEnter+=new System.EventHandler(OnMouseHandler);

Both work well, as demonstrated by the small program below, where the
MouseEnter-event is hooked the one way, the MouseLeave-event the other.
What is the preferred way to hook events to methods, and why?
//class Formclass Form:System.Windows.Forms.Form{ //data member label
System.Windows.Forms.Label label=new System.Windows.Forms.Label();
//constructor Form() { Controls.Add(label); MouseEnter+=OnMouseEnter;
MouseLeave+=new System.EventHandler(OnMouseLeave); } //OnMouseEnter void
OnMouseEnter(System.Object a,System.EventArgs b) {
label.Text="MouseEntered"; } //OnMouseLeave void
OnMouseLeave(System.Object a,System.EventArgs b) {
label.Text="MouseLeft"; } //Main [System.STAThread] static void Main()
{ System.Windows.Forms.Application.Run(new Form()); }}

Nov 21 '06 #3
Hi Martijn, I prefer the first way. The both compile to exactly the same
thang and, IMO, the conciseness does not make it less readable. In fact,
it makes it more readable because, normally, you don't care about the delegate
type that is being instantiated it. Also, because the code in the event handlers
is so simple, I might rewrite it like this:

//class Form
class Form: System.Windows.Forms.Form
{
//data member label
System.Windows.Forms.Label label =new System.Windows.Forms.Label();

//constructor
Form()
{
Controls.Add(label);
MouseEnter += delegate { label.Text = "MouseEntered"; };
MouseLeave += delegate { label.Text = "MouseLeft"; };
}

//Main
[System.STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 21 '06 #4
Martijn,

You didn't say what the problem was. There shouldn't be any problem because
the code generated by the c# compiler is exactly the same in both cases. It
has been added for convinience only. The preffered way is 'whatever makes
you code more readable'.

Execuse me for saying that, but please format a little your code snippets
before posting them. It is unreadable.
--
HTH
Stoitcho Goutsev (100)

"Martijn Mulder" <i@mwrote in message
news:45***********************@news.wanadoo.nl...
There are 2 ways to hook an event to a method. For example, to hook the
MouseEnter-event, you can use

MouseEnter+=OnMouseEnter;

or

MouseEnter+=new System.EventHandler(OnMouseHandler);

Both work well, as demonstrated by the small program below, where the
MouseEnter-event is hooked the one way, the MouseLeave-event the other.
What is the preferred way to hook events to methods, and why?
//class Formclass Form:System.Windows.Forms.Form{ //data member label
System.Windows.Forms.Label label=new System.Windows.Forms.Label();
//constructor Form() { Controls.Add(label); MouseEnter+=OnMouseEnter;
MouseLeave+=new System.EventHandler(OnMouseLeave); } //OnMouseEnter void
OnMouseEnter(System.Object a,System.EventArgs b) {
label.Text="MouseEntered"; } //OnMouseLeave void
OnMouseLeave(System.Object a,System.EventArgs b) {
label.Text="MouseLeft"; } //Main [System.STAThread] static void Main()
{ System.Windows.Forms.Application.Run(new Form()); }}

Nov 21 '06 #5
Hi Stoitcho

:-) Thank you, I'll try to format a little. Also thanks for other posts that
you answered in a most helpfull way.

//class Form
class Form:System.Windows.Forms.Form
{
//data member label
System.Windows.Forms.Label label=new System.Windows.Forms.Label();
//constructor
Form()
{
Controls.Add(label);
MouseEnter+=OnMouseEnter;
MouseLeave+=new System.EventHandler(OnMouseLeave);
}
//OnMouseEnter
public void OnMouseEnter(System.Object a,System.EventArgs b)
{
label.Text="MouseEntered";
}
//OnMouseLeave
public void OnMouseLeave(System.Object a,System.EventArgs b)
{
label.Text="MouseLeft";
}
//Main
[System.STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}

Martijn,

You didn't say what the problem was. There shouldn't be any problem
because the code generated by the c# compiler is exactly the same in both
cases. It has been added for convinience only. The preffered way is
'whatever makes you code more readable'.

Execuse me for saying that, but please format a little your code snippets
before posting them. It is unreadable.
--
HTH
Stoitcho Goutsev (100)

"Martijn Mulder" <i@mwrote in message
news:45***********************@news.wanadoo.nl...
>There are 2 ways to hook an event to a method. For example, to hook the
MouseEnter-event, you can use

MouseEnter+=OnMouseEnter;

or

MouseEnter+=new System.EventHandler(OnMouseHandler);

Both work well, as demonstrated by the small program below, where the
MouseEnter-event is hooked the one way, the MouseLeave-event the other.
What is the preferred way to hook events to methods, and why?
//class Formclass Form:System.Windows.Forms.Form{ //data member label
System.Windows.Forms.Label label=new System.Windows.Forms.Label();
//constructor Form() { Controls.Add(label); MouseEnter+=OnMouseEnter;
MouseLeave+=new System.EventHandler(OnMouseLeave); } //OnMouseEnter void
OnMouseEnter(System.Object a,System.EventArgs b) {
label.Text="MouseEntered"; } //OnMouseLeave void
OnMouseLeave(System.Object a,System.EventArgs b) {
label.Text="MouseLeft"; } //Main [System.STAThread] static void Main()
{ System.Windows.Forms.Application.Run(new Form()); }}


Nov 21 '06 #6
Also, one can consider doing the GUI in VB.Net. They're similar enough
that a coder should have no problem jumping from one to the other, and
if you're writing your GUI as a separate project from your logic, then
they can be in different languages anyways. VB.Net has the "Handles"
syntactic sugar that is very very nice.

Advantages of "Handles" (1) afaik, it's tied to the reference, not the
object, so if the reference changes to point to another object, it
automatically handles removing the handler from the old object and
adding it to the new object. (2) Since declaration and handling are all
in one line (the function declaration) you can just delete the
declaration when you accidentally create new handler code after
accidentally doubleclicking on a widget, rather than having to crawl
into the autogenerated code to find that += statement.

Martijn Mulder wrote:
There are 2 ways to hook an event to a method. For example, to hook the
MouseEnter-event, you can use

MouseEnter+=OnMouseEnter;

or

MouseEnter+=new System.EventHandler(OnMouseHandler);

Both work well, as demonstrated by the small program below, where the
MouseEnter-event is hooked the one way, the MouseLeave-event the other. What
is the preferred way to hook events to methods, and why?
//class Formclass Form:System.Windows.Forms.Form{ //data member label
System.Windows.Forms.Label label=new System.Windows.Forms.Label();
//constructor Form() { Controls.Add(label); MouseEnter+=OnMouseEnter;
MouseLeave+=new System.EventHandler(OnMouseLeave); } //OnMouseEnter void
OnMouseEnter(System.Object a,System.EventArgs b) {
label.Text="MouseEntered"; } //OnMouseLeave void OnMouseLeave(System.Object
a,System.EventArgs b) { label.Text="MouseLeft"; } //Main [System.STAThread]
static void Main() { System.Windows.Forms.Application.Run(new Form()); }}
Nov 21 '06 #7
Advantages of "Handles" ..SNIP..

Disadvantage of "Handles":

If you want your event handlers to activate *after* you set all your
design time values, so they don't fire while initializing, you still
have to use the syntax where you "add" the function to the event.

Not a very big problem for us C# programmers, but as I have experienced,
it tends to confuse VB programmers to no end.

The C# Forms Designer, by the way (as you probably all know), always
adds the handler to the event *after* design time property
initialization of its controls. Default behavior in that respect differs
from the VB way using "Handles". I personally definitely prefer the C# way.

David
Nov 21 '06 #8

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

Similar topics

6
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the...
3
by: Sam | last post by:
I’m just starting to learn delegates. I’m at the very beginning. If I understand correctly, delegates are for when you want to pass a function as a parameter. For example the client provides a...
4
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...
4
by: AMDRIT | last post by:
I am trying to understand Delegates and where/when to use them. I can see one potential use of a delegate (on form closing, set the cancel property in the event arguments.) Does anyone have a...
6
by: =?Utf-8?B?Sko=?= | last post by:
I have a logger component that logs to multiple sources, ie textfile, eventlog etc. and I have two methods that depending on where I call up my logger comp. one of them will be called. For ex. if...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
6
by: =?Utf-8?B?T2xkQ2FEb2c=?= | last post by:
My question is regarding the use of delegates in C#. I see how .Net uses delegates to wire event handlers to events. It’s an object created by a single line of code by the system and that makes...
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...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.