Form's Move event from derived class getting called 3 times instead of once

  • Thread starter Thread starter Augie
  • Start date Start date
A

Augie

Hi All.

I have a class that subscribes to a Form's Move event on the constructor.
SeaPersistWindow::SeaPersistWindow(Form *form, String *path)

: mParent(form), mRegPath(path)

{

mParent->Move += new System::EventHandler(this, SeaPersistWindow_Move);

}


When the Form is moved, the Forms Move event gets called once but the
classes Move method gets called 3 times. Why? Can someone tell me how to
correct this?

Please help...

Thanks, Regards

Augie
 
Hi Augie,

The problem is probably that you have subscribed 3 times for that event.

if you do (you don't mind the c# syntax, do ya?)
parent.Move += new EventHandler(moveHandler);
parent.Move += new EventHandler(moveHandler);
parent.Move += new EventHandler(moveHandler);

It will hook three times the event for the same event handler. In this case
your event handler will be called three times in a row.

Look at the code and try to find out if this is your case.

The probelm could be that you don't unhook prevoius instance of the class.
The delagate behind that event will keep the object alive. If you set a
break point in the code the debuger will stop, say, three times but it might
be for three diferent instances.

It is kind of hard to say what is the real reason for this, having only the
code you have posted.
 
Hi.

Thanks for the reply.

I'm pretty sure there are no multiple calls to insert the Move event. I
have a write statement by the subscription to the Move event and that only
gets called once in the class constructor.

I was thinking this might be related to some other problem/issue... I'm not
sure what... I have been working on this for a few days and nothing
positive so far.

I'm trying to implement a very simple PersistWindow class to keep the window
position, size and state. When I do this in a sample app it works great.
When I put the same code in a class as part of a DLL. I link the DLL in the
new app and instantiate an object of this class.

Any time the user moves the window, I get 3 calls instead of one. I never
have this problem with the Load, Closing or Resize events. Only the Move.

I read some issues about EventHandler inheritance problems and this is where
I think the problem might be...

Any more ideas???

TIA

Augie

Stoitcho Goutsev (100) said:
Hi Augie,

The problem is probably that you have subscribed 3 times for that event.

if you do (you don't mind the c# syntax, do ya?)
parent.Move += new EventHandler(moveHandler);
parent.Move += new EventHandler(moveHandler);
parent.Move += new EventHandler(moveHandler);

It will hook three times the event for the same event handler. In this case
your event handler will be called three times in a row.

Look at the code and try to find out if this is your case.

The probelm could be that you don't unhook prevoius instance of the class.
The delagate behind that event will keep the object alive. If you set a
break point in the code the debuger will stop, say, three times but it might
be for three diferent instances.

It is kind of hard to say what is the real reason for this, having only the
code you have posted.
--
HTH
Stoitcho Goutsev (100) [C# MVP]


Augie said:
Hi All.

I have a class that subscribes to a Form's Move event on the constructor.
SeaPersistWindow::SeaPersistWindow(Form *form, String *path)

: mParent(form), mRegPath(path)

{

mParent->Move += new System::EventHandler(this, SeaPersistWindow_Move);

}


When the Form is moved, the Forms Move event gets called once but the
classes Move method gets called 3 times. Why? Can someone tell me how to
correct this?

Please help...

Thanks, Regards

Augie
 
Hi Augie,

if you want to you can send me a sample project at guzzev_at_yahoo_dot_com.
I might be able to help you better if I have a working example of the
problem

--

Stoitcho Goutsev (100) [C# MVP]


Augie said:
Hi.

Thanks for the reply.

I'm pretty sure there are no multiple calls to insert the Move event. I
have a write statement by the subscription to the Move event and that only
gets called once in the class constructor.

I was thinking this might be related to some other problem/issue... I'm not
sure what... I have been working on this for a few days and nothing
positive so far.

I'm trying to implement a very simple PersistWindow class to keep the window
position, size and state. When I do this in a sample app it works great.
When I put the same code in a class as part of a DLL. I link the DLL in the
new app and instantiate an object of this class.

Any time the user moves the window, I get 3 calls instead of one. I never
have this problem with the Load, Closing or Resize events. Only the Move.

I read some issues about EventHandler inheritance problems and this is where
I think the problem might be...

Any more ideas???

TIA

Augie

Stoitcho Goutsev (100) said:
Hi Augie,

The problem is probably that you have subscribed 3 times for that event.

if you do (you don't mind the c# syntax, do ya?)
parent.Move += new EventHandler(moveHandler);
parent.Move += new EventHandler(moveHandler);
parent.Move += new EventHandler(moveHandler);

It will hook three times the event for the same event handler. In this case
your event handler will be called three times in a row.

Look at the code and try to find out if this is your case.

The probelm could be that you don't unhook prevoius instance of the class.
The delagate behind that event will keep the object alive. If you set a
break point in the code the debuger will stop, say, three times but it might
be for three diferent instances.

It is kind of hard to say what is the real reason for this, having only the
code you have posted.
--
HTH
Stoitcho Goutsev (100) [C# MVP]


Augie said:
Hi All.

I have a class that subscribes to a Form's Move event on the constructor.
SeaPersistWindow::SeaPersistWindow(Form *form, String *path)

: mParent(form), mRegPath(path)

{

mParent->Move += new System::EventHandler(this, SeaPersistWindow_Move);

}


When the Form is moved, the Forms Move event gets called once but the
classes Move method gets called 3 times. Why? Can someone tell me
how
 
Back
Top