1

I’m trying to show and hide a pop-up when certain events occur. The pop-up is appearing and disappearing properly, but all of its labels are blank. I originally was trying to populate the labels prior to showing the form, but I’ve commented-out all of that logic. The labels are all blank, but the space is properly allocated for each label (see screenshot).

my popup control:

public MyPopUp()
{
    InitializeComponent();
}

my separate class:

MyPopUp _MyPopUp;

protected override void OnLoad(IServiceProvider myServiceProvider)
{
    _MyPopUp = new MyPopUp();
}

protected override void WhenSomethingHappens() {
    _MyPopUp.Show();
}

protected override void WhenSomethingElseHappens() {
    _MyPopUp.Hide();
}

Here is part of my designer.cs file. I've only copy/pasted the code from one of the labels, but the other 5 labels on the form have nearly-identical code.

private System.Windows.Forms.Label label1;

private void InitializeComponent()
{
    this.label1 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.AutoSize = true;
    this.label1.Location = new System.Drawing.Point(58, 9);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(41, 13);
    this.label1.TabIndex = 0;
    this.label1.Text = "Some Label Text"; 
    // 
    // MyPopUp
    // 
    this.Controls.Add(this.label1);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
    this.Name = "MyPopUp";
    this.Text = "Some Text";
    this.ResumeLayout(false);
    this.PerformLayout();
}

screenshot

4

4 回答 4

3

您需要调用 Refresh() 函数来强制每个子控件使其客户区无效,即重绘。

对于您的情况:

protected override void WhenSomethingHappens() {
    _MyPopUp.Show();
    _MyPopUp.Refresh();
}
于 2014-07-06T11:52:26.100 回答
1

发生这种情况是因为您正在无模式地运行表单。

如果您愿意,您可以通过模态方式运行它来检查您的字体/背景颜色是否良好等:

System.Windows.Forms.Application.Run(YourFormName);

然后将 DoEvent 行添加到您的无模式调用中:

Form2 F2 = new Form2();
F2.Show();
System.Windows.Forms.Application.DoEvents();

那应该这样做!

于 2014-12-16T16:23:49.060 回答
0

尝试在显示表单之前调用,这样表单将在主线程中创建。

if (this.InvokeRequired)
            this.BeginInvoke((Action)(() => //'this.' is the form
            {
                _MyPopUp.Show();
            }));
于 2013-03-20T14:30:08.643 回答
0

我做了一个测试,发现可以让标签文本消失。ForeColor如果与 相同,则标签文本会消失BackColor。从您的标签添加代码中,我看到您没有指定这些颜色。WinForms 控件从父控件(在本例中为窗体本身)继承了许多属性,如果它们未定义的话。这可能意味着Form 的ForeColorBackColor是相同的,并且这会传播到标签。更改ForeColor表单或标签的颜色以解决此问题。

于 2013-03-20T13:32:56.497 回答