Whereas, you open the Form1
, within it you have a command that opens the Form2
, and in the Form2
, a button to add to tabPage
in the Form1
, the code should look like this:
Form1.Cs:
//Evento que abre o Form2
private void buttonAbrirForm2_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Parent = this;
form.Show();
}
Form2.Cs:
//Evento que adiciona a tab no Form1
private void buttonAddTabForm1_Click(object sender, EventArgs e)
{
Form1 f1 = ((Form1)this.Parent);
var tab = new TabPage();
var aba = new Abas();
tab.Text = "Guia " + ((int)f1.tabControl1.TabCount + 1).ToString();
f1.tabControl1.TabPages.Add(tab);
tab.Controls.Add(aba);
f1.tabControl1.SelectTab(tab);
aba.Dock = DockStyle.Fill;
}
There is still how not to pass the Form1
as Parent
of Form2
, and from anywhere in the application you access the Form1
:
private void buttonAddTabForm1_Click(object sender, EventArgs e)
{
foreach (Form f in Application.OpenForms)
{
if (f is Form1)
{
Form1 f1 = ((Form1)f);
var tab = new TabPage();
var aba = new Abas();
tab.Text = "Guia " + ((int)f1.tabControl1.TabCount +1).ToString();
f1.tabControl1.TabPages.Add(tab);
tab.Controls.Add(aba);
f1.tabControl1.SelectTab(tab);
aba.Dock = DockStyle.Fill;
break;
}
}
}
As I mentioned in the comments, I believe that this is not the situation for the creation of a custom userControl. But anyway I made one that alters the Form1, and here it worked perfectly:
Example:
//Evento do botão que está dentro do userControl1
private void button1_Click(object sender, EventArgs e)
{
if (this.Parent is Form1)
{
((Form1)this.Parent).Text = "Texto foi alterado pelo controle";
}
}
In the case of your Control:
private void button5_Click(object sender, EventArgs e)
{
if (this.Parent is Form1)
{
Form1 f1 = ((Form1)this.Parent);
var tab = new TabPage();
var aba = new Abas();
tab.Text = "Guia " + ((int)f1.tabControl1.TabCount + 1).ToString();
f1.tabControl1.TabPages.Add(tab);
tab.Controls.Add(aba);
f1.tabControl1.SelectTab(tab);
aba.Dock = DockStyle.Fill;
}
}
Note: For you to access the out-of-form controls, the property modifiers
must be defined as public
solved your problem ? puts more information to try to help
– Rovann Linhalis
Not yet, I’ve modified the post.
– Francisco
you are with both openers at the same time ?
– Rovann Linhalis
and by clicking this button, it should add this tab ?
– Rovann Linhalis
Yes, actually the user control is inside the Form1.
– Francisco
so, you seem to be instantiating Form1 again, this one has already been instantiated and is open on the screen, post the code when you have open Form1, or Form2 (I do not know which opens first)
– Rovann Linhalis
There is no form2. The program opens Form1 and user control is instantiated within it.
– Francisco
You can post this control?
– Rovann Linhalis
Not much to show, he has some buttons and only, in case, the button5 (of which I’m having trouble) is in the post.
– Francisco
is because it doesn’t make much sense for you to create a controller, but its function is to change Form1... so I wanted to really understand what you did. but anyway your problem is being the instance, in button5 event, use the Parent property as shown in the example that should work
– Rovann Linhalis
Was a mistake:
System.ArgumentException: 'Não é possível adicionar o controle de nível superior a um controle.'
– Francisco
this control you made, only serves for the Form1 ?
– Rovann Linhalis
Yes. It is only used in Form1.
– Francisco
in this case, advise against creating a control of this type, will not be useful anywhere else in the application, it would be much simpler to place the components directly on the screen
– Rovann Linhalis
But I need each tabpage to be something different, more or less how a browser tabs work.
– Francisco
I managed to put a button on the Form1 that overlaps this button, anyway, thanks for the help.
– Francisco