473,405 Members | 2,262 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,405 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 1964
ó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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
0
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...
0
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...

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.