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

Ahh not again!

Hello

I have in a small application a usercontrol with 4 buttons. Btn1, Btn2, Btn3
and Btn4 these button is placed at the buttom of the usercontrol. On top of
the usercontrol i place an anothe usercontrol depends on the buttons i click
on. A little bit like the Outlook Side navigator.

In the usercontrols (the above) i have some label controls and some other
stuff, but when I click on one of the label controls, I want to raise an
event - CustomerCreate

That event i made by using:

public delegate void CustomerCreateClickHandler();
public event CustomerCreateClickHandler CustomerCreateClick;

In the click event on the label i use:

If ( CustomerCreateClick != null )
{
CustomerCreateClick();
}
to raise the event. I'll use this approach in btn1 - btn 3 and it works just
fine. Now on the btn4 the CustomerCreateClick is always Null?????

Can anyone explain to my the life cycle for a delegate and when it is
created on the control - or if there is another approach for creating
events/delegates.

Kind Regards
Johnny E. Jensen
Sep 21 '07 #1
4 1101
Johnny E. Jensen wrote:
[...]
Can anyone explain to my the life cycle for a delegate and when it is
created on the control - or if there is another approach for creating
events/delegates.
A delegate has the same lifetime as any other object. If something has
a reference to the delegate, it will continue to exist. Once no more
references to the delegate exist, it is eligible for garbage collection
and at some point in the future may be released.

This doesn't have anything to do with why an event you've declared
remains null. If you'd actually subscribed a delegate to the event, it
would remain subscribed until you unsubscribe it.

So either you haven't subscribed to the event, or you have subsequently
unsubscribed the delegate from that event. Whatever delegate you create
to subscribe to the event will exist for as long as you have a reference
to the delegate; if the delegate is subscribed to an event, then the
event has a reference to the delegate and the delegate will live as long
as it remains subscribed.

Pete
Sep 21 '07 #2
Johnny E. Jensen <jo****@XXemde.dkwrote:

<snip>
Can anyone explain to my the life cycle for a delegate and when it is
created on the control - or if there is another approach for creating
events/delegates.
CustomerCreateClick will be null unless something subscribes to the
event.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '07 #3
Hi Johnny,

Why not use a normal method in your user control that handles your event, to
which you point four events and use the button name using a cast of the
sender. That is all you have to do?

Cor

"Johnny E. Jensen" <jo****@XXemde.dkschreef in bericht
news:ev**************@TK2MSFTNGP04.phx.gbl...
Hello

I have in a small application a usercontrol with 4 buttons. Btn1, Btn2,
Btn3 and Btn4 these button is placed at the buttom of the usercontrol. On
top of the usercontrol i place an anothe usercontrol depends on the
buttons i click on. A little bit like the Outlook Side navigator.

In the usercontrols (the above) i have some label controls and some other
stuff, but when I click on one of the label controls, I want to raise an
event - CustomerCreate

That event i made by using:

public delegate void CustomerCreateClickHandler();
public event CustomerCreateClickHandler CustomerCreateClick;

In the click event on the label i use:

If ( CustomerCreateClick != null )
{
CustomerCreateClick();
}
to raise the event. I'll use this approach in btn1 - btn 3 and it works
just fine. Now on the btn4 the CustomerCreateClick is always Null?????

Can anyone explain to my the life cycle for a delegate and when it is
created on the control - or if there is another approach for creating
events/delegates.

Kind Regards
Johnny E. Jensen
Sep 22 '07 #4
Hello

Thanks Peter, Jon and Cor

I think i'll got it right here. First i create a new instance of the
UserControl1 : objNavEmployee = new UserControl1();
The event on the objNavEmployee.CreateEmployeeClick is still null - right?
Now i add a reference to that event on the form like
objNavEmployee.CreateEmployeeClick +=
UserControl1.CreateEmployeeClickHandler(ClickEvent );

The event on the objNavEmployee is not null - and for the first 3 buttons i
just did that. But on the 4 button I'll also did the above but at the moment
i added the UserControl i'll created a new instance and then this instances
CreateEmployeeClick event is still null besause the event has not been
referenced.

I have struggled with this a lot of times - now i know what to do - so
thanks again.

Kind regards
Johnny E. Jensen

"Johnny E. Jensen" <jo****@XXemde.dkwrote in message
news:ev**************@TK2MSFTNGP04.phx.gbl...
Hello

I have in a small application a usercontrol with 4 buttons. Btn1, Btn2,
Btn3 and Btn4 these button is placed at the buttom of the usercontrol. On
top of the usercontrol i place an anothe usercontrol depends on the
buttons i click on. A little bit like the Outlook Side navigator.

In the usercontrols (the above) i have some label controls and some other
stuff, but when I click on one of the label controls, I want to raise an
event - CustomerCreate

That event i made by using:

public delegate void CustomerCreateClickHandler();
public event CustomerCreateClickHandler CustomerCreateClick;

In the click event on the label i use:

If ( CustomerCreateClick != null )
{
CustomerCreateClick();
}
to raise the event. I'll use this approach in btn1 - btn 3 and it works
just fine. Now on the btn4 the CustomerCreateClick is always Null?????

Can anyone explain to my the life cycle for a delegate and when it is
created on the control - or if there is another approach for creating
events/delegates.

Kind Regards
Johnny E. Jensen

Sep 22 '07 #5

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

Similar topics

7
by: Phil Powell | last post by:
I am having this problem: My PHP script will set a cookie, it's there in my /Cookies folder. I delete the cookie (I have to for testing purposes, the PHP script I run behaves according to this...
5
by: Bruce | last post by:
I have a number of forms that do significant work based on variables POSTed from the form. What is the common method of detecting and preventing this work from being done when the form is POSTed as...
1
by: Jiten | last post by:
Hi Cor I spoke with u previously about this problem i had and u gave me some code that helped me. That code worked fine but i have now encountered another issuee that im hoping u may know how to...
8
by: jon morgan | last post by:
OK, I'm going to be brave. There is a bug in VS.Net 1.1 that causes random compiler errors. I have raised this issue in posts at least three time in the past couple of months without attracting...
17
by: Gabriel Mejía | last post by:
Services or applications using ActiveX Data Objects (ADO) 2.0 or greater may intermittently return empty recordsets on queries that should be returning valid results. At the time the problem...
3
by: penny336 | last post by:
dear all, i am using vc++ 6.0 sp5 i have a class called Address,it allocated some memory space for streetname and city i first define it as Address add = new Address("Madrian","UK"); ......
11
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
6
by: teedilo | last post by:
We have an application with a SQL Server 2000 back end that is fairly database intensive -- lots of fairly frequent queries, inserts, updates -- the gamut. The application does not make use of...
34
by: Reinhold Birkenfeld | last post by:
Hi, the arguments in the previous thread were convincing enough, so I made the Path class inherit from str/unicode again. It still can be found in CVS:...
5
by: Mike TI | last post by:
March 24, 2006 Hi all I am new to VB.NET and am using VB.NET 2005. I have an MDI form with a Split Container Control. On demand I am adding and removing User Controls on Panel 2. I am using...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.