Talking to one form from another form

Talking to one form from another form

Post by Mar » Tue, 18 Feb 2003 07:55:45



Hiya,

I can't seem to figure this out.  Let's say I load form and it contains one
label.  I then load form two, and I want to change the text of the label on
form one from form two.  How would I go about doing this?  Help is

Thanks in advance,
Mar

 
 
 

Talking to one form from another form

Post by Goran Gente » Tue, 18 Feb 2003 18:33:17


class Form1 : System.Windows.Forms.Form
{
    private System.Windows.Forms.Label lbl;

    // Property
    public string LabelText
    {
        get
        {
            return lbl.Text;
        }
        set
        {
            lbl.Text = value;
        }
     }

    public Form1()
    {

    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
        Form2 f2 = new Form2(this);
        f2.ShowDialog();
    }

Quote:}

class Form2 : System.Windows.Forms.Form
{
    private Form1 owner;

    public Form2(Form1 owner)
    {
        this.owner = owner;
    }

     private void Form2_Load(object sender, System.EventArgs e)
    {
        owner.LabelText = "new label text";
    }

Quote:}

Do you get the point?


> Hiya,

> I can't seem to figure this out.  Let's say I load form and it contains
one
> label.  I then load form two, and I want to change the text of the label
on
> form one from form two.  How would I go about doing this?  Help is

> Thanks in advance,
> Mar