문제

I dont understand why it doesnt work. I want to set label's text outside from popup form.

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

Usage:

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

Text saves here - controlLabel.Text = value; but label's text doesnt change. I tried Application.DoEvents() but no luck.

도움이 되었습니까?

해결책

Instead of

new ComboBoxPopup().ShowDialog(this);

you must state

txp.ShowDialog(this);

Otherwise you create and show a new instance of your popup and the label change to the first instance is not shown.

다른 팁

Well, the answer to your question is that controlLabel isn't placed on BasePopup. However, the solution to your problem is quite a bit different. It appears that what you really want is this:

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);
    }
 }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top