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
ListViewItemalways 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
EmployeeDAOand right after that theread, of course it will be null or empty.– Rovann Linhalis
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?
– Aline
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?
– Gabriel Gomes
@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 aMainand appears theRegisterafter I user fill in the data and register, the formRegisterwill pass the data toMainand will close!– Gabriel Gomes
take a look: https://answall.com/questions/13797/passar-dados-entre-forms/198273#198273
– Rovann Linhalis
How do you open the Main when you close the Register?
– Aline
Yeah, that’s when you close the
Registerand when to start the system– Gabriel Gomes