Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / October 2005

Tip: Looking for answers? Try searching our database.

custom event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Victor Paraschiv - 28 Oct 2005 16:45 GMT
I create a custom event as follows:
        public delegate void delegMarkReceivedMessege(char ch);
        public event delegMarkReceivedMessege MessageReceivedEvent;

and I want to fire it.
 I use
MessageReceivedEvent('A');
and I get a nullreferenceexception. I can see by myself that the event
is null but I didn't know that I have to initialize such an object. What
shall I do ?

       Victor
Tim Wilson - 28 Oct 2005 18:54 GMT
Someone would need to have hooked into the event, otherwise it will be null.
See the "Handling and Raising Events" section of the MSDN docs.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cp
conevents.asp


Signature

Tim Wilson
.NET Compact Framework MVP

> I create a custom event as follows:
>          public delegate void delegMarkReceivedMessege(char ch);
[quoted text clipped - 8 lines]
>
>         Victor
Victor Paraschiv - 28 Oct 2005 22:16 GMT
Actually someone is hooked to the event. This event is part of a class
and is exposed to the exterior. This event is handled in at least 3
different classes.

        Victor

> Someone would need to have hooked into the event, otherwise it will be null.
> See the "Handling and Raising Events" section of the MSDN docs.
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cp
conevents.asp
Tim Wilson - 28 Oct 2005 22:34 GMT
The typical way to raise a custom control event is as follows...

public event AlarmEventHandler Alarm;

protected virtual void OnAlarm(AlarmEventArgs e)
{
 if (Alarm != null)
 {
   Alarm(this, e);
 }
}

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(...);

Signature

Tim Wilson
.NET Compact Framework MVP

> Actually someone is hooked to the event. This event is part of a class
> and is exposed to the exterior. This event is handled in at least 3
[quoted text clipped - 4 lines]
> > Someone would need to have hooked into the event, otherwise it will be null.
> > See the "Handling and Raising Events" section of the MSDN docs.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cp
conevents.asp

Victor Paraschiv - 28 Oct 2005 23:06 GMT
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(...);
Tim Wilson - 29 Oct 2005 00:25 GMT
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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.