无法更改弹出窗口上的标签文本

时间:2014-04-23 14:22:16

标签: c# winforms label

我不明白为什么它不起作用。 我想从弹出窗口中设置标签的文本。

class BaseForm : Form
{}

public class BasePopup
{
    private Form popupForm;
     private Panel controlPanel;
    private Label controlLabel; 

    public BasePopup()
    {
        popupForm = new BaseForm
        {
           ...
        };

        controlPanel = new Panel { Dock = DockStyle.Fill, BorderStyle = BorderStyle.Fixed3D };
        popupForm.Controls.Add(controlPanel);

        controlLabel = new Label { Location = new Point(30, 30), Text = "AAA" };
        controlPanel.Controls.Add(controlLabel);
    }
 }

 public string ControlLabelText
    {
        get { return controlLabel.Text; }
        set { controlLabel.Text = value; }
    }

public class ComboBoxPopup : BasePopup
{
}

用法:

 var txp = new ComboBoxPopup();
        txp.ControlLabelText = "Please select the language";
        new ComboBoxPopup().ShowDialog(this);

文字保存在这里 - controlLabel.Text = value;  但标签的文字没有变化。我试过Application.DoEvents()但没有运气。

2 个答案:

答案 0 :(得分:1)

而不是

new ComboBoxPopup().ShowDialog(this);

你必须陈述

txp.ShowDialog(this);

否则,您将创建并显示弹出窗口的新实例,并且不会显示对第一个实例的标签更改。

答案 1 :(得分:1)

嗯,您的问题的答案是controlLabel并未放在BasePopup上。但是,您的问题的解决方案有点不同。 出现你真正想要的是:

public class BasePopup : BaseForm
{
    private Panel controlPanel;
    private Label controlLabel; 

    public BasePopup()
    {
        controlPanel = new Panel { Dock = DockStyle.Fill, BorderStyle = BorderStyle.Fixed3D };
        this.Controls.Add(controlPanel);

        controlLabel = new Label { Location = new Point(30, 30), Text = "AAA" };
        controlPanel.Controls.Add(controlLabel);
    }
 }