Dumbo
Dumbo

Reputation: 14122

Event for adding or removing objects to a List

I am working on a control to draw some graphs. For this purpose I want to derive a control from PictureBox and add a field to it which basically a List of following simple class:

public class Curve
{
     public List<PointF> DataPoints;
     public Color CurveColor;
     //and constructor and stuff
}

And the PictureBox class:

public class Graph : PictureBox
{
    List<Curve> Curves;
    //And some code to take care of drawing the curves
}

So I really apriciate some tips here, is there a way to hook up an event so whenever a Curve object is being added or removed in the Graph class, it redraws itself with remaining curve objects?

I found some details here here but this is for ArrayList

Upvotes: 0

Views: 502

Answers (1)

Matx
Matx

Reputation: 265

One way could be to use an ObservableCollection into your Graph class then listen to its CollectionChanged Event to Redraw your Graph.

public class Graph : PictureBox {
    ObservableCollection<Point> Curves;
    //And some code to take care of drawing the curves

    public Graph() {
      Curves.CollectionChanged += Curves_CollectionChanged;
    }

    void Curves_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
      Redraw();
    }
}

Upvotes: 5

Related Questions