Tak na szybciora pisałem, sposób na zmianę za pomącą delegata:
Kod Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.ChangeButtonText += this.ChangeButtonText;
f.Show();
}
private void ChangeButtonText()
{
this.button1.Text = "Jakis tam twoj tekst";
}
}
Kod Form2(rozumiem, że nie jest wymagane by form2 dziedziczyła od form1)
public partial class Form2 : Form
{
public delegate void ChangeButtonTextDelegate();
public ChangeButtonTextDelegate ChangeButtonText;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (ChangeButtonText != null)
this.ChangeButtonText();
}
}
Na obu formach połóż bo buttonie i podepnij zdarzenia OnClick
A tu jest sposob za pomaca referencji(ale stosuj ten za pomoca delegatow):
Kod Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.FriendlyForm= this;
f.Show();
}
public string ButtonText
{
get { return this.button1.Text; }
set { this.button1.Text = value; }
}
public void ChangeButtonText(string text)
{
this.button1.Text = text;
}
}
Kod Form2:
public partial class Form2 : Form
{
private Form1 friendlyForm;
public Form2()
{
InitializeComponent();
}
public Form1 FriendlyForm
{
set { this.friendlyForm= value; }
}
private void button1_Click(object sender, EventArgs e)
{
if (this.friendlyForm != null)
{
this.friendlyForm.ButtonText = "Jakis tam twoj tekst";
//lub to samo za pomoca metody(odkomentuj):
//this.friendlyForm.ChangeButtonText("Jakis tam twoj tekst");
}
}
}