user3772063
user3772063

Reputation: 39

C# Add Item to List View from another form

I have been searching and trying a few thing but no luck

I have 2 forms, Form 1 has a listview and Form2 searches for an item, if the item in Form2 is found I want to add it to the listview in form1

Note Form2 Launches on a button click from Form1

Form1 (name is MainScreen):

// Add item from  the add item dialog
    public void AddFromItemDialog(ListViewItem itms)
    {
        listItems.Items.Add(itms);
    }

Form2 ():

private void btnAdd_Click(object sender, EventArgs e)
    {
            MainScreen mainScreen = new MainScreen();
            MessageBox.Show("Item Added!");

            ListViewItem lvi = new ListViewItem();
            lvi.Text = "Item 1";
            lvi.SubItems.Add("Second Item");

            mainScreen.AddFromItemDialog(lvi);
    }

There are no errors but nothing gets added to the listview in form 1

Upvotes: 0

Views: 3646

Answers (3)

user3772063
user3772063

Reputation: 39

I found the solution, what I wanted to do was change the controller FROM another form so I just had to pass the first form as a parameter to the second form so this is what I now have and works.

Form1:

 private void button1_Click(object sender, EventArgs e)
        {
            AddItemDialog addItemDialog = new AddItemDialog(this);
            addItemDialog.Show();

        }

// Add item form  the add item dialog
    public void AddFromItemDialog(ListViewItem itms)
    {
        listItems.Items.Add(itms);
    }

Form2:

private MainScreen mainScreen;

 //overloaded constructor with handle to Form1
    public AddItemDialog(MainScreen frm1)
    {
        InitializeComponent();

        mainScreen = frm1;
    }

private void btnAdd_Click(object sender, EventArgs e)
{
        MessageBox.Show("Item Added!");

        ListViewItem lvi = new ListViewItem();
        lvi.Text = "Item 1";
        lvi.SubItems.Add("Second Item");

        mainScreen.AddFromItemDialog(lvi);
}

Upvotes: 0

Mark Hall
Mark Hall

Reputation: 54552

You are creating a new MainForm in Form2, it is not the instance of the form that created Form2 and is not being displayed therefore any changes are not visible. When you use Show or ShowDialog you have the option to set the Owning Form. i.e.

   frm2.Show(this);

You can then access it from your AddItemDialog Form.

private void btnAdd_Click(object sender, EventArgs e)
{
    MessageBox.Show("Item Added!");
    ListViewItem lvi = new ListViewItem();
    lvi.Text = "Item 1";
    lvi.SubItems.Add("Second Item");
    ((MainScreen)this.Owner).AddFromItemDialog(lvi);
}

Though the proper way to do this would be to use properties and events to link the two forms, that way the created form is not required to have internal knowledge of the creating form.

Upvotes: 0

Steve Wellens
Steve Wellens

Reputation: 20640

You have a stack variable:

MainScreen mainScreen = new MainScreen();

When the function exits, it ceases to exist. Try making it a class variable. Something like:

private MainScreen mainScreen;

private void btnAdd_Click(object sender, EventArgs e)
{
    if (mainScreen == null)
    {
         mainScreen = new MainScreen();

You also have to show the form, not just create it.

Upvotes: 1

Related Questions