WinForm应用程序中未显示标签

问题描述 投票:1回答:3

我正在为一个类开发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;
}

再次,我不确定我在做什么错。任何帮助,将不胜感激。

谢谢,

c# windows winforms label
3个回答
1
投票

您要在哪个控件中添加错误标签?

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

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;

重要

标签必须添加到类似于我的第三行的控件中。您的控件可以是您的情况下的一种形式。就我而言,它是一个groupbox,并且必须将group box本身添加到myform中,并且myform必须可见。

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

2
投票

我看不到将标签添加到窗体的控件集合的位置:

this.Controls.Add(this.ErrorLabel);

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

与此相关,由于相同的原因,如果您定义的其他按钮,复选框等也未显示在表单上,​​我不会感到惊讶。

通常,Designer.cs文件会自动处理此问题。


0
投票

这可能是一个很明显的答案,但这完全让我感到头疼。我验证了我的代码是否具有此处提到的this.groupBox2.Controls.Add(this.ErrorLabel);。我不知道为什么我没看到我的标签。我的标签是一个星号*,在设计器窗口中,它的尺寸很小,但我可以在设计器窗口中看到*。原来我只需要增加它在设计器窗口中的面积,因为当我实际渲染该应用程序时它很小。

© www.soinside.com 2019 - 2024. All rights reserved.