繁体   English   中英

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

[英]Visual Studio 2019 winforms custom control designer problem

我有一个 Visual Studio 2019 / Windows Forms 问题。 我得到了 - “名为 'ChartArea1' 的图表元素已经存在于 'ChartAreaCollection' 中。” 那么-我想做什么...我创建了一个从 System.Windows.Forms.DataVisualization.Charting.Chart 派生的自定义控件。 我称之为 XChart。 我想要一个带有预定义属性/区域/轴/颜色/图例/系列的自定义图表,在设计新的 forms 时将显示在工具箱中。 一切正常,除了一件事,这不仅仅是这个控件,它似乎是一个可能永远存在的一般设计问题。 只要我更改表单中的任何内容,所有控件属性都会写入 MyForm.InitializeComponent(),这反过来又会使下面的所有内容在同一个图表中出现两次 - 给出错误。 属性值不会保留在自定义控件中,它们会全部复制到表单中,即使我没有更改其中之一。

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

有什么好主意吗?

这就是我的 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>方法来抑制/强制代码生成。

自定义生成代码的另一种方法是在控件上实现自定义CodeDomSerializer 查看这篇文章了解详情

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

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM