سؤال

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