Adding Legends to charts programatically

jimctr

Well-known member
Joined
Dec 1, 2011
Messages
52
Programming Experience
Beginner
I am totally missing how to add a legend to a chart programmatically (versus dragging a chart object into designer). I thought this was done similar to creating a series or chart area. For example,

Dim Legend1 As New Legend()
Chart2.Legends.Add(Legend1)

This leads to the error: The chart element with the name Legend1 could not be found in the Legend collection.

If I use the example code provided from Samples Environment for Microsoft Chart Controls - Release: Samples for Chart Control - .NET Framework 4

and simply reference some "Default" as in (and assume the Legend already exists)

Chart1.Legends("Default").LegendStyle = LegendStyle.Column

I get a complaint about default.

Can someone help me instantiate a legend for a graph. Thanks
 
A simple way to get to know how this control (and any other) is arranged, is to design a chart in designer and look at the designer generated code for it. The code designer generates is the same you can do in your manual code.
To show designer code click 'show all files' in Solution Explorer, expand the form node and open the .Designer.vb file.
 
Back
Top