-3

I cannot understand how delegates work with events in C#.

The form of the syntax is:

public event someNameDelegate someName;

control.someName += new control.someNameDelegate(methodName);

So how does it know what event (like mouse click etc) fires the method. I'm totally missing how this works. I understand that the delegate will call methodName but I don't understand for what event.

[Addittional Info] In the 2 lines above if someName is ContentModified then the code compiles In the 2 lines above if someName is Banana then the code does not compile.

However ContentModified is nowhere else in the code apart from the 2 line above:

So How does the compiler tell the difference?

Paul McCarthy
  • 818
  • 9
  • 24
  • How does what know what fires the method? – ProgrammingLlama Jan 22 '21 at 12:18
  • It's unclear what you call "it". Perhaps you miss the part raising event? Assuming you know delegates, have you been [here](https://stackoverflow.com/a/213651/1997232) already? – Sinatr Jan 22 '21 at 12:23
  • the event handle need to derive from EventArgs and the delegate should have sign like this : void OnEventHandling(object sender,EventArgs argument) – Michael Gabbay Jan 22 '21 at 12:23
  • 2
    Take a look at these tutorials: [Delegates (TutorialsPoint)](https://www.tutorialspoint.com/csharp/csharp_delegates.htm) and [Events (TutorialsPoint)](https://www.tutorialspoint.com/csharp/csharp_events.htm) and [Delegates (TutorialsTeacher)](https://www.tutorialsteacher.com/csharp/csharp-delegates) and [Events (TutorialsTeacher)](https://www.tutorialsteacher.com/csharp/csharp-event). –  Jan 22 '21 at 12:23
  • Does this answer your question? [When & why to use delegates?](https://stackoverflow.com/questions/2019402/when-why-to-use-delegates) and [Where do I use delegates?](https://stackoverflow.com/questions/31497/where-do-i-use-delegates) –  Jan 22 '21 at 12:24
  • 1
    What technology do you use (WinForms, WPF, UWP, ASP.NET, GTK...)? You may also find this helpful: [Event Overview (Windows Forms .NET)](https://learn.microsoft.com/dotnet/desktop/winforms/forms/events) and [Event order in Windows Forms](https://learn.microsoft.com/dotnet/desktop/winforms/order-of-events-in-windows-forms) –  Jan 22 '21 at 12:28
  • @Olivier Rogier in this case it is WPF but in future I could experience the problem in other technologies. – Paul McCarthy Jan 26 '21 at 13:13

1 Answers1

1

For example, suppose you create a userControl and your control has a button, and when the button text changes, you define an event for it. That is, you see where the button text changes and there you define an event for it.

 public event TextChangedEventHandler ItemTextChanged;
 public delegate void TextChangedEventHandler(object Sender);




 private void btnContent_Click(object sender, RoutedEventArgs e)
 {
      if (ItemTextChanged != null)
      {
           ItemTextChanged(txtbChildBtn);
      }
 }

Same example with another name

public event ValueChangedEventHandler ItemValueChanged;
public delegate void ValueChangedEventHandler(object Sender);




private void btnContent_Click(object sender, RoutedEventArgs e)
{
   if (ItemValueChanged != null)
   {
       ItemValueChanged(sender);
   }
}

or FucosChanged button

 public event FucosEventHandler FucosChanged;
 public delegate void FucosEventHandler(object Sender, DependencyPropertyChangedEventArgs e);

 private void btnContent_FocusableChanged(object sender, , DependencyPropertyChangedEventArgs e)
 {
      if (FucosChanged != null)
      {
           FucosChanged(sender, e);
      }
 }
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17
  • Yes, but how do you know it is TextChangedEventHandler rather that TextFontChangedEventhandler or some other name. Is there a list of the allowed ones somewhere? – Paul McCarthy Jan 25 '21 at 10:17
  • Ah, I see what I was looking for. It's in the metadata for TextBoxBase e.g. public static readonly RoutedEvent TextChangedEvent; – Paul McCarthy Jan 25 '21 at 10:31
  • 1
    Using the delegete keyword, you can select any name and create any type of event without any restrictions. – Meysam Asadi Jan 25 '21 at 10:42
  • @meyasm asadi You are correct, but what I was looking for was where I could get what type of events are available. e.g. is there TextSpecialChar event available for textbox. The metadata provides this. – Paul McCarthy Jan 26 '21 at 13:11