Different ways of Event Handling

Event Handling can used different ways in c#. Some of the examples are working in latest .net framework only.

Traditional way of implementation

buttonOne.Click +=new Office._CommandBarButtonEvents_ClickEventHandler(buttonOne_Click);
 
        private void buttonOne_Click(Office.CommandBarButton ctrl,
            ref bool cancel)

        {
                    MessageBox.Show("test");
        }


Inline way of event implementation

buttonOne.Click += new  Office._CommandBarButtonEvents_ClickEventHandler(delegate(Office.CommandBarButton c, ref bool c1)
                            {
                                MessageBox.Show("test");

                            });

 Anonymous methods

 Using Lambda Expression

 The lambda expression syntax uses the => operator. This separates the parameters and statement body of the anonymous function

buttonOne.Click += (Office.CommandBarButton c1, ref bool c11) => MessageBox.Show("test");
 
Using delegate
Delegate is a c# key word is used for match a signature of a method it will point to.

buttonOne.Click += delegate { showMessage(); };
private void showMessage()
        {

            MessageBox.Show("test");
        }


No comments:

Post a Comment