Generate controls by programming C #

1

I want to generate textBox or listBox by pressing a buttom on a selected form. (From the secondary to the Principal). through Codigo, It consists of generating notes and adding them in a TabControl, which contains a TabPage, in this desire to add the controls to be generated.

This is the Principal

This secondary

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace agenda
{
public partial class Nueva_asignatura : Form
{
    public Nueva_asignatura()
    {
        InitializeComponent();
    }

    private void Nueva_asignatura_Load(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        button1.Text = "Guardar cambios";
    }

    private void checkBox6_CheckedChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        FmPrincipal Principal = new FmPrincipal();
        TabControl hoja = new TabControl();
        TabPage pagina = new TabPage();
        TextBox NuevaAsignatura = new TextBox();
        NuevaAsignatura.Location = new Point(400,150);
        pagina.Controls.Add(NuevaAsignatura);

        hoja.Controls.Add(pagina);
        //          NuevaAsignatura.Location = pagina.Controls.Add();
        // this.Controls.Add(NuevaAsignatura);

        NuevaAsignatura.Text = textBox1.Text + Environment.NewLine
                              + textBox3.Text + Environment.NewLine
                              + textBox4.Text + Environment.NewLine
                              + domainUpDown1.Text + " : " + domainUpDown2.Text + Environment.NewLine
                              + domainUpDown3.Text + " : " + domainUpDown4.Text + Environment.NewLine.ToString();
        hoja.Location = new Point(300, 200);
       // Principal.Controls.Add(hoja);
    }
}
}
    
asked by Ricky1904 30.11.2017 в 06:45
source

1 answer

1

What you are doing is not going to work for you. First, from the secondary form you create a new FmPrincipal, it will not make reference to the main form that you had already created.

You can create the "sheet" in the second form and send it to the main one already created or send the data to the principal and form it there.

To create it in the secondary one and pass it to the main one you can do:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace agenda
{
public partial class Nueva_asignatura : Form
{
    public delegate void pasoDatosHandler(TabControl h); //Declaramos un evento para enviarlo al otro formulario e indicamos que enviamos un TabControl
    public event pasoDatosHandler OnPasoDatos;
public Nueva_asignatura()
{
    InitializeComponent();
}

private void Nueva_asignatura_Load(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
    button1.Text = "Guardar cambios";
}

private void checkBox6_CheckedChanged(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
    FmPrincipal Principal = new FmPrincipal();
    TabControl hoja = new TabControl();
    TabPage pagina = new TabPage();
    TextBox NuevaAsignatura = new TextBox();
    NuevaAsignatura.Location = new Point(400,150);
    pagina.Controls.Add(NuevaAsignatura);

    hoja.Controls.Add(pagina);
    //          NuevaAsignatura.Location = pagina.Controls.Add();
    // this.Controls.Add(NuevaAsignatura);

    NuevaAsignatura.Text = textBox1.Text + Environment.NewLine
                          + textBox3.Text + Environment.NewLine
                          + textBox4.Text + Environment.NewLine
                          + domainUpDown1.Text + " : " + domainUpDown2.Text + Environment.NewLine
                          + domainUpDown3.Text + " : " + domainUpDown4.Text + Environment.NewLine.ToString();
    hoja.Location = new Point(300, 200);

    if(OnPasoDatos != null) OnPasoDatos(hoja); //Enviamos la hoja al formulario principal
   // Principal.Controls.Add(hoja);
}
}
}

Now we have to subscribe to this event from the main form where, in addition to subscribing to the event, we will create the callback function to add the sheet when receiving it from the secondary form. This code is from the main form.

//Cuando creamos el formulario secundario nos suscribimos al evento que hemos creado
NuevaAsignatura formulario = new NuevaAsignatura();
formulario.OnPasoDatos += reciboDatos;

//Definimos la función reciboDatos que es donde recogeremos el TabControl enviado desde el formulario secundario y lo añadiremos al principal
public void reciboDatos(TabControl hoja)
{
    //Aquí ya tenemos el TabControl enviado desde el otro formulario.
    //Ya podemos agregar la hoja nueva que hemos creado
}

Another option that you have, to do it without touching your code so much, is to pass the main form as a parameter when creating the NewAsignature form, but I think it is a better response than the one I have written since the use of events It is very important and useful and pass the form by the builder I consider it a bad practice.

    
answered by 30.11.2017 / 07:59
source