Error when trying to add specific UserControls in the Form's Designer

Viewed 99

At some point in the development of this private project, some UserControls disappeared from the designer. They were still visible when executing the project, since they are declared, initialized and added to the window in the Form1.Designer.cs file.
I first ignored this problem, since I thought it was just a visual bug or something similar. Later in the development process, I had to visually design something around these UserControls, which forced me to fix the problem with this problem.
When trying to add a new instance of the UserControl PartUC, I received a similar if not the same error as below (just a really long error message). This error message occurred when trying to add the UserControl PartUC as well as completely new UserControls. Adding a new AmpPropertiesUC or PartPropertiesUC UserControl is no problem at all (this is what confuses me the most, it's not that every UserControl works except for one or two (as you would probably expect). It's exactly the other way around. None of the UserControls work except for two)

After not really finding a solution to solve this problem, I decided to redesign the Window in a completely new project. I used the Form1.Designer.cs file as a reference for the names, locations, sizes, colors, etcetera to speed up the process.
After I was done redesigning the main window and the PartUC UserControl, I added the AmpPropertiesUC and the PartPropertiesUC UserControl to the Project's directory and built the project, so they appear in the Toolbox.

Now, I added the AmpPropertiesUC and the PartPropertiesUC UserControls to the Form in the designer. After adding these, I tried to add the newly recreated PartUC to the newly recreated Form, but I still can't add the UserControl, even though I redesigned the window as well as the UserControl using the .Designer.cs file as a reference.
I am now able to add new, empty UserControls, but I still can't add the PartUC UserControl.

At this point, I'm just completely out of ideas of what could be the cause of this problem, since I also can't think of anything in the souce code, which could cause this error.

Here's a screenshot of the error message:

Error Message occurring, when trying to add the UserControl PartUC

and here's a screenshot of what the Form (from the old project) looks like with the PartUC UserControls (red) as well as a PartUC with some properties changed (blue) in the properties tab (green), which is a PartPropertiesUC UserControl:

enter image description here

