在这个私人项目开发的某个时刻,一些用户控件从设计器中消失了。它们在执行项目时仍然可见,因为它们是在
Form1.Designer.cs
文件中声明、初始化并添加到窗口的。PartUC
的新实例时,我收到了类似的错误,如下所示(只是一条非常长的错误消息)。尝试添加 UserControl PartUC
以及全新的 UserControl 时出现此错误消息。添加一个新的 AmpPropertiesUC
或 PartPropertiesUC
UserControl 完全没问题(这是最让我困惑的,并不是每个 UserControl 都可以工作,除了一个或两个(正如你可能期望的那样)。这正是另一个除了两个之外,没有一个可以工作)
在没有真正找到解决这个问题的解决方案之后,我决定在一个全新的项目中重新设计窗口。我使用
Form1.Designer.cs
文件作为名称、位置、大小、颜色等的参考,以加快流程。PartUC
UserControl 的重新设计后,我将 AmpPropertiesUC
和 PartPropertiesUC
UserControl 添加到项目目录并构建了项目,因此它们出现在工具箱中。
现在,我将
AmpPropertiesUC
和 PartPropertiesUC
UserControls 添加到设计器中的表单中。添加这些之后,我尝试将新重新创建的 PartUC
添加到新重新创建的表单中,但我仍然无法添加 UserControl,即使我使用 .Designer.cs 文件重新设计了窗口以及 UserControl参考。PartUC
UserControl。
此时,我完全不知道导致此问题的原因是什么,因为我也想不出源代码中的任何内容可能会导致此错误。
这是错误消息的屏幕截图:
这是表单(来自旧项目)的屏幕截图,带有
PartUC
用户控件(红色)以及属性选项卡(绿色)中的一些属性已更改(蓝色)的 PartUC
,即PartPropertiesUC
用户控件:
这是
PartUC
用户控件的源代码:
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 "";
}
}
}
}
这是来自
PartUC
UserControl (PartUC.Designer.cs
) 的设计器文件的源代码(当然是由 Visual Studio 生成的):
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;
}
}
如果您需要任何其他文件,这里是整个存储库的链接: https://github.com/HeCoding180/ActiveFilterSimulator
提前非常感谢!