Form Load Issue with Chart

bones

Well-known member
Joined
Aug 23, 2014
Messages
143
Programming Experience
Beginner
I setup my basic chart parameters in my from load event. This works great normally but in this instance there is an issue because the form can get closed and reopened.

The chart gets it's data from another form's DGV. On the form with the DGV is a command button to open the chart form.

If the chart form is opened and closed and reopened, I get an error telling me that the series already exists....OK, makes perfect sense. I a trying to code the form load event to see if the series already exists and take action based on that. It's not working. I looked at some examples but couldn't find anything in VB, [all in C]. I thought this code would do the trick but the If Not line seems to be ignored and it blows up with "series of name1 already exists....

VB.NET:
If Not Chart1.Series.NextUniqueName = ("name1") Then
            Chart1.Series.Add("name1")
            Chart1.Series(0).Color = Color.Black
            Chart1.Series(0).BorderWidth = 2
            Chart1.Series(0).IsVisibleInLegend = True
            Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Spline
        Else
            Exit Sub
 
Hold the phone....I solved it. For the benefit of any other newbies.... Using series count did the trick. In my case <=5 was the required number base on number of series [6]. Counts start at 0

Dim countOfSeries As Integer = Chart1.Series.Count
Chart1.Series.Count <= 5
 
Back
Top