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

qc6wkl3g  于 2023-02-13  发布在  其他
关注(0)|答案(3)|浏览(128)

我在为一个类开发WinForm应用程序时遇到了一个似乎找不到根源的bug。当我运行该应用程序时,除了一个错误标签外,一切正常。该标签本应显示不正确的用户输入。起初,我以为我为它编写了错误的事件处理程序。所以我在启动时停止隐藏它,但标签仍然丢失。我不确定是在某个后端文件中丢失了一些东西,还是只是丢失了一些非常明显的东西。
这是创建标签的函数。

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

再说一次,我不知道我做错了什么。任何帮助都将不胜感激。
谢谢你,

9jyewag0

9jyewag01#

您要在哪个控件中添加errorlabel?
正常的标签初始化应如下所示

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

brccelvz2#

我没有看到您要将标签添加到窗体控件集合的位置:

this.Controls.Add(this.ErrorLabel);

如果它不是Controls集合的成员,您将看不到它。
与此相关的是,如果您定义的其他按钮、复选框等也因为同样的原因没有显示在表单上,我不会感到惊讶。
通常,这将由Designer.cs文件自动处理。

hof1towb

hof1towb3#

这可能是一个显而易见的答案,但它完全超出了我的理解范围。我验证了我的代码具有this.groupBox2.Controls.Add(this.ErrorLabel);,就像其他人在这里提到的那样。我不明白为什么我没有看到我的标签。我的标签是一个星号*,在设计器窗口中,我将其调整为非常小的大小。但是我可以在设计器窗口中看到*。原来我只需要在设计器窗口中增加它的区域,因为当我实际呈现应用程序时,它太小了。

相关问题