Jorge Zapata
Jorge Zapata

Reputation: 2336

Add listview items from other form (using objects)

I want to get some data to fill a listview control, but this data it's determined in other form. This is what I code in form1 (Nuevo_Credito):

private void combo_cliente_SelectionChangeCommitted(object sender, EventArgs e)
    {
        Credito_Grupo ventana = new Credito_Grupo(combo_cliente.SelectedItem);
        ventana.ShowDialog(); 
    }
 public void AgregaIntegrantes(string id, string nombre, string monto)
        {
            ListViewItem elem = new ListViewItem(id);
            elem.SubItems.Add(nombre);
            elem.SubItems.Add(monto);
            listView_integrantes.Items.Add(elem);
        }

I'm invoking form2 (Credito_grupo) as show dialog window, then I want to retrieve some values and pass them to Form1 using the public method "AgregaIntegrantes". So in form2 I did the following:

public Credito_Grupo(dynamic item)
    {
        this.id = item.IDCliente;
        this.nombre = item.NomComp;

        InitializeComponent();
    }

    private void Credito_Grupo_Load(object sender, EventArgs e)
    {
        text_nombre.Text = this.nombre;
    }

    private void button_AgregaCliente_Click(object sender, EventArgs e)
    {
        Nuevo_Credito obj = new Nuevo_Credito();
        obj.AgregaIntegrantes(id.ToString(), nombre, text_monto.Text);

        this.Close();
    }

When the event button_AgregaCliente_click is triggered I need to add the data to listview in form1 using the method described above, but none data is added. I found a solution using delegates here 3077677, is there an approach using objects?

Upvotes: 0

Views: 644

Answers (1)

Al Kepp
Al Kepp

Reputation: 5980

You have an error in button_AgregaCliente_Click method (the last one in the listing). You create a new Nuevo_Credito form there, and pass the data to listview. It looks OK. But this newly created Nuevo_Credito form does exist only in the local variable, so then you throw it away without displaying it when button_AgregaCliente_Click finishes.

I think you need to delete this line: Nuevo_Credito obj = new Nuevo_Credito(); You need to get your real Nuevo_Credito form, not create a new one here.

You can send this from your Nuevo_Credito to the constructor of the Credito_Grupo form. Then you can use it to call back to the original Nuevo_Credito. This approach is based only on objects, and not delegates. As you wanted. :-)

Upvotes: 1

Related Questions