简体   繁体   中英

how to remove an object from a list in C#

Hi guys i am working on a small project where i am adding, removing and updating items in a file rather than database. i have a listview and list where i add items in listview in the UI and object in a list.but when i remove the item is removed from a listview but not the object from the list and throwing exception.the code is the following and line of the problem and problem is indicated.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;


   namespace Address_Book_students
  {
    public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }
    List<student> boy = new List<student>();

    private void Form1_Load(object sender, EventArgs e)
    {
        String path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        if (!Directory.Exists(path + "\\Address_project_irfan"))
            Directory.CreateDirectory((path + "\\Address_project_irfan"));
        if (!File.Exists(path + "\\Address_project_irfan\\setting.xml"))
            File.Create((path + "\\Address_project_irfan\\setting.xml"));

    }

    private void button2_Click(object sender, EventArgs e)
    {
        student b = new student();
        b.Name = textBox1.Text;
        b.Address = textBox2.Text;
        b.Email = textBox3.Text;
        b.Birthday = dateTimePicker1.Value;
        b.Additional_info = textBox4.Text;
        listView1.Items.Add(textBox1.Text);
        boy.Add(b); // when i add the items in the listview1 at same time object is 
                    // object is b is added to the lis boy
        textBox1.Text = "";
        textBox2.Text = "";
        textBox3.Text = "";
        dateTimePicker1.Value = DateTime.Now;
        textBox4.Text = "";


    }
    public void Remove()
    {
       listView1.Items.Remove(listView1.SelectedItems[0]);
        boy.RemoveAt(listView1.SelectedItems[0].Index);//But when i remove items from
                                                       //the listview1 the items deleted
                                                       //from the listview but the object
                                                       // is not deleting from the list 
                                                       //boy throwing the exception(Value of 0 is not
                                                       //a valid arguement) what the problems here with list?


    }

    private void button3_Click(object sender, EventArgs e)
    {
        Remove();
    }



}
public class student
{
    public string Name
    {
        get;
        set;

    }
    public string Address
    {
        get;
        set;

    }
    public string Email
    {
        get;
        set;

    }
    public DateTime Birthday
    {
        get;
        set;

    }
    public string Additional_info
    {
        get;
        set;

    }

}
 }

You're using listView1.SelectedItems[0] to see which item is selected - twice, once for removing it from the ListView and another time to remove it from your List.

Unfortunately, after you remove it from your ListView, listView1.SelectedItems[0] no longer exists, so you get an exception.

Easiest way to fix this - remove the item from your List first, then from the ListView.

I think you would be better off getting the object, then removing that from the list then rebinding the listview.

student stu = (student)boy.Where(s => s.Id == (int)listView1.SelectedValue)).Single();
boy.Remove(stu);

Then rebind your listview. This does suggest you have an Id for the boy so its unique. But you can use one of the other fields, but you want it to be unique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM