Pass data from one form to another form’s listview

Asked

Viewed 190 times

0

Good afternoon, I am developing a system in c# where I need to find the note number and when clicking add, it passes the information to the main form in listview. Can anyone help me how to develop this? Thank you inserir a descrição da imagem aqui

This is my form to add note number public partial class Frmaddshipment : Form { public int iRetorno = 0;

    public FrmAddRemessa()
    {
        InitializeComponent();
    }

    private void Txt_CodigoRemessa_Validating(object sender, CancelEventArgs e)
    {
        if (txt_CodigoRemessa.Text.Trim().Equals(string.Empty))
            return;

        var oNota = new RemessaNG().BuscarNota(Convert.ToInt32(txt_CodigoRemessa.Text.Trim()));



        txt_CodigoCliente.Text = Convert.ToInt32(oNota.ClienteID).ToString();
        txt_NotaID.Text = Convert.ToInt32(oNota.NotaID).ToString();


        MascaraCampoCodigo.RetornarMascara(txt_CodigoCliente, new EventArgs());
        MascaraCampoCodigo.RetornarMascara(txt_NotaID, new EventArgs());

    }

    private void Txt_CodigoRemessa_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            Txt_CodigoRemessa_Validating(txt_CodigoRemessa, new CancelEventArgs());
        }
    }


    private void Txt_CodigoCliente_TextChanged(object sender, EventArgs e)
    {
        //Verifica se está vazio
        if (txt_CodigoCliente.Text.Trim().Equals(string.Empty))
            return;

        var oCliente = new RemessaNG().BuscarCliente(Convert.ToInt32(txt_CodigoCliente.Text.Trim()));

        txt_NomeCliente.Text = oCliente.Nome;

        MascaraCampoCodigo.RetornarMascara(txt_CodigoCliente, new EventArgs());
        MascaraCampoCodigo.RetornarMascara(txt_CodigoRemessa, new EventArgs());
    }

    private void Bt_Adicionar_Click(object sender, EventArgs e)
    {
        if (txt_NotaID.Text.Trim().Equals(string.Empty))
            return;

            iRetorno = Convert.ToInt32(txt_CodigoRemessa.Text.Trim());

    }

And this is my add button in the form ` private void Bt_addressta_click(Object Sender, Eventargs and) {

        var frmAddRemessa = new FrmAddRemessa();
        frmAddRemessa.ShowDialog();
        var iRetorno = frmAddRemessa.iRetorno;
        //iRetorno = 0
        if (iRetorno < 1)
            return;




    }`

1 answer

2

Rayane, you need access to Form1 within the Form2. You can do this by passing the object of Form1 by reference to the creation of the Form2, for example.

In the Form1, you create the Form2 thus:

Form2 frm = new Form2(this);

Obs: the this is the very object of Form1 being passed by reference.

In the Form2, you store the Form1, passed by reference in the constructor method, in a private variable, for example meuForm1:

private Form1 meuForm1;

public Form2(Form1 frm1) {
    InitializeComponent();
    meuForm1 = frm1;
}

So the button inside the Form2 can call the object meuForm1 and change the ListView of him, for example:

private void button1_Click(object sender, EventArgs e) {
    meuForm1.listView1.Items.Add("");
}

Don’t forget to change the visibility (Modifiers Property) of ListView of Form1 for public, for you to have access to it from other objects!

  • Wilson, it worked perfectly! Thank you so much for your help

  • @Rayanneborges, if you helped solve the problem, mark the answer as a solution to the question! ;)

Browser other questions tagged

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