473,626 Members | 3,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Add an eventHandler only one time?

Hi!

"CDemo.Call " eventHandler is added to "button3.cl ick"
Event when both button1 and button2 are clicked.
However, I want to add "cd.Call" only one time even though
I clicked both button1 and button2.

For Example,
1. Click button1
2. Click button2
3. Click button3
4. One MessageBox is shown

How to do this job?
private System.Windows. Forms.Button button1;
private System.Windows. Forms.Button button2;
private System.Windows. Forms.Button button3;

public CDemo cd = new CDemo();
..
private void button1_Click(o bject sender, System.EventArg s
e)
{
button3.Click += new EventHandler(cd .Call);
}

private void button2_Click(o bject sender, System.EventArg s
e)
{
button3.Click += new EventHandler(cd .Call);
}

..
..
..
public class CDemo
{
public void Call(object sender, System.EventArg s e)
{
MessageBox.Show ("Demo");
}
}
Nov 16 '05 #1
12 4412

private bool handlerAdded = false;

private void button1_Click(o bject sender, System.EventArg s e)
{
if ( !handlerAdded ) {
button3.Click += new EventHandler(cd .Call);
handlerAdded = true;
}
}

and similarly for button2_Click.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Hi!

Even your code provides one solution, I can't use your
code.
I don't want use another global variable in order to solve
this situation.
I have to solve this problem only with both object
instance button3 and cd.

Thanks,
Jongmin

-----Original Message-----

private bool handlerAdded = false;

private void button1_Click(o bject sender, System.EventArg s e){
if ( !handlerAdded ) {
button3.Click += new EventHandler(cd .Call);
handlerAdded = true;
}
}

and similarly for button2_Click.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.comPlease reply only to the newsgroup.
.

Nov 16 '05 #3
how about this:

private void button1_Click(. ..) {
button3.Click -= new EventHandler(cd .Call);
button3.Click += new EventHandler(cd .Call);
}

private void button2_Click(. ..) {
button3.Click -= new EventHandler(cd .Call);
button3.Click += new EventHandler(cd .Call);
}

Keeps only one event handler.
Nov 16 '05 #4
Hi,

Why don't you enable button3 on button1 (or button2) click?
Has it any more functionality than cd.Call?

<snip>
I don't want use another global variable in order to solve
this situation.


If code of your button handlers lies in a form class,
you shold not affraid to use any simple variables. They take
less memory than any of your control.

Previous solutions should be suitable too.

Regards

Marcin
Nov 16 '05 #5
The codesnippet just simplified my problem for explaining.

In real code, I have many kinds of object not window form
types.
-----Original Message-----
Hi,

Why don't you enable button3 on button1 (or button2) click?Has it any more functionality than cd.Call?

<snip>
I don't want use another global variable in order to solve this situation.
If code of your button handlers lies in a form class,
you shold not affraid to use any simple variables. They

takeless memory than any of your control.

Previous solutions should be suitable too.

Regards

Marcin
.

Nov 16 '05 #6
Yes, I have used your way as the following code.

for(int intI=0; intI < 10; intI++)
button3.Click -= new EventHandler(cd .Call);

button3.Click += new EventHandler(cd .Call);

I thought this is so clumsy.

-----Original Message-----
how about this:

private void button1_Click(. ..) {
button3.Click -= new EventHandler(cd .Call);
button3.Click += new EventHandler(cd .Call);
}

private void button2_Click(. ..) {
button3.Click -= new EventHandler(cd .Call);
button3.Click += new EventHandler(cd .Call);
}

Keeps only one event handler.
.

Nov 16 '05 #7
Can you explain why you want such a strange behavior? Maybe you have wrong
approach to the problem.
Uzytkownik "Jongmin" <an*******@disc ussions.microso ft.com> napisal w
wiadomosci news:4c******** *************** *****@phx.gbl.. .
Yes, I have used your way as the following code.

for(int intI=0; intI < 10; intI++)
button3.Click -= new EventHandler(cd .Call);

