Why doesn’t it work on my user control?

Asked

Viewed 90 times

0

I have a code that would like to put it in the user control of my windows Forms, but it does not make changes to Form1, what is the reason?

Code:

private void button5_Click(object sender, EventArgs e)
{
    Form1 f1 = new Form1();
    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;
}

If I put this code on a button of Form1 itself, it works normally.

  • solved your problem ? puts more information to try to help

  • Not yet, I’ve modified the post.

  • you are with both openers at the same time ?

  • and by clicking this button, it should add this tab ?

  • Yes, actually the user control is inside the Form1.

  • 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)

  • There is no form2. The program opens Form1 and user control is instantiated within it.

  • You can post this control?

  • Not much to show, he has some buttons and only, in case, the button5 (of which I’m having trouble) is in the post.

  • 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

  • Was a mistake: System.ArgumentException: 'Não é possível adicionar o controle de nível superior a um controle.'

  • this control you made, only serves for the Form1 ?

  • Yes. It is only used in Form1.

  • 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

  • But I need each tabpage to be something different, more or less how a browser tabs work.

  • I managed to put a button on the Form1 that overlaps this button, anyway, thanks for the help.

Show 11 more comments

1 answer

3


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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.