C# - Passing data between Windows Form using Listview

Asked

Viewed 324 times

0

I have a project in C# Windows Form, in my project I have 2 screens, 1 - Listview to show the information, 2 Form to get the user data and pass to Form1 Listview, but the problem is: pass the information from form2 to Form1

Minha Logica:

  • Get user information from form2 and pass it to another class
  • In Form1 I read the information of this other class and pass to Listview

Problem:

  • Obejeto of the type ListViewItem always returns null

Form2: To obtain information

    namespace Company
{
    public partial class Register : Form
    {
        EmployeeDAO employeeDAO = new EmployeeDAO();

        public Register()
        {
            InitializeComponent();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            Employee employee = new Employee();
            employee.idEmployee = Convert.ToInt16(this.txtId.Text);
            employee.nameEmployee = this.txtName.Text;
            employeeDAO.insert(employee);

        }

    }
}

My class to get the information from form2 and pass to Form1: (Placing in Listviewitem)

namespace Company
{
    class EmployeeDAO
    {
        ListViewItem item = new ListViewItem();

        public void insert(Employee employee)
        {
            string id;
            string name;
            id = Convert.ToString(employee.idEmployee);
            name = employee.nameEmployee;
            String[] row = { id, name };
            item = new ListViewItem(row);
        }

        public ListViewItem read()
        {
            //This item are returning null
            return item;
        }
    }
}

Form1 to display the data in Listview: (I get a Listviewitem object and add in Listview)

namespace Company
{
    public partial class Main : Form
    {

        public Main()
        {
            InitializeComponent();
            lstEmployee.View = View.Details;
            lstEmployee.FullRowSelect = true;
            lstEmployee.Columns.Add("ID", 150);
            lstEmployee.Columns.Add("Nome", 150);
            insert();
        }

        private void insert()
        {
            EmployeeDAO employeeDAO = new EmployeeDAO();
            ListViewItem item = employeeDAO.read();
            if (item == null)
            {
                //Always this block run
                MessageBox.Show("No Item");
                return;
            }
            else
            {
                MessageBox.Show("Item");
                lstEmployee.Items.Add(item);
            }            
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            Register register = new Register();
            register.Show();
            this.Hide();
        }
    }
}

Can someone help me with this?

  • It’s pretty strange this code there, but your problem is that you create an instance of the object EmployeeDAO and right after that the read, of course it will be null or empty.

  • Does one form open the other, or is one the child of the other? I understood that you have a form with a search, where it informs the user id and you want to add that user in the listview of the other form. That?

  • So I’m learning, but if I take the instance and put it on top of Main, it will create a rest just the same will not?

  • @Aline one opens the other, for example when it starts, it opens the main form which is the Main, in it are listed all registered employees, the first time it will be null because it has not had any registration, in this same screen, has a put register! where the user clicks and adds a Main and appears the Register after I user fill in the data and register, the form Register will pass the data to Main and will close!

  • take a look: https://answall.com/questions/13797/passar-dados-entre-forms/198273#198273

  • How do you open the Main when you close the Register?

  • Yeah, that’s when you close the Register and when to start the system

Show 2 more comments

1 answer

0

You can add a non-mandatory parameter in the constructor of your Main class:

public partial class Main : Form
{
    public Main(Dictionary<int, string> listEmployee = new Dictionary<int, string>)
    {
                    InitializeComponent();
                    lstEmployee.View = View.Details;
                    lstEmployee.FullRowSelect = true;
                    lstEmployee.Columns.Add("ID", 150);
                    lstEmployee.Columns.Add("Nome", 150);
                    insert();
    }

    private void insert()
    {          
                    foreach(var item in listEmployee)
                    lstEmployee.Items.Add(new ListViewItem(string[] {item.Key.toString(), item.Value}));    
    }

    private void btnRegister_Click(object sender, EventArgs e)
    {
                    Register register = new Register();
                    register.Show();
                    this.Hide();
    }
}

And its Register:

public partial class Register : Form
{
        Dictionary<int, string> listEmployee = new Dictionary<int, string>

        public Register()
        {
                InitializeComponent();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
                listEmployee.Add(Convert.toInt32(this.txtId.Text), this.txtName.Text);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
                var main = new Main(listEmployee);
        }

 }

It does not seem to me to make sense that you have those intermediate classes (Employeedao). Besides, in the main you instance it again, which means that it is a new object, without any attribution that is not made directly in its constructor or directly in its attributes.

Browser other questions tagged

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