button3.Click += new EventHandler(cd .Call);

I thought this is so clumsy.

-----Original Message-----
how about this:

private void button1_Click(. ..) {
button3.Click -= new EventHandler(cd .Call);
button3.Click += new EventHandler(cd .Call);
}

private void button2_Click(. ..) {
button3.Click -= new EventHandler(cd .Call);
button3.Click += new EventHandler(cd .Call);
}

Keeps only one event handler.
.

Nov 16 '05 #8

Basically, I have four classes related to this situation,
TPart, TRefPart, TPartCollection , and TRefPartCollect ion.
TPart is basic type and holds data.
TRefPart is referring TPart instance. It means TRefPart
depends on TPart.
TPartCollection and TRefPartCollect ion is used for
DataBinding.

For Example,

TPart part = new TPart();
part.Idx = 1;
part.Name = "Hard Disk XXXXXXX";

TRefPart refPart1 = new TRefPart(part);
TRefPart refPart2 = new TRefPart();
refPart2.Idx = part.Idx;

TRefPartCollect ion refParts = new TRefPartCollect ion();
refParts.Add(re fPart1);
refParts.Add(re fpart2);

listBox.DataSou rce = refParts;
part.Name = "Hard Disk";
MessageBox.Show (refPart1); // -> "Hard Disk";
MessageBox.Show (refPart2); // -> "Hard Disk";

Even though both refPart1 and refPart2 show correct Name
of part1, this part.Name change can't refresh list of
ListBox fully, because refPart2's OnName_Changed
eventhandler is not added to Part's NameChange Event.

I have to add the following code after creating part2.
part.NameChange d = refPart2.OnName _Changed

My problem is this adding job takes places multifully at
differet area.

Thanks,
Jongmin


public class TPart
{
public event EventHandler NameChanged;

private int _Idx = 0;
public virtual int Idx
{
set { _Idx = value;}
get { return _Idx ;}
}

private string _Name = "Noname";
public string Name
{
set
{
_Name = value;
Name_Changed();

}
get { return _Name ;}
}
private void Name_Changed()
{
if (NameChanged != null)
NameChanged(thi s, System.EventArg s.Empty);
}
}
public class TRefPart
{
public event EventHandler NameChanged;

public TRefPart()
{
}

public TRefPart(TPart part)
{
this.Idx = part.Idx;
ConnectEventHan dler(part);
}

public static TPart operator ~(TRefPart a)
{
/*
return the TPart which has same Idx in my
application.
*/
return null;
}

public string Name
{
get
{
if ((~this) != null)
return (~this).Name;
else
return "N/A";
}
}
private void ConnectEventHan dler(TPart part)
{
if (part != null)
{
part.NameChange d +=new EventHandler
(OnName_Changed );
}

}
private void DisconnectEvent Handler(TPart part)
{
if (part != null)
{
part.NameChange d -=new EventHandler
(OnName_Changed );
}
}

public override string ToString()
{
return Name;
}

public void OnName_Changed( object sender,
System.EventArg s e)
{
base.Name_Chang ed();
}
}

public class TRefPartCollect ion :CollectionBase ,
IBindingList
{
public TPartCollection ()
{
}

private ListChangedEven tArgs resetEvent = new
ListChangedEven tArgs(ListChang edType.Reset, -1);
public event ListChangedEven tHandler ListChanged;
public event EventHandler RefPartAdded;
public event EventHandler RefPartRemoved;

..
..

protected override void OnInsertComplet e(int index,
object value)
{
RefPart_Added(( TRefPart)value) ;
OnListChanged(n ew ListChangedEven tArgs
(ListChangedTyp e.ItemAdded, index));
}

private void RefPart_Added(T RefPart item)
{
item.NameChange d += new EventHandler
(OnRefPartName_ Changed);
}

private void OnRefPartName_C hanged(object sender,
System.EventArg s e)
{
OnListChanged(r esetEvent);
}
}

