Hi!
"CDemo.Call" eventHandler is added to "button3.click"
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(object sender, System.EventArgs
e)
{
button3.Click += new EventHandler(cd.Call);
}
private void button2_Click(object sender, System.EventArgs
e)
{
button3.Click += new EventHandler(cd.Call);
}
..
..
..
public class CDemo
{
public void Call(object sender, System.EventArgs e)
{
MessageBox.Show("Demo");
}
} 12 4396
private bool handlerAdded = false;
private void button1_Click(object sender, System.EventArgs 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.
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(object sender,
System.EventArgs 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. .
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.
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
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 .
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.
.
Can you explain why you want such a strange behavior? Maybe you have wrong
approach to the problem.
Uzytkownik "Jongmin" <an*******@discussions.microsoft.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.
.
Basically, I have four classes related to this situation,
TPart, TRefPart, TPartCollection, and TRefPartCollection.
TPart is basic type and holds data.
TRefPart is referring TPart instance. It means TRefPart
depends on TPart.
TPartCollection and TRefPartCollection 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;
TRefPartCollection refParts = new TRefPartCollection();
refParts.Add(refPart1);
refParts.Add(refpart2);
listBox.DataSource = 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.NameChanged = 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(this, System.EventArgs.Empty);
}
}
public class TRefPart
{
public event EventHandler NameChanged;
public TRefPart()
{
}
public TRefPart(TPart part)
{
this.Idx = part.Idx;
ConnectEventHandler(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 ConnectEventHandler(TPart part)
{
if (part != null)
{
part.NameChanged +=new EventHandler
(OnName_Changed);
}
}
private void DisconnectEventHandler(TPart part)
{
if (part != null)
{
part.NameChanged -=new EventHandler
(OnName_Changed);
}
}
public override string ToString()
{
return Name;
}
public void OnName_Changed(object sender,
System.EventArgs e)
{
base.Name_Changed();
}
}
public class TRefPartCollection :CollectionBase,
IBindingList
{
public TPartCollection()
{
}
private ListChangedEventArgs resetEvent = new
ListChangedEventArgs(ListChangedType.Reset, -1);
public event ListChangedEventHandler ListChanged;
public event EventHandler RefPartAdded;
public event EventHandler RefPartRemoved;
..
..
protected override void OnInsertComplete(int index,
object value)
{
RefPart_Added((TRefPart)value);
OnListChanged(new ListChangedEventArgs
(ListChangedType.ItemAdded, index));
}
private void RefPart_Added(TRefPart item)
{
item.NameChanged += new EventHandler
(OnRefPartName_Changed);
}
private void OnRefPartName_Changed(object sender,
System.EventArgs e)
{
OnListChanged(resetEvent);
}
}
public class TPartCollection :CollectionBase, IBindingList
{
public TPartCollection()
{
}
private ListChangedEventArgs resetEvent = new
ListChangedEventArgs(ListChangedType.Reset, -1);
public event ListChangedEventHandler ListChanged;
public event EventHandler PartAdded;
public event EventHandler PartRemoved;
}
> 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.EventArgs e)
{
base.Name_Changed();
}
what's the base? Object?
Btw, what you present now is a bit different from what you asked about
previously. 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, youhave part, so you can assign part's OnNameChanged.
This is example why I need to add EventHander to event
seperately.
Another option would be looking up the part object in
TPartCollection andonce you get it, assign its OnNameChanged, all inside
TRefPart.Idx setter. public void OnName_Changed(object sender, System.EventArgs e) { base.Name_Changed(); }
what's the base? Object?
It is my mistypo. It should be Name_Changed();
Btw, what you present now is a bit different from what
you asked aboutpreviously.
I know this code is totally differen from previous one.
But, I simplified my code for explaining.
Again, the problem is
bbbb.XXXXXXXX += new EventHandler(cccc.AAAAAA);
When I add a event handler of cccc to bbbb's event,
1. If the event handler(AAAAAA) has already been added
to the bbbbb's event(XXXXXXXX ), NOT ADD.
2. If the event handler(AAAAAA) has NOT, ADD cccc's
AAAA to bbbb.XXXXXXXX
Thanks,
Jongmin
Another nasty suggestion (untested):
public delegate void XxxEventHandler();
....
private ArrayList delegates;
public void AddXxxDelegate(XxxEventHandler deleg) {
if (!delegates.Contains(deleg))
delegates.Add(deleg);
}
protected virtual void OnXxx() {
foreach (XxxEventHandler deleg in delegates)
deleg.DynamicInvoke(null);
}
Better yet, use Delegate.RemoveAll(myDelegate) instead.
U¿ytkownik "lukasz" <bb****@op.pl> napisa³ w wiadomo¶ci
news:Ow**************@TK2MSFTNGP15.phx.gbl... Another nasty suggestion (untested):
public delegate void XxxEventHandler();
...
private ArrayList delegates;
public void AddXxxDelegate(XxxEventHandler deleg) { if (!delegates.Contains(deleg)) delegates.Add(deleg); }
protected virtual void OnXxx() { foreach (XxxEventHandler deleg in delegates) deleg.DynamicInvoke(null); } This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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;...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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:
...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
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: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |