Click here to Skip to main content
16,005,316 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hello,




Can any body give me a simple example of delegate and event on click button event in window form C#. like if i click on button event which is configure on click button will fire.



C#
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication21
{
    public partial class Form1 : Form
    {
        public delegate void BoilerLogHandler(string status);
        public event BoilerLogHandler BoilerEventLog;
	public Form1()
	{
	    InitializeComponent();

	}

	private void button1_Click(object sender, EventArgs e)
	{
	    MessageBox.Show("Dot Net Perls says hello.",
		"How is your day going?");
	}
    }
}


Thanks

What I have tried:

how to fire delegate and event on button click event in window form c#
Posted
Updated 8-May-16 19:43pm
Comments
Sergey Alexandrovich Kryukov 9-May-16 1:14am    
The question makes no sense. You don't "fire" event, the System.Windows.Forms library does. You can only handle the even; and, for this purpose, you supply a delegate instance. It means inversion of control.

There is no a way to invoke this event at all, even if you create a derived class; this is the important .NET fool-proof feature. You can only invoke the event you declare yourself in your own class/struct.

—SA
Member 12131277 9-May-16 1:17am    
ok...can you give me a example. how invoke control?
it is possible you expalain with a simple example
Sergey Alexandrovich Kryukov 9-May-16 1:25am    
Wrong question. You don't need any examples at this moment. And you should not learn anything based on examples.
How can you understand any of them if you use the words "invoke control"? there is no such thing...
—SA
Member 15416709 2-Nov-21 17:28pm    
A simple no would have sufficed. People like you really make new programmers lose interest on keep learning. Go eat a snickers man.

It possible you mean something like this:
C#
private delegate void BoilerLogHandler(string status);
BoilerLogHandler theDelegate = BoilerLogHandlerB;
private void myCheckBox_CheckedChanged(object sender, EventArgs e)
    {
    if (myCheckBox.Checked)
        {
        theDelegate = BoilerLogHandlerA;
        }
    else
        {
        theDelegate = BoilerLogHandlerB;
        }
    }

private void myButton_Click(object sender, EventArgs e)
    {
    theDelegate("Hello from a button click!");
    }
private static void BoilerLogHandlerA(string status)
    {
    Console.WriteLine("A: {0}", status);
    }
private static void BoilerLogHandlerB(string status)
    {
    Console.WriteLine("B: {0}", status);
    }

When you change the status of the checkbox, it changes the delegate. And when you click the button, the delegate is used to call whichever method is currently set.
For an event it's slightly different:
C#
/// <summary>
/// Event to indicate Test Event Thrown
/// </summary>
public event EventHandler TestEvent;
/// <summary>
/// Called to signal to subscribers that Test Event Thrown
/// </summary>
/// <param name="e"></param>
protected virtual void OnTestEvent(EventArgs e)
    {
    EventHandler eh = TestEvent;
    if (eh != null)
        {
        eh(this, e);
        }
    }
private void myCheckBox_CheckedChanged(object sender, EventArgs e)
    {
    if (myCheckBox.Checked)
        {
        TestEvent +=frmMain_TestEventA;
        }
    else
        {
        TestEvent += frmMain_TestEventB;
        }
    }

void frmMain_TestEventA(object sender, EventArgs e)
    {
    Console.WriteLine("EventA");
    }

void frmMain_TestEventB(object sender, EventArgs e)
    {
    Console.WriteLine("EventB");
    }

private void myButton_Click(object sender, EventArgs e)
    {
    OnTestEvent(new EventArgs());
    }

Now each time you change the CheckBox, another handler is added to the event.
Since an event is internally implemented via a delegate, you can also use the "+=" syntax with delegates to "chain" methods into it, but that's a little more rare to do directly.
 
Share this answer
 
How to Use Delegate -

C#
public delegate void PalashDelegate();

        class TestExpDelegate
        {
            public static void MyFunc()
            {
                Console.WriteLine("I was called by delegate ...");
            }

            public static void Main()
            {
                // Instantiation
                PalashDelegate PalashDelegate = new PalashDelegate(MyFunc);

                // Invocation
                PalashDelegate();
            }
        }



How to use Event -


C#
class TestExpEvent
{
   public event EventHandler chwEventFire_click;

   TestExpEvent()
   {                
       this.chwEventFire_click += new EventHandler(Obj_chwEventFire_click);
       this.chwEventFire_click += new EventHandler(Obj_chwEventFire_click);
       Console.Write("Constructor");
   }

   private void Button_Click(object sender, RoutedEventArgs e)
   {
       if (chwEventFire_click != null)
       {
           chwEventFire_click(this, new EventArgs());
       }
    }
       
    void Obj_chwEventFire_click(object sender, EventArgs e)
    {
       Console.Write("Event fire");
    }   
            
}




You can refer this link also.

Chapter 17. Delegates and Events[^]
 
Share this answer
 
v2
Sorry, no code for you, because the question makes no sense. It does not mean that doing what you ask is possible or impossible; it's just because you are using non-existing or incorrectly understood concepts. Please see my comment to the question.

You really need to understand how event-oriented programming works. This is something to start with:
Inversion of control — Wikipedia, the free encyclopedia[^],
Event-driven programming — Wikipedia, the free encyclopedia[^],
Delegates (C# Programming Guide)[^],
Delegates Tutorial (C#)[^],
Events Tutorial (C#)[^],
Handling and Raising Events[^].

I want to warn you: don't even play with the idea of doing UI development at this moment, before you learn and very clearly understand all of the above. It would be programming by cookbook recipes, which would be very bad. You have to learn all that, and the best way would be doing it based on very simple console-only exercises at first.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900