-1

I have an bitmap and an property like this :

   private Bitmap host_Bitmap;
   private Bitmap Host_Bitmap {get;set;}

How can I create an event when the host_Bitmap change?

  • Also MSDoc articles: [How to: Implement Property Change Notification](https://learn.microsoft.com/dotnet/desktop/wpf/data/how-to-implement-property-change-notification) and [INotifyPropertyChanged.PropertyChanged Événement](https://learn.microsoft.com/dotnet/api/system.componentmodel.inotifypropertychanged.propertychanged) –  Jan 04 '21 at 07:05
  • Also using simple events: [Data binding without INotifyPropertyChanged](https://updatecontrols.net/cs/index.html) –  Jan 04 '21 at 07:08

1 Answers1

3

If you want to take the simple route for one property, you add an event, and in the set you invoke it:

public event EventHandler BitmapChanged;

private Bitmap _hostBitmap;
public Bitmap HostBitmap { get => _hostBitmap;
  set{
    _hostBitmap = value;
    BitmapChanged?.Invoke(this, EventArgs.Empty);
  }
}

If you want to pass more info about the event you can provide a modified EventArgs subclass and declare the BitmapChanged property type to be EventHandler<YourEventArgsSubclass>

If you have a lot of properties to associate with events, look at implementing INotifyPropertyChanged

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Also instead of using `EventHandler<>` a [delegate](https://learn.microsoft.com/dotnet/standard/events/#delegates) can be created to be used for the type of the event, thus it will be strongly typed. –  Jan 04 '21 at 07:29
  • @OlivierRogier: while I don't find this answer to be of much use (there are plenty of duplicates already on the site), I don't understand your critique here. The `EventHandler` and `EventHandler` types are _both_ already delegate types. Using either creates just as "strongly typed" an event as using any other delegate type would. – Peter Duniho Jan 04 '21 at 07:39
  • @Olivier Yes, but that is probably only necessary in exceptional circumstances such as when in a context that doesn't understand the generic `EventHandler` - the [documentation](https://learn.microsoft.com/en-us/dotnet/standard/events/) outlines that it shouldn't really be necessary to do it so I'm not sure that saying "instead of" is "right", because (to me) it implies a circumstance that is commonly necessary, when it isn't necessary? – Caius Jard Jan 04 '21 at 07:41
  • What I meant by strongly typed on top of a generic is not a critic but just an alternative meaning that it can sometimes be easier and cleaner to use a dedicated type like for example a `MyPropertyChanged` delegate instead of a circumstantial `Eventhandler`. Otherwise I find the answer adapted and personalized. –  Jan 04 '21 at 07:46
  • Thank you for helping, I will try it out – Chinh Cross Jan 04 '21 at 07:49
  • @Olivier so.. breaking away from the standard event handler signature of `SomeName(object sender, EventArgs e)` perhaps? – Caius Jard Jan 04 '21 at 07:53
  • Oh, and I don't actually need to create any more delegate because all I want is when the bitmap changed, a method within that class will be called. – Chinh Cross Jan 04 '21 at 07:56
  • @CaiusJard I don't understand. Using a delegate is the .NET default pattern for every events. The signature is libre, indeed: we can respect the standard `(sender, args)` or use what we want even no parameters if not needed: `public delegate void MyPropertyChanged();`, so it will just trigger a simple callback. But using `EventHandler` is clean. –  Jan 04 '21 at 07:57
  • @ChinhCross The caius code creates an event, thus you need to assign it from the exterior to have it working. So every time the property is changed, this event is called if not null. Because it is defined as event you can add/remove many. If you remove [`event`](https://docs.microsoft.com/dotnet/standard/events/) keyword, it becomes one var you can assign only one time and any other assignment will erase the previous. Also you can add a check at the beginning of the setter to not update the same value: `if ( _hostBitmap == value ) return;` so we save an assignment and the event is not called . –  Jan 04 '21 at 08:02
  • @OlivierRogier But I still not understand this, do we need to use the property somewhere? or all we need is to register for the event to trigger it? – Chinh Cross Jan 04 '21 at 08:26
  • @ChinhCross Once you registered the event somewhere, every time you assign a value to the property, you will get the event handler called. Isn't your goal? –  Jan 04 '21 at 08:29
  • @OlivierRogier oh sorry for misunderstanding, what I meant is when I try to register the event when the form is load, then change the value _hostBitmap, I see the HostBitmap still grey out (not being used), and whenever I tried to change the _hostBitmap, not thing happen – – Chinh Cross Jan 04 '21 at 08:51
  • @ChinhCross You need to change the property `HostBitmap` and not the field `_HostBitmap` to have the event working. –  Jan 04 '21 at 08:52
  • @OlivierRogier It worked! Thank you very much, but then what does the `_hostBitmap` has to do anything with this? I saw most of the common event problem solution has it, but I still don't know what is it for – Chinh Cross Jan 04 '21 at 08:58
  • Read for example: https://www.tutorialspoint.com/csharp/csharp_properties.htm & https://www.geeksforgeeks.org/c-sharp-properties & https://www.c-sharpcorner.com/article/understanding-properties-in-C-Sharp & http://www.functionx.com/csharp/introduction/Lesson20.htm. Also video: https://www.youtube.com/watch?v=uYXQH2QFmqk&list=PLAC325451207E3105&index=26 & https://www.youtube.com/watch?v=iGR425gMKeA&list=PLAC325451207E3105&index=27 & https://www.youtube.com/watch?v=_jbdVayepNE –  Jan 04 '21 at 08:59