我的 C# 程序中的 List<Radio Buttons> 导致错误?索引总是超出范围?

List<Radio Buttons> in my C# program causes errors? Index always out of range?

对于我的计划,我正在创建一个注册系统来标记学生的出勤率。我首先让学生加入列表学生,并为每个学生创建一个面板,并附上姓名文本框。

下一步是向每个面板添加 3 个单选按钮,但它似乎没有按预期工作,我不知道为什么。你能不能像我一样使用单选按钮列表。

为了避免混淆,目前学生名单是一项测试,他们的名字是 1 - 10,关于按钮,一个类似于 present,absent,另一个 late。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }


    private void Form1_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 10; i++)
        {
            studentlist.Add(i.ToString());
        }
        drawregisterpanels();
    }
    List<string> studentlist = new List<string>();

    public void drawregisterpanels()
    {
        TextBox[] StudentNames = new TextBox[studentlist.Count];

        Panel[] RowOfChoice = new Panel[studentlist.Count];

        int width = 100;
        int height = 25;
        for (int k = 0; k < RowOfChoice.Length; k++)
        {
            StudentNames[k] = new TextBox
            {
                Text = studentlist[k],
                Size = new Size(width, height),
                Location = new Point(0, height * k),
            };
            RowOfChoice[k] = new Panel
            {
                Name = k.ToString(),
                Size = new Size(width, height),
                Location = new Point(StudentNames[k].Width, height * k),
                BackColor = Color.CadetBlue,
                BorderStyle = BorderStyle.Fixed3D,

            };

            RegisterPanel.Controls.Add(StudentNames[k]);
            RegisterPanel.Controls.Add(RowOfChoice[k]);
            addradiobuttons(k, RowOfChoice);

        }

    }

    List<RadioButton> RegisterStatusChoice = new List<RadioButton>();
    void addradiobuttons(int panelno,Panel[] RowOfChoice)
    {
        int Width = RowOfChoice[panelno].Width / 3;
        int Height = RowOfChoice[panelno].Height;

        for (int p = 0; p < 3; p++)
        {
            Console.WriteLine((panelno*3)+p);
            RegisterStatusChoice[(panelno * 3) + p] = new RadioButton
            {
                Size = new Size(Width, Height),
                Location = new Point(Width * p, 0),
                CheckAlign = ContentAlignment.MiddleCenter,
            };
            RowOfChoice[panelno].Controls.Add(RegisterStatusChoice[(panelno * 3) + p]);
        }

    }

}

在尝试添加单选按钮之前可以看到它按预期工作

但是我不知道为什么会出现这个错误,因为提供的整数不应该超出范围

您需要将RadioButtons 添加到列表中,您只是对其进行初始化但不添加它。请注意,列表不同于数组。如果您 initialize it with a capacity 它仍然是空的,您无法访问给定索引处的项目。无论如何,您正在使用默认构造函数,它只是创建一个具有默认容量(4)的空列表。所以你需要先添加它们。

所以代替:

RegisterStatusChoice[(panelno * 3) + p] = new RadioButton
{
    Size = new Size(Width, Height),
    Location = new Point(Width * p, 0),
    CheckAlign = ContentAlignment.MiddleCenter,
};
RowOfChoice[panelno].Controls.Add(RegisterStatusChoice[(panelno * 3) + p]);

使用这个:

RadioButton rb = new RadioButton
{
    Size = new Size(Width, Height),
    Location = new Point(Width * p, 0),
    CheckAlign = ContentAlignment.MiddleCenter,
};
RegisterStatusChoice.Add(rb);
RowOfChoice[panelno].Controls.Add(rb);