Resolved Under C# in VS 2022 basic problem for events.

Molo2003

New member
Joined
May 30, 2023
Messages
2
Programming Experience
3-5
Hello

A basic issue about events in a WindForm program in Visual Studio 2022 Enterprise

I had abandoned the dev in C#; Help me

My code:

First class:
public partial class Form2 : Form
{
    public EventHandler event_momo;     //  déclaration de l'évènement

    //  déjà ici, dans debogage,  "event_momo = null"  mais de type EventHandler  ????

    //....

    //  plus loin, dans  la méthode qui va déclencher l'évènement

    EventHandler eh = event_momo;

    if ( eh != null )
    {
        eh.Invoke(this, EventArgs.Empty);
    }

    //....
}
Second class:
public partial class Form1 : Form
{
    //   pour abonner une méthode à l'évènement

    Form2  f2  =  new Form2();

    f2.event_momo += Maj_fichier;

    //....
}

Obviously, I am here because no event is triggered.

I can't see my mistake.

In the debugging of the app, I always find "event_momo" of type "EventHandler" but always value "= null" (so ditto for "eh").

When running the app, the compiler is silent ... But no event is launched.

What for???? Thank you for your help.

In this rather simple app, all events related to controls (like click on button ...) work wonderfully ....

Molo
 
Last edited by a moderator:
Solution
The problem is that you haven't declared an event anywhere. You have simply declared a field of type EventHandler, not an actual event. An event requires the event keyword. You would usually setup an event like this:
C#:
public event EventHandler SomeEvent;

protected virtual void OnSomeEvent(EventArgs e)
{
    SomeEvent?.Invoke(this, e);
}
When you want to raise the SomeEvent event, you do this:
C#:
OnSomeEvent(EventArgs.Empty);
It is good practice to raise the event in a method named after it, with the prefix "On". By declaring it protected virtual, derived classes can override the behaviour, e.g. when you create a custom control, you might override the OnPaint method to perform custom drawing...
This site accepts post in English only. I have translated your post this time. Any future posts that are not in English will be rejected.

I have also cleaned up and properly formatted the code. Please post readable code in future.
 
The problem is that you haven't declared an event anywhere. You have simply declared a field of type EventHandler, not an actual event. An event requires the event keyword. You would usually setup an event like this:
C#:
public event EventHandler SomeEvent;

protected virtual void OnSomeEvent(EventArgs e)
{
    SomeEvent?.Invoke(this, e);
}
When you want to raise the SomeEvent event, you do this:
C#:
OnSomeEvent(EventArgs.Empty);
It is good practice to raise the event in a method named after it, with the prefix "On". By declaring it protected virtual, derived classes can override the behaviour, e.g. when you create a custom control, you might override the OnPaint method to perform custom drawing each time the Paint event is raised.

Because an event of type EventHandler never has any data, you might prefer to do this:
C#:
public event EventHandler SomeEvent;

protected virtual void OnSomeEvent()
{
    SomeEvent?.Invoke(this, EventArgs.Empty);
}
and not have to pass in EventArgs.Empty each time. If you only do it in once place though, it's really no big deal and it means that it will use the same pattern as events that do have their own data. You may like to check out my blog post on custom events here.
 
Solution

Latest posts

Back
Top Bottom