0

Hope I can get some help with my problem, might be a simple fix. I'm trying to create a custom chart control (inheriting from the Chart class). In my constructor, I'm adding a ChartArea and Legend. Everything is fine when I add the control to the form, but when I run it, I get an error in the form designer saying a chart area already exists with the same name as the one I added in the constructor. So my problem is that the chart area is trying to be added a second time in the form designer generated code. I could remove this from the generated code but I wanted to see if there was an easier way to control it in my custom chart class. The constructor code for the chart is:

    public MultiFunctionalGraph(DataForGraph dataA, DataForGraph dataB, DataForGraph dataC, DataForGraph dataD)
    {
        this.dataA = dataA;
        this.dataB = dataB;
        this.dataC = dataC;
        this.dataD = dataD;

        ChartArea chartArea = new ChartArea();
        Legend legend = new Legend();
        Axis xAxis = new Axis(chartArea, AxisName.X);
        Axis yAxis = new Axis(chartArea, AxisName.Y);

        chartArea.Name = "ChartArea";
        chartArea.Visible = true;
        this.ChartAreas.Add(chartArea);
        legend.Name = "Legend";
        this.Legends.Add(legend);

    }

And I get this in the code generation in the designer:

        this.Graph1 = Graph();
        ((System.ComponentModel.ISupportInitialize)(this.Graph1)).BeginInit();

        // Graph1
        // 
        this.Graph1.BackColor = System.Drawing.Color.Transparent;
        chartArea1.AxisX.Interval = 5D;
        chartArea1.AxisX.MajorGrid.Interval = 10D;
        chartArea1.AxisX.MajorTickMark.Interval = 5D;
        chartArea1.AxisY.LabelStyle.Interval = 500D;
        chartArea1.AxisY.MajorGrid.Interval = 500D;
        chartArea1.AxisY.MajorTickMark.Interval = 500D;
        chartArea1.AxisY.MinorGrid.Interval = 500D;
        chartArea1.AxisY.MinorTickMark.Interval = 500D;
        chartArea1.CursorX.LineColor = System.Drawing.Color.LimeGreen;
        chartArea1.CursorX.LineWidth = 2;
        chartArea1.CursorY.LineColor = System.Drawing.Color.LimeGreen;
        chartArea1.CursorY.LineWidth = 2;
        chartArea1.Name = "ChartArea";
        this.Graph1.ChartAreas.Add(chartArea1);
        legend1.AutoFitMinFontSize = 5;
        legend1.Name = "Legend";
        legend1.TextWrapThreshold = 20;
        this.Graph1.Legends.Add(legend1);
        this.Graph1.Location = new System.Drawing.Point(46, 302);
        this.Graph1.Name = "Graph1";
        this.Graph1.Size = new System.Drawing.Size(569, 300);
        this.Graph1.TabIndex = 7;
        this.Graph1.Text = "Graph1";

So basically I have to stop generating the code to add the chart area in the designer. I think I can get it using the DesignerSerialize attribute somewhere but I could really use some help. Thank you!

5
  • Have you a) tried to remove the generated ChartArea before adding yours? b) considered using a different name, which would mena to live with 2 ChartAreas from the start or c) simply using the generated ChartArea? Commented Jun 2, 2014 at 15:29
  • My problem is that in the constructor of the custom chart I am adding a chart area, say "ChartArea1". When I add this chart to the form, the generated code in the designer automatically adds this same line, so it adds a chart area named "ChartArea1". Now that the same thing is being called in both the designer and constructor, I am trying to add two chart areas with the same name, which blows up. I need to somehow have the designer not generate that code. Commented Jun 2, 2014 at 15:38
  • Hm. Just came across this post. Is your custom Chart in a separate library project? Commented Jun 2, 2014 at 16:13
  • It's in the same project as the form I'm trying to add it to. I also came across that post but didn't think much of it as I wasn't getting anywhere close to the same problem as posted. It wouldn't hurt to put it in a separate library project I suppose.. I'll give it a try. Commented Jun 2, 2014 at 16:21
  • If you succeed, please do keep us posted! tia Commented Jun 3, 2014 at 9:24

2 Answers 2

0

In the graphical designer, open the ChartAreas collection property on the Chart, and remove anything currently there.

ETA: You can also do this programatically, in the constructor after the InitializeComponent method is called:

public MyChartClass()
{
    InitializeComponent();
    ChartAreas.Clear();
    ChartAreas.Add("ChartArea");
}
Sign up to request clarification or add additional context in comments.

1 Comment

This works, but its not quite the path I'm trying to take. I did resolve the problem in a round about way though, thank you for the input.
0

Well I solved it in a round about way. Instead of extending the Chart control, I extended the UserControl class and added a chart object in there. Now I can control it much easier with less hassle. Thanks all for the help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.