C# (C"表格)刽子手游戏";argumentoutofrange“;

C# (C"表格)刽子手游戏";argumentoutofrange“;,c#,forms,outofrangeexception,C#,Forms,Outofrangeexception,当我试图显示下划线时,我尝试制作的hangman游戏在for循环中抛出“ArgumentOutOfRangeException”。我试着重写代码,但没有成功 List<Label> underline; int left = 300; int top = 275; for (int i = 0; i < data.solution.Length; i++) //data is th

当我试图显示下划线时,我尝试制作的hangman游戏在for循环中抛出“ArgumentOutOfRangeException”。我试着重写代码,但没有成功

            List<Label> underline;
            int left = 300;
            int top = 275;

            for (int i = 0; i < data.solution.Length; i++) //data is the object that contains the word that is the solution
            {
                underline = new List<Label>();
                underline[i].Parent = this;
                underline[i].Text = "_";
                underline[i].Size = new Size(35, 35);
                underline[i].Location = new Point(left, top);
                left += 30;
                underline[i].Font = new Font(new FontFamily("Microsoft Sans Serif"), 20, FontStyle.Bold);
            }
列表下划线;
int左=300;
int top=275;
for(int i=0;i

我不明白这是怎么回事。当我点击“新建游戏”时,它会立即将其丢弃。

首先,不要为for循环中的每个迭代创建一个新列表,而是在循环之外创建它。其次,您必须在列表中添加一个新的
标签
实例,然后才能通过其索引访问它:

List<Label> underline = new List<Label>();
int left = 300;
int top = 275;

for (int i = 0; i < data.solution.Length; i++) //data is the object that contains the word that is the solution
{
    underline.Add(new Label());
    underline[i].Parent = this;
    underline[i].Text = "_";
    underline[i].Size = new Size(35, 35);
    underline[i].Location = new Point(left, top);
    left += 30;
    underline[i].Font = new Font(new FontFamily("Microsoft Sans Serif"), 20, FontStyle.Bold);
}
List underline=新列表();
int左=300;
int top=275;
for(int i=0;i
您可以在每个
循环迭代中实例化一个新的标签列表:

for (int i = 0; i < data.solution.Length; i++)
{
     underline = new List<Label>();
     ...
}

A真的很有帮助。可能是重复的谢谢你。现在我觉得自己愚蠢得难以置信。我很高兴我能帮上忙:)谢谢你的帮助!
var underline = new List<Label>(data.solution.Length);
for (int i = 0; i < data.solution.Length; i++)
{
     var lbl = new Label();
     lbl.Parent = ...
     ...
     underline.Add(lbl);
}
var underline  = data.solution.Select(x = 
                      {
                          left += 30;
                          return new Label 
                          { 
                             Parent = this,
                             Text = "_", 
                             ...
                          }
                      }).ToList();