Results 1 to 8 of 8

Thread: Events

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Events

    How do you create a custom event? is it possible?

    I have a form
    it creates an instance of another class
    this class file has a public property which is of type bool

    everytime that bool value changes, i want the form to pick it up (the change) and notify me. is it possible?

    If it is possible, then the "notification" event can be done on the form? or must it be done in the class the variable/eventhandler is created?

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Events

    ok I wrote an extended sample for you
    make a new project, add 2 buttons to it. Copy this inside the Form1 class:

    PHP Code:
            private MySampleClass test  = new MySampleClass();
            private 
    void Form1_Load(object senderSystem.EventArgs e)
            {
                
    // Add event handlers
                
    test.CoolChanged   += new MySampleClass.IsCoolChangedEventHandler(test_CoolChanged);
                
    test.FooWasCalled  += new EventHandler(test_FooWasCalled);
            }

            
    // has to have the signature of a System.EventHandler delegate
            
    private void test_FooWasCalled (object senderEventArgs e)
            {
                
    MessageBox.Show    ("function Foo of the test class was called!!!");
            }

            
    // has to have the signature of a MySampleClass.IsCoolChangedEventHandler delegate
            
    private void test_CoolChanged (MySampleClass senderbool newCoolValueobject dummy)
            {
                
    MessageBox.Show    ("Field IsCool of the test class was called and the new value is: " +
                    
    newCoolValue.ToString() );
            }

            private 
    void button1_Click(object senderSystem.EventArgs e)
            {
                
    this.test.IsCool true;
            }

            private 
    void button2_Click(object senderSystem.EventArgs e)
            {
                
    this.test.Foo();
            } 
    this is a test class with events. paste this to a new file or outside the form1 class:
    PHP Code:
    public class MySampleClass
        
    {
            
    // An event that uses a generic event handler (therefore we use System.EventHandler

            /// <summary>
            /// Raised when the Foo() function of this class is called
            /// </summary>
            
    public event EventHandler FooWasCalled;


            
    // We need a custom delegate to handle out special event
            // This events parameters are customized! yay :D
            
    public delegate void IsCoolChangedEventHandler (MySampleClass senderbool newCoolValueobject dummy);

            
    /// <summary>
            /// Raised when the IsCool field of the class is changed.
            /// </summary>
            
    public event IsCoolChangedEventHandler CoolChanged;




            private 
    bool isCool  false;
            public 
    bool IsCool
            
    {
                
    get
                
    {
                    return 
    isCool;
                }
                
    set
                
    {
                    
    isCool value;
                    
    // value changed, raise the event

                    
    if (CoolChanged!=null)
                        
    CoolChanged (thisvaluenull);
                }
            }

            
    /// <summary>
            /// Does some stuff then raises the FooWasCalled event
            /// </summary>
            
    public void Foo()
            {
                
    /*
                 * do stuff
                 */
                  

                // If there are any event handlers attached to this event, then
                // the value will NOT be null, that's why we have to check for
                // a null reference first
                
    if (this.FooWasCalled != null)
                {
                    
    // the sytax for a System.EventHandler delegate is this: 
                    //     (object sender, EventArgs e)
                    // So I'm passing the class as the sender, and I'm not
                    // passing anything as the eventargs
                    
    FooWasCalled (thisnull);
                }
            }        
        } 


    also note that if you wanna get really fancy in C#, you can use events as properties (most people dont know this). For example
    PHP Code:
    private event EventHandler myEvent;
            public 
    event EventHandler MyEvent
            
    {
                
    // add event handler
                
    add
                
    {
                    
    myEvent += value;

                    
    //do other stuff you want
                
    }
                
    // remove event handler
                
    remove
                
    {
                    
    myEvent -= value;

                    
    //do other stuff you want
                
    }
            } 
    let me know if you need explanations and rate if it helps
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Events

    sounds awesome!
    i will give this a try and then get back to you with questions but also will give you a rating

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Events

    Hopefully, you will find this interesting.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Events

    Thanks hack!

  6. #6
    New Member
    Join Date
    Apr 2007
    Posts
    1

    Post Hello, good site! Respect :-)

    Good site Respect for admin...

  7. #7
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    Re: Events

    If you are using .Net 2.0 or above you can use the generic EventHandler<T> thus removing the need to use a delegate. please find an example attached.

    Hope this helps!!
    Attached Files Attached Files

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Events

    Just note, Bombdrop, that using the EventHandler<TEventArgs> doesn't mean you don't have to USE a delegate. It means you don't have to DECLARE a delegate. EventHandler<TEventArgs> is itself a delegate.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width