public class TPartCollection :CollectionBase , IBindingList
{
public TPartCollection ()
{
}

private ListChangedEven tArgs resetEvent = new
ListChangedEven tArgs(ListChang edType.Reset, -1);
public event ListChangedEven tHandler ListChanged;
public event EventHandler PartAdded;
public event EventHandler PartRemoved;

}

Nov 16 '05 #9
> TRefPart refPart1 = new TRefPart(part);
TRefPart refPart2 = new TRefPart();
refPart2.Idx = part.Idx;


Why not create refPart2 the same way as refPart1? If you have part.Idx, you
have part, so you can assign part's OnNameChanged.

Another option would be looking up the part object in TPartCollection and
once you get it, assign its OnNameChanged, all inside TRefPart.Idx setter.

public void OnName_Changed( object sender,
System.EventArg s e)
{
base.Name_Chang ed();
}

what's the base? Object?

Btw, what you present now is a bit different from what you asked about
previously.
Nov 16 '05 #10

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

Similar topics

2
2678
by: Sandor Heese | last post by:
Question, When using BeginInvoke (on a From to marshal to the UI thread) with the EventHandler delegate I see some strange behaviour. The problem is that I call the UpdateTextBox method with a class derived from EventArgs ( i.e. MyEventArgs) and when the BeginInvoke is called and the UpdateTextBox methode is call on the UI thread the parameter e (EventArgs) does not contain the derived MyEventArgs object but a EventArgs object. The...
3
3065
by: Remco | last post by:
Hello, Serverside I'm generating a html page. There are different controls for which I want to create an eventhandler manually. Like: document.forms.TextBox1.onchange = EventHandler; document.forms.Button1.onclick = EventHandler;
5
624
by: Torben | last post by:
For test purposes I attach an event to a control, say a TextBox TextChanged event. At another time the same event delegate is attached to some other control, maybe a listbox. Same event function every where. The event function should happen only once for that control (and then, maybe again if it is attached to the control again). Could I make that deattachment operation general? could the function find out what event it is attaced?...
5
2769
by: JMWilton | last post by:
Is there a way to determine what has been added to the eventhandler list? I have code that uses += and -= to turn event handling code on and off. Apparently, it is possible to add the same event handler more than once. Documentation refers to a Delegate invocationlist...but I can't seem to access it from C#. I am looking for a way to ensure that the contents of the eventhandler list is "well known".
4
5470
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new dropdownlist to the screen for selection. This continues until there are no children, and then it checks for a help article list based on that last selection and displays actual articles for display. Adding the controls and getting everything...
3
23189
by: CodeRazor | last post by:
Hi, I am trying to dynamically create linkbuttons. They need an event handler, so i can respond to the user's click. I try to add the eventhandler on the fly, but when i click on the link, the code does not execute, it just reloads the page. Where am i going so wrong? i don't understand what's missing. many thanks in advance.
9
3076
by: Christopher Weaver | last post by:
Can anyone tell me how I could iterate through a collection of controls on a form while assigning their event handlers to another identical collection of controls on the same form. So far, thanks to another programmer, I've got this working out quite nicely for the properties: Type ctrlType = subject.GetType(); ConstructorInfo cInfo = ctrlType.GetConstructor(Type.EmptyTypes); Control retControl = (Control)cInfo.Invoke(null);
1
1572
by: Alessandro Rossi | last post by:
Hi, I am having this problem: I have developed a composite component which has 2 components: a textbox and a button. I need to add an eventhandler to a button click. I have added the eventhandler, in the CreatechildControls override, but it doesn't work. this is the code: namespace Space1 { public class LookUp: System.Web.UI.WebControls.WebControl , INamingContainer {
6
1478
by: Henrik | last post by:
Hi all, I have a problem with an application that is consuming events from an unmanaged dll. I use the following pseudo-code to create and register my eventhandler: System.Threading.WaitHandle myEvent = new AutoResetEvent(false); // Register the event to my eventhandler
0
8269
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8203
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
8642
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...
0
8512
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
7203
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5576
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
4094
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
1815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1515
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.