正在调试WinForm应用程序中缺少的标签

本文关键字:标签 应用程序 调试 WinForm | 更新日期: 2023-09-27 17:59:01

我正在为一个类开发WinForm应用程序,遇到了一个似乎找不到根源的错误。当我运行该应用程序时,除了一个错误标签外,其他一切都正常工作,该错误标签本应带有不正确的用户输入。一开始我以为我为它编写了错误的事件处理程序,所以我在启动时停止了隐藏它,但标签仍然不见了。我不确定我是在某个后端文件中遗漏了什么,还是只是遗漏了一些非常明显的东西。

这是创建标签的函数。

private void InitializeErrorLabel()
{
    int width = 200, height = 13,
        anchorY = this.Label.Location.Y - this.Label.Size.Height - 3;
    // Initialize Component
    this.ErrorLabel.AutoSize = true;
    this.ErrorLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold,
        System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.ErrorLabel.ForeColor = System.Drawing.Color.Red;
    this.ErrorLabel.Location = new System.Drawing.Point((XSize - width) / 2, (anchorY - height));
    this.ErrorLabel.Anchor = System.Windows.Forms.AnchorStyles.Top;
    this.ErrorLabel.Name = "ErrorLabel";
    this.ErrorLabel.Size = new System.Drawing.Size(width, height);
    this.ErrorLabel.Text = "Invalid User ID. Please try again!";
    return;
}

这是初始化我的控件的函数:

private void InitializeComponent()
{
    this.UserInput = new System.Windows.Forms.TextBox();
    this.SwitchMajor = new System.Windows.Forms.RadioButton();
    this.SwitchToCS = new System.Windows.Forms.CheckBox();
    this.SwitchToCE = new System.Windows.Forms.CheckBox();
    this.KeepMajor = new System.Windows.Forms.RadioButton();
    this.AcceptValues = new System.Windows.Forms.Button();
    this.Label = new System.Windows.Forms.Label();
    this.ErrorLabel = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // Initialize Components
    this.InitializeLabel();
    this.InitializeMainWindow();
    this.InitializeUserInput();
    this.InitializeSwitchMajorBtn();
    this.InitializeChangeToCSBtn();
    this.InitializeChangeToCEBtn();
    this.InitializeAcceptValuesBtn();
    this.InitializeErrorLabel();
    this.ResumeLayout();
    this.PerformLayout();
    return;
}

再说一遍,我真的不确定我在这里做错了什么。如有任何帮助,我们将不胜感激。

谢谢,

正在调试WinForm应用程序中缺少的标签

在什么控件中添加错误标签?

正常的标签初始化应该看起来像

private System.Windows.Forms.Label ErrorLabel;
this.ErrorLabel = new System.Windows.Forms.Label();
this.groupBox2.Controls.Add(this.ErrorLabel);
this.ErrorLabel.AutoSize = true;
this.ErrorLabel.Location = new System.Drawing.Point(8, 59);
this.ErrorLabel.Name = "ErrorLabel";
this.ErrorLabel.Size = new System.Drawing.Size(55, 13);
this.ErrorLabel.TabIndex = 69;
this.ErrorLabel.Text = "Address 2";
this.ErrorLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

重要

标签必须添加到像我的第三行这样的控件中。在您的情况下,您的控件可以是一个表单。在我的情况下,它是一个组框,组框本身必须添加到myform中,并且myform必须可见。

 this.groupBox2.Controls.Add(this.ErrorLabel);

我看不出有什么地方可以将标签添加到表单的控件集合中:

this.Controls.Add(this.ErrorLabel);

如果它不是Controls集合的成员,则不会看到它。

与此相关的是,如果出于同样的原因,您定义的其他按钮、复选框等也没有出现在表单上,我也不会感到惊讶。

通常情况下,这将由Designer.cs文件自动处理。

这可能是一个显而易见的答案,但它完全超出了我的想象。我验证了我的代码具有this.groupBox2.Controls.Add(this.ErrorLabel);,就像这里提到的其他代码一样。我不明白为什么我没有看到我的标签。我的标签是一个星号*,在设计器窗口中,我把它调整得很小,但我可以在设计器窗口看到*。事实证明,我只需要在设计器窗口中增加它的面积,因为当我真正渲染应用程序时,它太小了。