Files
dotNetStudyWithGPT/MySolution/ConsoleApp/Samples/EventSample.cs

33 lines
701 B
C#
Raw Normal View History

2023-05-11 11:47:56 +09:00
using System;
2023-05-11 13:56:53 +09:00
namespace Samples.Event
2023-05-11 11:47:56 +09:00
{
public delegate void CustomEventHandler(object sender, EventArgs e);
class EventSample
{
public static void Sample()
{
Button button = new Button();
button.Click += new CustomEventHandler(Button_Click);
button.OnClick();
}
private static void Button_Click(object sender, EventArgs e)
{
System.Console.WriteLine("Button clicked!");
}
}
class Button
{
public event CustomEventHandler Click;
public void OnClick()
{
if (Click != null)
Click(this, EventArgs.Empty);
}
}
}