2021年4月30日星期五

C# Winforms add Chart as Control from seperate class. Chart not drawing

I'm adding a chart as a control created in a seperate class. The background of the chart paints, but the chart itself is not painting. Can someone point out where my error is? I've tried things like BringToFront, Anchoring, Dock.Fill, Invalidate.

using System.Windows.Forms;    namespace WindowsFormsApp2  {      public partial class Form1 : Form      {          ChartControl MyChart;          public Form1()          {              InitializeComponent();              MyChart = new ChartControl();              this.Controls.Add(MyChart);              MyChart.chart1.Series[0].Points.AddXY(1.4, 1.3);              MyChart.chart1.Series[0].Points.AddXY(1.7, 1.9);          }      }  }  

The Chart Class

using System.Windows.Forms.DataVisualization.Charting;    namespace WindowsFormsApp2  {      public class ChartControl : Chart      {          public Chart chart1;            public ChartControl()          {              ChartArea chartArea1 = new ChartArea();              Legend legend1 = new Legend();              Series series1 = new Series();              chart1 = new Chart();              ((System.ComponentModel.ISupportInitialize)chart1).BeginInit();              SuspendLayout();              chartArea1.Name = "ChartArea1";              chart1.ChartAreas.Add(chartArea1);              legend1.Name = "Legend1";              chart1.Legends.Add(legend1);              chart1.Location = new System.Drawing.Point(0, 0);              chart1.Name = "chart1";              series1.ChartArea = "ChartArea1";              series1.Legend = "Legend1";              series1.Name = "Series1";              chart1.Series.Add(series1);              chart1.Size = new System.Drawing.Size(300, 300);              chart1.TabIndex = 0;              chart1.Text = "chart1";                ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();              ResumeLayout(false);          }      }  }  
https://stackoverflow.com/questions/67342304/c-sharp-winforms-add-chart-as-control-from-seperate-class-chart-not-drawing May 01, 2021 at 10:05AM

没有评论:

发表评论