Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Instantiating Custom events in c# csharp .net

Posted on 2008-11-17
2
Medium Priority
?
481 Views
Last Modified: 2013-12-17
So I have created an inherited object for which I have defined a custom Even and matching delegate. I also defined the custom EventArgs. Finally, I have tied the objects event with a function that will execute when the event is called.

The problem I am having is instantiating the custom event: I want the event to occur outside the object, and the object to react to it.

The reason is this: I will have several objects observing the event, and I want them all to react.

Thank you so much for reading this!
//
// Inherited Object
//
namespace NA_minu
{
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
 
    // Delegate of Event
    public delegate void GridColorChangedEventHandler(
        object sender, GridColorChangeEventArgs e);
 
    class ModifiedDataGridView: DataGridView
    {
        // Public event
        public event GridColorChangedEventHandler ColorChangedEvent;
 
        public ModifiedDataGridView()
        {
            // Empty constructor
        }
    }
    
    // Custom EventArgs
    class GridColorChangeEventArgs : EventArgs
    {
        private Color _gridBack;
        private Color _gridFore;
        private Color _gridSelectedFore;
        private Color _gridSelectedBack;
 
        public GridColorChangeEventArgs(
            Color gridBack,
            Color gridFore,
            Color gridSelectedFore,
            Color gridSelectedBack)
        {
            this._gridBack = gridBack;
            this._gridFore = gridFore;
            this._gridSelectedFore = gridSelectedFore;
            this._gridSelectedBack = gridSelectedBack;
        }
 
        public Color GridBack
        {
            get { return _gridBack; }
        }
        public Color GridFore
        {
            get { return _gridFore; }
        }
        public Color GridSelectedFore
        {
            get { return _gridSelectedFore; }
        }
        public Color GridSelectedBack
        {
            get { return _gridSelectedBack; }
        }
    }
}
 
// In the form, I attach a method to the custom event
dgvFile.ColorChangedEvent += 
                new GridColorChangedEventHandler(dgvFile_ColorChangedEvent);
 
 
// The attached method
void dgvFile_ColorChangedEvent(object sender, GridColorChangeEventArgs e)
{
    // Here I will want to manipulate the object.
}
 
// This button click should fire the event...
private void btnClose_Click(object sender, EventArgs e)
{
    
    //*** What event do I call?
    //*** When I type below the event GridColorChange, it isn't listed
    //*** in the intelli-type thing that comes up.
    if( /* ??? what do I type here??? */ != null)
    {
        /* ??? what do I type here??? */(new GridColorChangeEventArgs(
            pbForeColor.BackColor,
            pbGridForeColor.BackColor,
            pbGridSelectedForeColor.BackColor,
            pbGridSelectedBackColor.BackColor));      
    }
}

Open in new window

0
Comment
Question by:yaronusa
2 Comments
 
LVL 3

Accepted Solution

by:
RusselR earned 2000 total points
ID: 22975313
dgvFile_ColorChangedEvent(null, new GridColorChangeEventArgs(
            pbForeColor.BackColor,
            pbGridForeColor.BackColor,
            pbGridSelectedForeColor.BackColor,
            pbGridSelectedBackColor.BackColor));      

will run the code that happens when the event fires.

If you want to actually cause the event to fire when the button is clicked you need to create a new method on the dgvFile object that causes the event to fire and run that method from your buttonclick.
0
 

Author Comment

by:yaronusa
ID: 22975763
OK, thank you for your time. I seem to have gotten something backwards in my brain but you straightened it out for me.

Thank you!
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question