Index was outside the bounds of the array

Apr 19 2009 9:13 PM
Im having an issue. I have an application that has a listbox, a textbox and 2 buttons. one button inserts the name into the listbox. the second converts the list box into an array. the problem i have is that i get an error stating that the {"Index was outside the bounds of the array."} my code is below, can anyone help ? HELP! that would be correct word. code is below.

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string[] storage = new string[2];



        public Form1()
        {
            InitializeComponent();
        }

        private void load_array(ListBox list)
        {
            string[] storage = this.storage;
            for (int i = 1; i <= storage.Length; i++)
            {
                list.SetSelected(i,true);

                storage[i] = list.SelectedItems[i].ToString();
            }

            Array.Sort<string>(storage);

            this.storage = storage;
        }

        private void add_Click(object sender, EventArgs e)
        {
            name_list.Items.Add(name_box.Text);

            name_box.Text = "";
        }

        private void sort_Click(object sender, EventArgs e)
        {
            load_array(name_list);
            name_list.Items.Clear();
            name_list.Items.AddRange(this.storage);
        }
    }
}


Answers (4)