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.

C# delegates, events and WinForms

I have a C# Winforms app that has 5 Winforms, lets say A through E.

A: Data entry. When data is entered here in any field, values are updated on
forms C, D, E.(Not B)
B: Data entry form. When data is entered here in any field, values are
updated on forms C, D, E (not A).

I am considering using delegates to fire events from forms A & B. In forms
C, D, E I will have functions with the same signature and even the same name
that just updates the data on those readonly screens.

Being new to delegates, I am a little confused on how to do this.

Do I:
1. Create a delegate in Form A and one in Form B.
2. Create a method in Form C, D, E where I can subscribe to the delegate
from forms A & B. This could be called on form load.

This is where my confusion starts (or possibly before)...when I subscribe to
the event in forms A and B, won't I need an instance of those form objects?
I don't think having form C, D, E have instances of Form A and B are the
appropriate way of doing it.

Am I way offbase with my thinking?

STom
Nov 15 '05 #1
8 7381
How I'd do it personally is have an underlying data object that raises
events when its data is changed. This way is a lot more flexable.

Greg
"STom" <st***********@hotmail.com> wrote in message
news:ed**************@TK2MSFTNGP12.phx.gbl...
I have a C# Winforms app that has 5 Winforms, lets say A through E.

A: Data entry. When data is entered here in any field, values are updated on forms C, D, E.(Not B)
B: Data entry form. When data is entered here in any field, values are
updated on forms C, D, E (not A).

I am considering using delegates to fire events from forms A & B. In forms
C, D, E I will have functions with the same signature and even the same name that just updates the data on those readonly screens.

Being new to delegates, I am a little confused on how to do this.

Do I:
1. Create a delegate in Form A and one in Form B.
2. Create a method in Form C, D, E where I can subscribe to the delegate
from forms A & B. This could be called on form load.

This is where my confusion starts (or possibly before)...when I subscribe to the event in forms A and B, won't I need an instance of those form objects? I don't think having form C, D, E have instances of Form A and B are the
appropriate way of doing it.

Am I way offbase with my thinking?

STom

Nov 15 '05 #2
Greg,

Thanks for the answer.

I will not be sending the data to the data component until I am ready to do
a save, that way they can tinker around with the numbers all they want too,
but if they don't save, they haven't screwed up their original work.
Therefore, the data component won't come into play during the time in which
I am changing the data on the screen.

STom
"Greg Bacchus" <FB**********@spammotel.com> wrote in message
news:OP**************@TK2MSFTNGP10.phx.gbl...
How I'd do it personally is have an underlying data object that raises
events when its data is changed. This way is a lot more flexable.

Greg
"STom" <st***********@hotmail.com> wrote in message
news:ed**************@TK2MSFTNGP12.phx.gbl...
I have a C# Winforms app that has 5 Winforms, lets say A through E.

A: Data entry. When data is entered here in any field, values are updated
on
forms C, D, E.(Not B)
B: Data entry form. When data is entered here in any field, values are
updated on forms C, D, E (not A).

I am considering using delegates to fire events from forms A & B. In
forms C, D, E I will have functions with the same signature and even the same

name
that just updates the data on those readonly screens.

Being new to delegates, I am a little confused on how to do this.

Do I:
1. Create a delegate in Form A and one in Form B.
2. Create a method in Form C, D, E where I can subscribe to the delegate
from forms A & B. This could be called on form load.

This is where my confusion starts (or possibly before)...when I

subscribe to
the event in forms A and B, won't I need an instance of those form

objects?
I don't think having form C, D, E have instances of Form A and B are the
appropriate way of doing it.

Am I way offbase with my thinking?

STom


Nov 15 '05 #3
Hi Stom,

My guess is that Greg means a class of your own design that looks
after the communications regarding data changes.

But let's look at your solution - your thinking isn't as confused
as you feel.

|| I am considering using delegates to fire events from forms
|| A & B. In forms C, D, E I will have functions with the
|| same signature and even the same name that just updates
|| the data on those readonly screens.

