muti-form event handlers

  • Thread starter Thread starter Daniel Passwater via DotNetMonster.com
  • Start date Start date
D

Daniel Passwater via DotNetMonster.com

I am newbie and have been thrown into the fire. Please be patient.
I am developing a multi-form user interface in C# in the compact framework. It communicates via serial connection. Each form passes and receives data. I have set up event handlers for receiving data in each of the forms. However, as I exit one form and procede to the next, from a central form, the event handler from the previous form seems to be still active. Should there be only one event handler? If so how is this implemented? I can't use a global buffer.

Thank you in advance for and all help.
Daniel

*****************************************
* This message was posted via http://www.dotnetmonster.com
*
* Report spam or abuse by clicking the following URL:
* http://www.dotnetmonster.com/Uwe/Abuse.aspx?aid=9489d737ce1c4f67a458eed6364e6b0e
*****************************************
 
You should explicitly remove the event handlers as you move between Forms if
you intend to keep the mechanism you have. Personally I'd use a global
event handler in a class that is used only once. Typically I have an
AppGlobal class in my apps that holds static void main and any application
global stuff. I'd put the Port class in there as well and its handlers.
I'd then implement events in the AppGlobal and have the Forms use those for
data arrival notifications.
 
I tried to explicitly remove the event handlers, but I'm not sure if I'm doing correctly. Here's what I have:

// Add event handler
MainOperations.port.DataReceived += new Serial.Port.CommEvent (GetData);

// Remove event handler
MainOperations.port.DataReceived -= new Serial.Port.CommEvent (GetData);

My problem with this is that the next form that's called acts like it has no event handler even though another on is added in tha form. The connection is still good, but it doesn't get the data. Is this the correct sytax for removal?

*****************************************
* A copy of the whole thread can be found at:
* http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-compact-framework/9186
*
* Report spam or abuse by clicking the following URL:
* http://www.dotnetmonster.com/Uwe/Abuse.aspx?aid=ba7dbd00b2ac493594efe1418e56de0e
*****************************************
 
Your removal is wrong. You're creating a new handler and then trying to
remove it. You need to declare a class level instance of the delegate and
store it after addition for removal later.
 
Back
Top