[英]Visual C# Stop Code Generation For Custom Control
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).
我正在尝试创建一个自定义图表控件(从Chart类继承)。 In my constructor, I'm adding a ChartArea and Legend.
在我的构造函数中,我添加了一个ChartArea和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.
我想我可以在某处使用DesignerSerialize属性来获取它,但我确实可以使用一些帮助。 Thank you!
谢谢!
In the graphical designer, open the ChartAreas
collection property on the Chart
, and remove anything currently there. 在图形设计器中,打开
Chart
上的ChartAreas
集合属性,然后删除当前存在的任何内容。
ETA: You can also do this programatically, in the constructor after the InitializeComponent
method is called: ETA:您也可以在调用
InitializeComponent
方法的构造函数中以编程方式执行此操作:
public MyChartClass()
{
InitializeComponent();
ChartAreas.Clear();
ChartAreas.Add("ChartArea");
}
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.
我没有扩展Chart控件,而是扩展了UserControl类,并在其中添加了一个图表对象。 Now I can control it much easier with less hassle.
现在,我可以以更少的麻烦轻松控制它。 Thanks all for the help!
谢谢大家的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.