Yes. Forms A and B will be firing events via Delegates. C, D and E
will have functions that conform. The same name isn't necessary but
makes good sense.

|| Create a Delegate in Form A and one in Form B.

Yes again.

|| Create a method in Form C, D, E where I can
|| subscribe to the Delegates from forms A & B.
|| This could be called on form load.

Yes again. And again.

|| This is where my confusion starts (or possibly before)

Well, not really and (no).

|| when I subscribe to the event in forms A and B,
|| won't I need an instance of those form objects?

Yes. Forms C, D and E will need to know who to subscribe to. So
they will need to know about A and B.

|| I don't think having form C, D, E have instances
|| of Form A and B are the appropriate way of doing it.

It's inevitable. Someone's got to know about someone to get a
connection going.

This is where Greg's idea comes back in as an alternative. By
having each Form know about his central object*, they needn't know
about each other. The object would have a port (function) through
which it receives value update information from A and B. It would have
the Delegate that C, D and E hook themselves on to. So when A or B
pass in a value, the Delegate is activated, calling C, D and E.

|| Am I way offbase with my thinking?

Oh yes. I haven't agreed with a word you've said, lol.

Regards,
Fergus

* Because there's only need for one, you could use a static class.
This makes it effectively global - so no need for passing it around.
Nov 15 '05 #4
Fergus,

Thanks a lot, that makes more sense now.

I have been tinkering around with how to do this using a winform app that
has two forms. I am having some trouble with getting the event to not be
null when the event is fired. Here is the code from this new apps

//FormA class
public delegate void SomethingChangedDelegate();
public event SomethingChangedDelegate OnMyChange;
......

[STAThread]
static void Main()
{
Application.Run(new FormA());

}

private void FormA_Load(object sender, System.EventArgs e)
{
myB = new FormB();
myB.Show();
}

private void txtNumber_TextChanged(object sender, System.EventArgs e)
{
if (OnMyChange != null)
OnMyChange();
}
//FormB class
private void FormB_Load(object sender, System.EventArgs e)
{
FormA myA = new FormA();
Subscribe(myA);
}

public void Subscribe(FormA form)
{
form.OnMyChange += new FormA.SomethingChangedDelegate(CallMeWhenChanged);
}

static void CallMeWhenChanged()
{
MessageBox.Show("I just got clicked");
}

----------------------------------------------------------------------------
---------------

Basically, when text is changed in the edit box of FormA, it checks to see
if OnMyChange is not null, but it is null so it doesn't do anything. The
subscribe seems to run fine, but I'm not sure if the reason OnMyChange is
always null is because my subscribe method is using a different instance of
FormA or not.

Thanks.

STom


"Fergus Cooney" <fi******@tesco.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi Stom,

My guess is that Greg means a class of your own design that looks
after the communications regarding data changes.

But let's look at your solution - your thinking isn't as confused
as you feel.

|| I am considering using delegates to fire events from forms
|| A & B. In forms C, D, E I will have functions with the
|| same signature and even the same name that just updates
|| the data on those readonly screens.

Yes. Forms A and B will be firing events via Delegates. C, D and E
will have functions that conform. The same name isn't necessary but
makes good sense.

|| Create a Delegate in Form A and one in Form B.

Yes again.

|| Create a method in Form C, D, E where I can
|| subscribe to the Delegates from forms A & B.
|| This could be called on form load.

Yes again. And again.

|| This is where my confusion starts (or possibly before)

Well, not really and (no).

|| when I subscribe to the event in forms A and B,
|| won't I need an instance of those form objects?

Yes. Forms C, D and E will need to know who to subscribe to. So
they will need to know about A and B.

|| I don't think having form C, D, E have instances
|| of Form A and B are the appropriate way of doing it.

It's inevitable. Someone's got to know about someone to get a
connection going.