Here's the source code for the PartUC UserControl:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ActiveFilterSimulator
{
    public delegate void PartUCEventHandler(PartUC sender);

    public partial class PartUC : UserControl
    {
        public event PartUCEventHandler PartClicked;

        private string _partName { set; get; }
        public string PartName
        {
            set
            {
                _partName = value;
                if ((_partType == "Open") || (_partType == "Connection"))
                    lName.Text = "";
                else
                    lName.Text = _partName;
            }
            get
            {
                return _partName;
            }
        }

        private double _partValue { set; get; }
        public double PartValue
        {
            set
            {
                _partValue = value;
                ScientifficValue scientifficValue = ScientifficValue.ParseValueToScientifficValue(value);

                if ((PartType == "Open") || (PartType == "Connection"))
                    lValue.Text = "";
                else
                    lValue.Text = scientifficValue.BareValue.ToString() + GetExtension(_partType, scientifficValue.ValueScientiffic);
            }
            get
            {
                return _partValue;
            }
        }

        private string _partType { set; get; }
        public string PartType
        {
            set
            {
                _partType = value;
                switch (_partType)
                {
                    case "Connection":
                        PartSymbolPannel.BackgroundImage = Image.FromFile(getPrivateImageFolder() + "Connection.png");
                        break;
                    case "Resistor":
                        PartSymbolPannel.BackgroundImage = Image.FromFile(getPrivateImageFolder() + "Resistor.png");
                        break;
                    case "Capacitor":
                        PartSymbolPannel.BackgroundImage = Image.FromFile(getPrivateImageFolder() + "Capacitor.png");
                        break;
                    case "Inductor":
                        PartSymbolPannel.BackgroundImage = Image.FromFile(getPrivateImageFolder() + "Inductor.png");
                        break;
                    default:
                        //Open Connection / Not Connected
                        _partType = "Open";
                        PartSymbolPannel.BackgroundImage = Image.FromFile(getPrivateImageFolder() + "Open.png");
                        break;
                }

                PartName = _partName;
                PartValue = _partValue;
            }
            get
            {
                return _partType;
            }
        }

        public PartUC()
        {
            InitializeComponent();

            PartName = "";
            PartValue = 1.0f;
            PartType = "Open";
        }

        private void InvokeClickedEvent(object sender, EventArgs e)
        {
            PartUCEventHandler handler = PartClicked;
            handler?.Invoke(this);
        }

        private string getPrivateImageFolder()
        {
            string CurrentDir = Directory.GetCurrentDirectory();
            CurrentDir = CurrentDir.Replace(@"bin\Debug\net6.0-windows", @"PartSymbols\");
            return CurrentDir;
        }

        private string GetExtension(string type, int scientiffic)
        {
            string scientifficCharacter;

            switch (scientiffic)
            {
                case 18:
                    scientifficCharacter = "E"; //Exa
                    break;
                case 15:
                    scientifficCharacter = "P"; //Peta
                    break;
                case 12:
                    scientifficCharacter = "T"; //Tera
                    break;
                case 9:
                    scientifficCharacter = "G"; //Giga
                    break;
                case 6:
                    scientifficCharacter = "M"; //Mega
                    break;
                case 3:
                    scientifficCharacter = "k"; //kilo
                    break;
                case 0:
                    scientifficCharacter = ""; //1
                    break;
                case -3:
                    scientifficCharacter = "m"; //milli
                    break;
                case -6:
                    scientifficCharacter = "µ"; //micro
                    break;
                case -9:
                    scientifficCharacter = "n"; //nano
                    break;
                case -12:
                    scientifficCharacter = "p"; //pico
                    break;
                case -15:
                    scientifficCharacter = "f"; //femto
                    break;
                case -18:
                    scientifficCharacter = "a"; //atto
                    break;
                default:
                    scientifficCharacter = "*"; //Unit prefix out of range
                    break;
            }

            switch (_partType)
            {
                case "Connection":
                    return "";
                case "Resistor":
                    return scientifficCharacter + "Ω";
                case "Capacitor":
                    return scientifficCharacter + "F";
                case "Inductor":
                    return scientifficCharacter + "H";
                default:
                    return "";
            }
        }
    }
}

and here's the source code (generated by visual studio of course) from the designer file of the PartUC UserControl (PartUC.Designer.cs):

namespace ActiveFilterSimulator
{
    partial class PartUC
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.PartSymbolPannel = new System.Windows.Forms.Panel();
            this.lName = new System.Windows.Forms.Label();
            this.lValue = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // PartSymbolPannel
            // 
            this.PartSymbolPannel.Location = new System.Drawing.Point(0, 0);
            this.PartSymbolPannel.Name = "PartSymbolPannel";
            this.PartSymbolPannel.Size = new System.Drawing.Size(75, 75);
            this.PartSymbolPannel.TabIndex = 0;
            // 
            // lName
            // 
            this.lName.AutoSize = true;
            this.lName.Font = new System.Drawing.Font("Open Sans Light", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.lName.Location = new System.Drawing.Point(81, 5);
            this.lName.Name = "lName";
            this.lName.Size = new System.Drawing.Size(57, 23);
            this.lName.TabIndex = 1;
            this.lName.Text = "lName";
            // 
            // lValue
            // 
            this.lValue.AutoSize = true;
            this.lValue.Font = new System.Drawing.Font("Open Sans Light", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.lValue.Location = new System.Drawing.Point(81, 28);
            this.lValue.Name = "lValue";
            this.lValue.Size = new System.Drawing.Size(53, 23);
            this.lValue.TabIndex = 2;
            this.lValue.Text = "lValue";
            // 
            // PartUC
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.lValue);
            this.Controls.Add(this.lName);
            this.Controls.Add(this.PartSymbolPannel);
            this.Name = "PartUC";
            this.Size = new System.Drawing.Size(200, 75);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private Panel PartSymbolPannel;
        private Label lName;
        private Label lValue;
    }
}

Here's the link to the whole repository in case you need any other files: https://github.com/HeCoding180/ActiveFilterSimulator

Thanks a lot in advance!

1 Answers

Add this line of code then close your solution,Rebuild and try. Add this line of code on load UC

 if (!this.DesignMode)
 {
    InitializeComponent();
 }
Related