0

I am trying to populate a ListView when clicking on a button from a different form from the form the ListView is on. I created a Method in Form1 to use in Form2 and pass the parameters to the method in Form1 then populate the ListView. When I debug I am getting all the correct values passed over, but the ListView is still not populating. No matter if I set the Modifier property to Public for the ListView or not.Here is my code which I dumbed done for simplicity.

Form1 (Only has a ListView and a button to open Form2)

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void setLvi(string name, string age, string dob)
        {
            ListViewItem lvi = new ListViewItem(name);
            lvi.SubItems.Add(age);
            lvi.SubItems.Add(dob);
            listView1.Items.Add(lvi);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 fm3 = new Form2();
            fm3.Show();
        }
    }
}

Form 2 (It only has a button that should populate the ListView)

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 fm2 = new Form1();
            string name2 = "Chris";
            string age2 = "23";
            string dob2 = "12/19/1989";
            fm2.setLvi(name2, age2, dob2);
        }
    }
}
Croeber
  • 451
  • 3
  • 10
  • 22

1 Answers1

2

The short answer is that the new Form1 you are creating is not being shown, thus the changes you are making to the list view are not reflected in any visual representation of the form. So you could keep the code you have, but call .Show() on the fm2 object you create in the button handler, but I suggest you rethink your approach to this, because there could be things you do not want to throw away from Form1, such as other controls that might get populated if or when they exist.

Instead consider this:

Form2 has a different instance of Form1 than you think it does, because you are newing up another Form1 instance.

private void button1_Click(object sender, EventArgs e)
{
    // fm2 is not the same Form1 that created this Form2 object
    Form1 fm2 = new Form1();
    string name2 = "Chris";
    string age2 = "23";
    string dob2 = "12/19/1989";
    fm2.setLvi(name2, age2, dob2);
}

Instead do this:

public partial class Form2 : Form
{
    public Form1 TheForm1 { get; set; }

    public Form2(Form1 _form1)
    {
        TheForm1 = _form1;
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string name2 = "Chris";
        string age2 = "23";
        string dob2 = "12/19/1989";
        TheForm1.setLvi(name2, age2, dob2);
    }
}

We have changed the constructor of Form2 to accept an instance of Form1, which we store in the property TheForm1, which will allow us to call the setLvi method in the Form1 class.

Now we have to change how we instantiate Form2, like this:

private void button1_Click(object sender, EventArgs e)
{
    Form2 fm3 = new Form2(this);
    fm3.Show();
}

Note: this represents an instance of the current class, which will be Form1, since we are in that class when we are newing up a Form2 object.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80