This is where Greg's idea comes back in as an alternative. By
having each Form know about his central object*, they needn't know
about each other. The object would have a port (function) through
which it receives value update information from A and B. It would have
the Delegate that C, D and E hook themselves on to. So when A or B
pass in a value, the Delegate is activated, calling C, D and E.

|| Am I way offbase with my thinking?

Oh yes. I haven't agreed with a word you've said, lol.

Regards,
Fergus

* Because there's only need for one, you could use a static class.
This makes it effectively global - so no need for passing it around.

Nov 15 '05 #5
Hi Stom,

Sorry I've taken so long to get back to you.

You were right about your new instance of FormA. Your FormA_Load is ok but
it needs to tell the second form about itself (ie pass a refernce).

private void FormA_Load(object sender, System.EventArgs e)
{
myB = new FormB (this); // Create a new FormB which knows about
this FormA.
myB.Show();
}
There needs to be a new FormB constructor which will accept another Form
for it to subscribe to.

public FormB (Form oForm) : this()
{
Subscribe (oForm);
}
Note the ": this()" which says to call the original constructor first.
This ensures that any Component initialisation is done. (Put some Controls on
the Form. Run it. Then take out ": this()" and see the difference.)

The subscription is no longer done in Load because the reference to FormA
is not available to Load().

public void Subscribe(Form oForm)
{
oForm.OnMyChange += new
FormA.SomethingChangedDelegate(CallMeWhenChanged);
}
Note that the parameter are Form rather than FormA. This generalises it so
that FormB could be hooked up to any Form which has an event conforming to the
SomethingChangedDelegate.

Note, too. The SomethingChangedDelegate needn't be declared <inside>
FormA. It can stand in its own right. This will allow <any> Form to ustilise
it. This will give:

public void Subscribe(Form oForm)
{
oForm.OnMyChange += new
SomethingChangedDelegate(CallMeWhenChanged);
}

Come back if you need more, or if I've made a mistake! :-)

Regards,
Fergus
Nov 15 '05 #6
Fergus Cooney <fi******@tesco.net> wrote:
Note the ": this()" which says to call the original constructor first.
This ensures that any Component initialisation is done. (Put some Controls on
the Form. Run it. Then take out ": this()" and see the difference.)


You won't see any difference. The parameterless base constructor is
called automatically if you don't specify any other constructors to
call. Here's an example to show that:

using System;

public class Base
{
public Base()
{
Console.WriteLine ("Base constructor");
}
}

public class Derived : Base
{
public Derived (string x)
{
Console.WriteLine ("x={0}", x);
}

static void Main()
{
Derived d = new Derived("hello");
}
}

Note also that the parameter type would have to be FormA (or whatever)
in order to have the OnMyChange event, unless it's a standard event
that you actually want to be triggered.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #7
Jon Skeet <sk***@pobox.com> wrote:
Fergus Cooney <fi******@tesco.net> wrote:
Note the ": this()" which says to call the original constructor first.
This ensures that any Component initialisation is done. (Put some Controls on
the Form. Run it. Then take out ": this()" and see the difference.)


You won't see any difference. The parameterless base constructor is
called automatically if you don't specify any other constructors to
call.


Doh - ignore me, I'm being silly. Yes, a call to this() is necessary -
it's a call to base() which wouldn't be necessary :)

(Fergus, I hope you get to see this one before replying to my previous
one. Sorry!)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #8
Hi Jon,

|| Doh!

No worries, mate :-)

Regards,
Fergus
Nov 15 '05 #9

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

Similar topics

4
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...
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...
12
by: Grant | last post by:
I am having great difficulty understanding this and any code samples I find online are kilometres long and complicated to understand...Please could someone give me a simple exampe of how to get a...
4
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with...
12
by: The One We Call 'Dave' | last post by:
Hi, Various custom controls in my WinForms application register for the 'Application.Idle' event on load, and unregister on dispose. To avoid memory leaks, it's essential that I remember to...
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...
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...
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: 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
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
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,...
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.