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

我有一个Visual Studio 2019 / Windows窗体问题。 我得到-“在'ChartAreaCollection'中已经存在一个名称为'ChartArea1'的图表元素”。 所以-我想做什么... 我创建了一个自System.Windows.Forms.DataVisualization.Charting.Chart派生的自定义控件。 我称它为XChart。 我想要一个具有预定义属性/区域/轴/颜色/传奇/系列的自定义图表 在设计新表单时,它将显示在“工具箱”中。 除了一件事情,所有的东西都可以正常工作,而不仅仅是这个控制, 这似乎是一个普遍存在的设计师问题,可能一直存在。 只要我在表单中进行任何更改,所有控件属性都会被记录下来 放入MyForm.InitializeComponent()中,这反过来又使以下所有内容在同一张图表中出现两次-给出错误。 属性值不会保留在自定义控件中,它们会被复制到表单中,即使我 没有改变其中之一。

使用TextBox甚至无法正确完成。 假设我创建了一个继承自TextBox的自定义控件XTextBox。 XTextBox.BackColor默认设置为-假设-红色。 然后,我在应用程序中的多个位置使用此XTextBox。 一段时间后,我想将默认的BackColor更改为Yellow。 所以我在自定义控件中将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)))));
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)))));
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)))));
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)))));
area.AxisY.ScrollBar.Size = 16D;
area.BackColor = System.Drawing.Color.FromArgb(((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);
qinhao8438 回答:Visual Studio 2019 Winforms自定义控件设计器问题

MSDN上有一篇文章,展示了一种自定义自动生成代码的方法。除其他外,它建议使用DesignerSerializationVisiblityAttributeDefaultValueAttributeShouldSerialize<Property Name>方法来抑制/强制生成代码。

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

,

这是正常情况。您可以通过重建项目来解决。重建它并将新的拖动到窗体上后,它将为“黄色”。

enter image description here

本文链接:https://www.f2er.com/3118138.html

大家都在问