Forms are classes like any other, just create properties or make the controls public to access.
The method you used works because all the controls are stored in the collection Controls
form and are accessible in the same way as an array, informing the key (in this case the name) of the control. But in addition to being a messy code, try changing the name of one of the controls, not even the refactoring of the IDE will update the changes and will bring a terrible problem.
There are other ways to do what you need, as I said above, you can create public properties and access from another object. Example:
In Form1:
public partial class Form1 : Form
{
public string PropriedadeForm1 { get; set; }
public string LabelForm1 { get { return labelForm1.Text; } set { labelForm1.Text = value; } }
public Form1()
{
InitializeComponent();
}
private void buttonAbrirForm2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show(this); //Passa a instância do Form1 (this) como Owner da instância do Form2
}
private void buttonSetPropriedadeForm2_Click(object sender, EventArgs e)
{
Form2 form2 = this.OwnedForms.OfType<Form2>().FirstOrDefault(); //Seleciona a instância do Form2 que é 'Owned' por esta instância do Form1
if (form2 != null) //Se há um Form2
{
form2.PropriedadeForm2 = textBoxForm1.Text; //Define a propriedade
}
}
}
Notice the public properties PropriedadeForm1
and LabelForm1
, they will be accessible from outside the Form.
Note also that, when instilling Form2, it was passed this
as a method parameter Show
. This causes the instance of Form2 to be owned by this instance of Form1. Subsequently, we can access them by the properties OwnedForms
of Form1, and Owner
form2.
Now an example of Form2:
public partial class Form2 : Form
{
public string PropriedadeForm2 { get; set; }
public Form2()
{
InitializeComponent();
}
private void buttonMostraLabelForm1_Click(object sender, EventArgs e)
{
Form1 form = this.Owner as Form1;
if (form != null)
MessageBox.Show(form.LabelForm1);
}
private void buttonSetLabelForm1_Click(object sender, EventArgs e)
{
Form1 form = this.Owner as Form1; //Pega a instância do Form1 que é dona desta instância do Form2
if (form != null)
form.LabelForm1 = textBoxForm2.Text;
}
private void buttonPropriedadeForm2_Click(object sender, EventArgs e)
{
MessageBox.Show(this.PropriedadeForm2);
}
}
Upshot:
There is also the possibility to make the fields public, and access them from outside the Form:
Thus, to access the labelForm1 is enough:
private void buttonMostraLabelForm1_Click(object sender, EventArgs e)
{
Form1 form = this.Owner as Form1;
if (form != null)
MessageBox.Show(form.labelForm1.Text);
}
If you use this form in your code instead of:
TabControl tabControl1 = (TabControl)form1.Controls["tbControl1"];
would be:
form1.tbControl1;
or
form1.LbTeste.Text = "Teste Label";
Lbteste is where? in Tabcontrol? if you are first searching, then you search for the Label!
– novic
I don’t even know if you don’t need to tell me which tab is Tabcontrol... But that’s the way
– Diego Rafael Souza
Yes, he’s in tabControl.
– Rafa
then on Form1 search
form1.Controls["tbControl"]
and check if the return is tbControl ai inside tbControl searches for the components and so on!– novic
I don’t know if I get it, is it something like that? Form Form1 = Application.Openforms["Formcadastro"]; Tabcontrol tabControl1 = (Tabcontrol)Form1.Controls["tbControl1"]; Label Lbteste = (Label)Form1.Controls["Lbteste"]; Lbteste.Text = "SIM";
– Rafa
This will all depend on whether this is the sequence of components ... maybe it is but, that’s right there .... you need to identify which elements you need to look for until you reach the innermost.
– novic
Virgilio Novic, I followed the hint and it worked, I had to get Tabcontrol, then Tabpage and then yes Label. Thank you so much for the help. Hug
– Rafa