here we have the parent form
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class ParentForm : Form
{
private Button button1;
private ChildForm childForm = new ChildForm();
public ParentForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.button1);
this.Name = "ParentForm";
this.Load += new System.EventHandler(this.ParentForm_Load);
this.ResumeLayout(false);
}
private void ParentForm_Load(object sender, EventArgs e)
{
childForm.Show();
}
private void button1_Click(object sender, EventArgs e)
{
if (childForm.lblText() == "true")
{
childForm.lblText("false");
}
else
{
childForm.lblText("true");
}
}
}
}
and here we have the child form. Click the button in the parent form and watch the label in the child form
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class ChildForm : Form
{
private Label label1;
public ChildForm()
{
InitializeComponent();
label1.Text = "false";
}
internal string lblText()
{
return label1.Text;
}
internal string lblText(string text)
{
return label1.Text = text;
}
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.label1);
this.Name = "ChildForm";
this.ResumeLayout(false);
this.PerformLayout();
}
}
}
Now you can also change
internal string lblText()
{
return label1.Text;
}
internal string lblText(string text)
{
return label1.Text = text;
}
to this
internal string lblText
{
get { return label1.Text; }
set { label1.Text = value; }
}
If you change to the second method you also have to change how you call the change text method to
if (childForm.lblText == "true")
{
childForm.lblText = "false";
}
else
{
childForm.lblText = "true";
}