Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

For my program I am creating a register system to mark attendance for students. I start by taking the students in list student and creating a panel for each with an accompanying name textbox.

the next step is to add 3 radio buttons to each panel however it doesn’t seem to work as intended and I don’t know why. Can you not use a list of RadioButtons in the way I have.

To cover any confusion the studentlist is a test at the moment with them being names 1 – 10 and with regards to the buttons one would resemble present, absent and the other late.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]);
        }

    }

}

enter image description here

As can be seen before the radio buttons are attempted to be added it works as intended
enter image description here

However I don’t know why this error occurs as the integers provided shouldn’t be out of the range

>Solution :

You need to add the RadioButtons to the list, you just initialize it but don’t add it. Note that a list is different to an array. If you initialize it with a capacity it’s still empty and you can’t access items at a given index. You are using the default constructor anyway that just creates an empty list with the default capacity(4). So you need to add them first.

So instead of:

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]);

use this:

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading