我有这个错误:索引超出范围。必须是非负数且小于集合的大小。
我花了很长时间试图解决它,但我无法解决。请我需要帮助
public partial class Form1 : Form
{
string[] generate = new string[] {"Name1","Name2","Name3","Name4","Name5","Name6","Name7","Name8","Name9","Name10","Name11","Name12","Name13","Name14","Name15","Name16","Name17","Name18","Name19","Name20" };
string[] n = new string[] { "Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7", "Name8", "Name9", "Name10", "Name11", "Name12", "Name13", "Name14", "Name15", "Name16", "Name17", "Name18", "Name19", "Name20" };
String[] a = new String[20];
string name;
int Num;
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e) // START btn
{
Num = int.Parse(textBox1.Text);
if (Num < 20)
{
if (generate.Length >= Num)
{
for (int i = Num; i > 0; i--)
{
name = generate.ElementAt(i);
listBox1.Items.Add(name); // Print it to list1
generate = generate.Where(s => s != name).ToArray(); // Delete name after using
}
}
else MessageBox.Show("Sorry, remaining names =" + generate.Length);
listBox2.Items.Clear();
listBox2.Items.AddRange(generate);
}
else MessageBox.Show("Max name is 20!");
}