0

I am building an app which displays a curve in 2D like for example the Math.Sin curve. The user is prompted to enter how many dots he wants to display on the plot. When he chooses the number of dots and enters the parameters for them - a new window is opened where the plot is displayed.

My question is, if there is a way, I can get back to the previous window and enter new parameters which will generate a new curve BUT display it together with the first one? My solution for now is for only one curve being displayed. Any time the Plot window is opened - a new instance of it is created, so I suppose I have to find a way to use the same instance since the window is not closed, only hidden, but I dont know how.

Checkout the following image: enter image description here

Palle Due
  • 5,929
  • 4
  • 17
  • 32

1 Answers1

0

As you have already assumed, you can begin by using the same instance of the Form which displays the PlotView. You could expose a method Update in the PlotDisplayWindow Form, which would then update the plot view with new points. For example, in your parent form.

PlotDisplayWindow plotDisplay;

private void RefreshPlot(object sender, EventArgs e)
{
    var dataPoints = GetNewDataPoints();
    if (plotDisplay == null)
    {
        plotDisplay = new PlotDisplayWindow();
        plotDisplay.Show();
    }
    plotDisplay.Update(dataPoints);
}

In your PlotDisplayWindow Form, you could initialize your Model when loading the Window for the first time and then, use the Update method to add more points to the Plot View. For example:

private void PlotDisplayWindow_Load(object sender, EventArgs e)
{
    this.plotView1.Model = new PlotModel { Title = "Example 1" };
}

public void Update(IEnumerable<DataPoint> points)
{
    this.plotView1.Model.Series.Add(new LineSeries { ItemsSource = points });
    this.plotView1.InvalidatePlot(true);
}

The PlotView.InvalidatePlot(true) would ensure the plot is refreshed and the newly added points are displayed.

Palle Due
  • 5,929
  • 4
  • 17
  • 32
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • Thank you for the reply. Unfortunetley this does not help. I get the "Object reference not set to an instance of an object" exception and it is thrown when I tried to call the Update() method on PlotDisplayWindow field which has no instance. I am not really sure why it has no instance since the PlotDisplay form is not closed.. – Richard Bashev Sep 17 '19 at 20:21
  • @RichardBashev If you have the null check as shown in the example, then a new instance of PlotDisplayWindow would be created if it is null. Could you debug and check if the conditional checks are executed ? – Anu Viswan Sep 18 '19 at 01:14
  • Yes, but I do not want to create a new instance. I want to use the current one. The problem is that if I Hide the form with the plot and open the one where I enter the parameters - the Plot Form Instance closes and I cant use it anymore if I dont create a new one.. I want to use the old instance of the Plot even if I hide the form.. – Richard Bashev Sep 18 '19 at 17:23
  • @RichardBashev It would be helpful if you could share your current code where you are handling the form instance. You could edit the Question to add code – Anu Viswan Sep 18 '19 at 21:12