Visual Studio 2019 winforms自定义控件设计器问题

Visual Studio 2019 winforms custom control designer problem

我有一个 Visual Studio 2019 / Windows 表格问题。 我得到 - "A chart element with the name 'ChartArea1' already exists in the 'ChartAreaCollection'." 所以 - 我想做什么...... 我创建了一个派生自 System.Windows.Forms.DataVisualization.Charting.Chart 的自定义控件。 我称之为 XChart。 我想要一个带有预定义 properties/areas/axis/colors/legends/series 的自定义图表 在设计新表单时将显示在工具箱中。 一切正常,除了一件事,那不仅仅是为了这个控制, 这似乎是一个普遍的设计师问题,可能永远存在。 只要我在我的表单中更改任何内容,所有控件属性都会被写下来 进入 MyForm.InitializeComponent(),这反过来又使下面的所有内容在同一图表中出现两次 - 给出错误。 属性值不会保留在自定义控件中,它们会全部复制到表单中,即使我 没有改变其中之一。

它甚至不能用 TextBox 正确完成。 假设我创建了一个继承自 TextBox 的名为 XTextBox 的自定义控件。 XTextBox.BackColor 默认设置为 - 比方说 - 红色。 然后我在我的应用程序的许多地方使用这个 XTextBox。 过了一会儿,我想将我的默认背景颜色更改为黄色。 所以我在我的自定义控件中将 XTextBox.BackColor 更改为 Yellow 并且没有任何反应 因为在我的所有表格中它仍然显示红色。

有什么好主意吗?

这就是我的 XChart.InitializeComponent()(和 MyForm.InitializeComponent())的样子:

System.Windows.Forms.DataVisualization.Charting.ChartArea area = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.Legend legend = new System.Windows.Forms.DataVisualization.Charting.Legend();
System.Windows.Forms.DataVisualization.Charting.Series serie0 = new System.Windows.Forms.DataVisualization.Charting.Series();
System.Windows.Forms.DataVisualization.Charting.Series serie1 = new System.Windows.Forms.DataVisualization.Charting.Series();
System.Windows.Forms.DataVisualization.Charting.Title title = new System.Windows.Forms.DataVisualization.Charting.Title();
// 
// XChart
// 
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(28)))), ((int)(((byte)(28)))));
area.AxisX.LabelStyle.ForeColor = System.Drawing.Color.SeaShell;
area.AxisX.LabelStyle.Format = "0.000";
area.AxisX.LineColor = System.Drawing.Color.DarkGray;
area.AxisX.MajorGrid.LineColor = System.Drawing.Color.DarkGray;
area.AxisX.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dot;
area.AxisX.MajorTickMark.LineColor = System.Drawing.Color.DarkGray;
area.AxisX.MajorTickMark.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dot;
area.AxisX.MinorTickMark.Enabled = true;
area.AxisX.ScrollBar.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(28)))), ((int)(((byte)(28)))));
area.AxisX.ScrollBar.ButtonColor = System.Drawing.Color.Gray;
area.AxisX.ScrollBar.ButtonStyle = System.Windows.Forms.DataVisualization.Charting.ScrollBarButtonStyles.None;
area.AxisX.ScrollBar.IsPositionedInside = false;
area.AxisX.ScrollBar.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(28)))), ((int)(((byte)(28)))));
area.AxisX.ScrollBar.Size = 16D;
area.AxisX2.ScrollBar.BackColor = System.Drawing.Color.White;
area.AxisX2.ScrollBar.ButtonColor = System.Drawing.Color.Silver;
area.AxisX2.ScrollBar.ButtonStyle = System.Windows.Forms.DataVisualization.Charting.ScrollBarButtonStyles.None;
area.AxisY.LabelStyle.ForeColor = System.Drawing.Color.SeaShell;
area.AxisY.LabelStyle.Format = "0.000";
area.AxisY.LineColor = System.Drawing.Color.DarkGray;
area.AxisY.MajorGrid.LineColor = System.Drawing.Color.DarkGray;
area.AxisY.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dot;
area.AxisY.MajorTickMark.LineColor = System.Drawing.Color.DarkGray;
area.AxisY.MajorTickMark.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dot;
area.AxisY.MinorTickMark.Enabled = true;
area.AxisY.ScrollBar.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(28)))), ((int)(((byte)(28)))));
area.AxisY.ScrollBar.ButtonColor = System.Drawing.Color.Gray;
area.AxisY.ScrollBar.ButtonStyle = System.Windows.Forms.DataVisualization.Charting.ScrollBarButtonStyles.None;
area.AxisY.ScrollBar.IsPositionedInside = false;
area.AxisY.ScrollBar.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(28)))), ((int)(((byte)(28)))));
area.AxisY.ScrollBar.Size = 16D;
area.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(28)))), ((int)(((byte)(28)))), ((int)(((byte)(28)))));
area.Name = "ChartArea1";
area.Position.Auto = false;
area.Position.Height = 97F;
area.Position.Width = 90F;
area.Position.Y = 3F;
this.ChartAreas.Add(area);
legend.BackColor = System.Drawing.Color.Transparent;
legend.ForeColor = System.Drawing.Color.SeaShell;
legend.Name = "Legend1";
legend.TitleForeColor = System.Drawing.Color.Empty;
this.Legends.Add(legend);
this.Location = new System.Drawing.Point(25, 149);
this.Name = "chart1";
serie0.ChartArea = "ChartArea1";
serie0.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;
serie0.Legend = "Legend1";
serie0.Name = "0";
serie1.ChartArea = "ChartArea1";
serie1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;
serie1.Legend = "Legend1";
serie1.Name = "1";
this.Series.Add(serie0);
this.Series.Add(serie1);
this.Size = new System.Drawing.Size(300, 200);
this.TabIndex = 3;
this.Text = "chart1";
title.ForeColor = System.Drawing.Color.SeaShell;
title.Name = "Title1";
title.Position.Auto = false;
title.Position.Height = 2.59811F;
title.Position.Width = 94F;
title.Position.Y = 1F;
title.Text = "Titles[0]";
this.Titles.Add(title);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnMouseUp);

MSDN 上有一篇文章介绍了自定义自动生成代码的方法。除其他外,它建议使用 DesignerSerializationVisiblityAttributeDefaultValueAttributeShouldSerialize<Property Name> 方法来 suppress/force 代码生成。

自定义生成代码的另一种方法是在控件上实现自定义 CodeDomSerializer。检查 this article 了解详情

这是正常情况。您可以通过重建项目来解决它。重建它并将一个新的拖到窗体上后,它将是 "Yellow"。