简体   繁体   中英

Visual Studio 2019 winforms custom control designer problem

I have a Visual Studio 2019 / Windows Forms problem. I'm getting - "A chart element with the name 'ChartArea1' already exists in the 'ChartAreaCollection'." So - what am I trying to do... I have created a custom control derived from System.Windows.Forms.DataVisualization.Charting.Chart. I call it XChart. I want to have a custom chart with predefined properties/areas/axis/colors/legends/series that will show up in the Toolbox when designing new forms. Everything works except for one thing, and that is NOT just for this control, it seems to be a general designer problem that probably has been around forever. As fast as I change ANYTHING in my form all the control properties get written down into MyForm.InitializeComponent(), which in turn makes all this below appear twice for the same chart - giving the error. The properties values don't stay in the custom control, they get copied all of them to the form, even though I haven't changed one of them.

It can't even be properly done with a TextBox. Let's say I create a custom control called XTextBox inherited from TextBox. The XTextBox.BackColor is default set to - let's say - Red. I then use this XTextBox in a number of places in my app. After a while I want to change my default BackColor to Yellow. So I change the XTextBox.BackColor to Yellow in my custom control and nothing happens because it still says Red in all my forms.

Any good ideas?

This is ruffly what my XChart.InitializeComponent() (and MyForm.InitializeComponent()) looks like:

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);

There's an article on MSDN that shows a ways to customize auto-generated code. Among other things it suggests using DesignerSerializationVisiblityAttribute , DefaultValueAttribute and ShouldSerialize<Property Name> method to suppress/force code generation.

Another way to customize the generated code is implementation of a custom CodeDomSerializer on the control. Check this article for details

This is a normal situation. You can solve it by rebuilding the project. After you rebuild it and drag a new one onto the Form, it will be "Yellow".

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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