Question

I'm trying to add some items from a class into a listview through a second form and this is what i get

public partial class Form1 : Form
{
    Elev x;
    ArrayList listaStudenti;
    ListViewItem itm;



    public Form1()
    {
        InitializeComponent();
        listaStudenti = new ArrayList();
        x=new Elev();
        x.nume =lvStud.Items[0].ToString();
        x.varsta = int.Parse(lvStud.Items[0].SubItems[3].Text);
        listaStudenti.Add(x);

    }

private void adaugaToolStripMenuItem_Click(object sender, EventArgs e)
{
        Form2 m = new Form2();
        m.ShowDialog();
        if(m.DialogResult==DialogResult.OK)
        {
            x = new Elev();
            x.nume = m.tbNume.Text;
            x.varsta = int.Parse(m.tbVarsta.Text);
            listaStudenti.Add(x);
            itm = new ListViewItem(m.tbNume.Text);
            itm.SubItems.Add(m.tbNume.Text);
            itm.SubItems.Add(m.tbVarsta.Text);
            lvStud.Items.Add(itm);
        }
}

Exception

Was it helpful?

Solution

It appears no items are in your collection when the form is being instantiated, so when you call:

x.nume =lvStud.Items[0].ToString();

...there are no records in .Items, hence the ArgumentOutOfRangeException. Be sure your ListView contains items before this logic is called, or add some logic to trap empty lists.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top