I've been reviewing this article for the past 3 days. I'm not trying to
raise a custom control event but a simply custom event.
I receive some date form the serial port and after I manage to build the
useful data I rise an event from my Connection class to the interface
whith a character that stands for my received message type.
The delegate is declared as follows:
public delegate void MarkReceivedMessege(char ch);
public event MarkReceivedMessege MessageReceivedEvent;
The event is handled in the constructor of the interface class:
connection = new Connection();
connection.MessageReceivedEvent += new
Connection.MarkReceivedMessege(connection_MessageReceivedEvent);
As I follow the course of the program in debugger the
MessageReceivedEvent remains allways null.
> The typical way to raise a custom control event is as follows...
>
[quoted text clipped - 17 lines]
>
> this.clock.Alarm += new AlarmEventHandler(...);
You're posting to the
"microsoft.public.dotnet.framework.windowsforms.controls" newsgroup and so
that's why I assumed that this was an event attached to a custom
control/component. I just ran a quick test using code similar to the
following and it seemed to work as expected.
public class Form1 : System.Windows.Forms.Form
{
...
private Connection connection;
...
public Form1()
{
InitializeComponent();
connection = new Connection();
connection.MessageReceivedEvent += new
Connection.MarkReceivedMessege(connection_MessageReceivedEvent);
}
private void button1_Click(object sender, System.EventArgs e)
{
connection.FireEvent();
}
private void connection_MessageReceivedEvent(char ch)
{
MessageBox.Show(ch.ToString());
}
...
}
public class Connection
{
public delegate void MarkReceivedMessege(char ch);
public event MarkReceivedMessege MessageReceivedEvent;
public void FireEvent()
{
MessageReceivedEvent('A');
}
}
And if I call the "FireEvent" method against the "connection" object, for
example through a Button Click event handler in the Form1 class, if fires ok
and a MessageBox is displayed with the string "A". Perhaps the exception is
somehow coming from a different line in the code?

Signature
Tim Wilson
.NET Compact Framework MVP
> I've been reviewing this article for the past 3 days. I'm not trying to
> raise a custom control event but a simply custom event.
[quoted text clipped - 28 lines]
> >
> > The complete implementation can be found here...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cp
conprovidingeventfunctionality.asp
> > What you indicated sounded like you were trying to raise the event when it
> > didn't have any subscribers. So you're sure that an event handler has been
> > created against the event for the specific instance of the control which is
> > raising the exception?
> >
> > this.clock.Alarm += new AlarmEventHandler(...);
Victor Paraschiv - 29 Oct 2005 10:20 GMT
Tim, thank you very much for your support and your time. I solved the
problem changing in the InitializeComponent() the order of some controls
and placing the creation of the Connection class at the end of the
constructor. It seemed that not all the controls on the Form were
initialized at the time of handling the MessageReceivedEvent.
Thank you very much with your support.
Victor Paraschiv
> You're posting to the
> "microsoft.public.dotnet.framework.windowsforms.controls" newsgroup and so
[quoted text clipped - 47 lines]
> and a MessageBox is displayed with the string "A". Perhaps the exception is
> somehow coming from a different line in the code?
Tim Wilson - 29 Oct 2005 15:10 GMT
You're welcome.

Signature
Tim Wilson
.NET Compact Framework MVP
> Tim, thank you very much for your support and your time. I solved the
> problem changing in the InitializeComponent() the order of some controls
[quoted text clipped - 57 lines]
> > and a MessageBox is displayed with the string "A". Perhaps the exception is
> > somehow coming from a different line in the code?