You can do via Events / delegates [which I find more robust], return values with static variable in third class, pass the form via argument to change properties and, there is also a possibility that no one cites - I think it is the most complicated - you CAN access the information directly through the list of open forms [Application.Openforms], find it and change the property.
The negative point is, if you change the control name or form, you will have to change it also in the code, making maintenance more complicated.
I did an example project to make the understanding simpler:
- frmMain - Parent form
- frmSearch - form search
- In frmMain, I create a Textbox, named txtEx
- I call the form Search frmSearch . Open the frmSearch , call the button click and in it I try to change the parent control:
Example:
// Inicio
private void button1_Click(object sender, EventArgs e)
{
TextBox myText = (TextBox)Application.OpenForms["frmMain"].Controls["txtEx"];
myText.Text = "10";
this.Dispose();
}
Ready, when you return to the frmMain form, the value "10" is written in the Textbox.
Be careful, if you miss the form or control name, you will come across the release of a System.NullReferenceException. Treat this code.
Use:
// Acesse: Application.OpenForms["NomeDoFormulário"] e Controls["NomeDoControle"]
// Declara um controle TextBox, que servirá de ponteiro para o seu controle no formulário pai
// Faz a Conversão explícita de tipo de Controle (para poder alterar as propriedades) [(TextBox) ObjetoXYZ]
TextBox myText = (TextBox) Application.OpenForms["frmMain"].Controls["txtEx"];
// Altera de fato a propriedade
myText.Text = "XYZ";
// Encerra seu formulário Filho
this.Dispose();
Simplifying:
(Application.OpenForms["frmMain"].Controls["txtEx"] as TextBox).Text = "Meu valor";
when I type the
this
innew FormFilha(this)
, I get an error: "Keyword 'this' is not available in the Current context.".– ptkato