How to modify an existing instance of a chart?

Gualino

Member
Joined
Sep 27, 2011
Messages
10
Programming Experience
Beginner
Hi all,

In the same class I have
VB.NET:
    Dim WithEvents YMDChart As Chart
    Dim WithEvents YMDChartArea As ChartArea

Then:
VB.NET:
    Public Sub PlotYMD() handles ButtonPlot.click

        YMDChart = New Chart
        YMDChartArea = New ChartArea("YMDChartArea")
        YMDChart.ChartAreas.Add(YMDChartArea)

        'Series
        For Me.Delta = DeltaMin To CDec(DeltaMax) Step DeltaIncrement
            YMDChart.Series.Add("Delta " & Round(Delta, 2).ToString & "°")
        Next

        For Me.Beta = BetaMin To CDec(BetaMax) Step BetaIncrement
            YMDChart.Series.Add("Beta " & Round(Beta, 2).ToString & "°")
        Next

...

Then:
VB.NET:
   Public Sub SetYMDToBlack() handles ButtonPlot.click

if checkbox.checked = true
            For Each serie As Series In YMDChart.Series
                serie.Color = Color.Black
            Next
end if

    End Sub
But here at line 5 I have an error saying that I am not refering to an existing instance whereas I am...

If think it is because at this step the YMDChart is dead even if it is present on my form. I have tried with the declaration "Static" but it is only possible within a method and is incompatible with "with event"...

How should I do?
 
Line 5? Everything you have there looks correct, except the two unindented lower case lines in SetYMDToBlack and the casing of your handles, but that was perhaps just illustrative? So if your withevents YMDChart variable has been assigned an object beforehand I don't see the problem.
 
Line 5? Everything you have there looks correct, except the two unindented lower case lines in SetYMDToBlack and the casing of your handles, but that was perhaps just illustrative? So if your withevents YMDChart variable has been assigned an object beforehand I don't see the problem.

Line 5: For Each serie As Series In YMDChart.Serie

In fact at this step YMDChart is nothing!

Yes that was for illustration...

My goal is to overlay a graph on the existing, but to make the reading easier I want to turn the first graph in black.

So I have a first button click that create the graph, and then if I check the case "Overlay" and do a second button click the existing graph is turned to black and the new one is overlaid.
But for now I am struggling at the step where I turn the first graph to black...

I already encountered this problem when I was trying to reset the chart zoom thanks to a single button. I was referring to the displayed chart but I had an error message saying that I was referring to a non existing instance...

In fact the one that will deliver me from this problem will help me to solve 3 problems at the same time...:tennis:
 
Last edited:
In fact at this step YMDChart is nothing!
That simply means you have not created a Chart object and assigned to the YMDChart class field yet. PlotYMD method would indicate that you did, but if YMDChart is Nothing then you didn't.

I'm not sure what you really intend to express with your pseudo code handles, but currently what you have posted is "handles ButtonPlot.click" for both methods. If you were to do that, that would mean you had two handlers for same control event, and you could not control which handler was called first, so SetYMDToBlack could very well be called before PlotYMD. You can easily control the order in which calls are made from a method though, so take care of that and be sure to create the object before you attempt to use it. If necessary check that a reference is not Nothing before utilizing it.
 
Back
Top