I can't create a list of pictureboxes, index outside the bounds of the array C#

i can nas
i can nas lyrics
i can by nas meaning
i can only imagine
i can 's
i can song
nas i can download
nas i can album

I need to make some "sort of game" with animations in C#. So I need to create new pictureBoxes at the press of a button, and I tried to make it work with a list but there is something I miss. Index outside the bounds of the array, I need to set the length of the pictureBoxList that's what the error tells me, but it doesn't work.

public partial class Form1 : Form
{
    List<PictureBox> pictureBoxList = new List<PictureBox>(10);
    int ID = 0;

    private void buttonAddEnemy_Click(object sender, EventArgs e)
    {
          ID++;
          pictureBoxList.Add((PictureBox)Controls.Find("pictureBox" + ID, true)[0]);
          pictureBoxList[ID].Location = new System.Drawing.Point(1, 90);
          pictureBoxList[ID].Name = "pictureBoxEnemy";
          pictureBoxList[ID].Size = new System.Drawing.Size(25, 25);
          pictureBoxList[ID].BackgroundImage = Properties.Resources.Enemy;
          pictureBoxList[ID].BackgroundImageLayout = ImageLayout.Zoom;
          pictureBoxList[ID].BringToFront();
    }
}

try this

 List<PictureBox> pictureBoxList = new List<PictureBox>(10);
    int ID = 0;
    int position = 0;
    private void metroButton1_Click(object sender, EventArgs e)
    {
        ID++;
        pictureBoxList.Add((PictureBox)Controls.Find("pictureBox" + ID, true)[0]);
        pictureBoxList[position].Location = new System.Drawing.Point(1, 90);
        pictureBoxList[position].Name = "pictureBoxEnemy";
        pictureBoxList[position].Size = new System.Drawing.Size(25, 25);
        pictureBoxList[position].BackgroundImage = Properties.Resources.Enemy;
        pictureBoxList[position].BackgroundImageLayout = ImageLayout.Zoom;
        pictureBoxList[position].BringToFront();
        position++;



    }

Chronixx - "I Can" (Official Music Video), Chronixx - "I Can" taken from the Chronology album out now http://smarturl.it/ ChronologyWW Duration: 4:06 Posted: Feb 26, 2018 The I CAN Enquiry Service continues to operate on Mondays to Wednesdays between 9.30 - 12.30 and 1.30 - 4.30. Please leave voicemail if you cannot get through and we will endeavour to call you back during opening hours. To have a chat with an I CAN speech and language therapist, call 020 7843 2544

First List<PictureBox> pictureBoxList = new List<PictureBox>(10); that 10 is the max length of the List its not filled with pictureboxes.So when you first click the button your id is 1 when you have only one element so it obviusly gives and excepetion. It should be

pictureBoxList.Add((PictureBox)Controls.Find("pictureBox" + ID+1/* If your picture box is called pictureBOx1 and not pictureBox0 */, true)[0]);
pictureBoxList[ID].Location = new System.Drawing.Point(1, 90);
pictureBoxList[ID].Name = "pictureBoxEnemy";
pictureBoxList[ID].Size = new System.Drawing.Size(25, 25);
pictureBoxList[ID].BackgroundImage = Properties.Resources.Enemy;
pictureBoxList[ID].BackgroundImageLayout = ImageLayout.Zoom;
pictureBoxList[ID].BringToFront();
ID++;

Of course this is assuming the Control.Find return a valid value and not null, but then you would have object reference exception.

I Can (Nas song), The idea that I might have to continue to work while facilitating another subpar semester of virtual learning is almost too much to bear. info@i-can.center (425)644-6328; Fax: (425)644-6295; Contact Us. BELLEVUE SOUTH; 5150 Village Park Dr SE Bellevue, WA 98006; info@i-can.center (425)657-0620; Fax

Add a guard to the Controls collection so that you do not access elements of the array if they do not exist. That means checking that Controls.Find actually contains items before accessing it with "[0]". I give an example below. Hope it helps you.

        List<PictureBox> pictureBoxList = new List<PictureBox>(10);
        int ID = 0;

        private void buttonAddEnemy_Click(object sender, EventArgs e)
        {
            Control[] pictureControls = Controls.Find("pictureBox", true);
            if (pictureControls != null && pictureControls.Length > 0)
            {
                pictureBoxList.Add((PictureBox)pictureControls[0]);
                pictureBoxList[ID].Location = new System.Drawing.Point(1, 90);
                pictureBoxList[ID].Name = "pictureBoxEnemy";
                pictureBoxList[ID].Size = new System.Drawing.Size(25, 25);
                pictureBoxList[ID].BackgroundImage = Properties.Resources.Enemy;
                pictureBoxList[ID].BackgroundImageLayout = ImageLayout.Zoom;
                pictureBoxList[ID].BringToFront();
            }
        }    

Informed Consent Action Network: Home, Because payment errors can happen in different steps of the payment process and be caused by a number of reasons, they're often difficult Published on Feb 14, 2018 These are the Lyrics to I Can by Chronixx. we do not own these lyrics and all rights are reserved to the owner Please enjoy,like,share and subscribe for more videos.

I Can't Keep Doing This. Please Open the Schools., Plan for college & find financial aid with Iowa College Access Network. Find money for college & get financial aid! Widely recognized as the premier line of beginning readers, I Can Read! books are organized into color-coded levels. With hundreds of titles featuring award-winning authors and illustrators, and the most beloved character friends in the history of children’s literature, you can find books at every stage to engage and excite your child.

International Campaign to Abolish Nuclear Weapons: ICAN, 'I can't even enjoy this.' #BlackBirdersWeek organizer shares her struggles as a black scientist. By Katie Langin Jun. 5, 2020 , 4:35 PM. This week, black� "Yes, I Can!" is a super simple animal song that introduces a bunch of fun animals, action verbs, and the phrases, "Can you___?" and "Yes, I can. / No I can't."

I can't make a purchase – Home - Minecraft Help Center, I-CAN! Virginia is a free online program that provides assistance with the preparation of court forms required to file for a protective order.

Comments
  • I do not see any code of creating a PictureBox.
  • After ID++ you are indexing the element at position 1 but there is no element there. The first element added is at index 0
  • do the ID++ after you set the pictureBoxList[ID] properties
  • You cannot assume that Controls.Find() will find anything. Blindly indexing with [0] is going to blow like this. You'll need to get ahead by actually creating the control (new PictureBox()) and setting its Name property.
  • No.
  • My code is working fine because pictureBox1 is control id name so we cant use ID as 1 for added pictureBoxList which dont have element at 1 position.
  • Yup, I modified so it will find it, but now it gives me the object reference error, I set the name of the current pictureBox before finding it pictureBoxList[ID].Name = "pictureBoxEnemy"; I think I didn't do it right .. Object reference not set to an instance of an object
  • @Ricdan then Controls.Find does not return the right value(its null) you probably dont have the right picture box in right container(Controls) maybe even you dont have it at all.Maybe you should loop trough this Controls.OfType<PictureBox>() and see if there are any that have that name.You need System.Linq for that.
  • @Ricdan no you do pictureBoxList[ID].Name = "pictureBoxEnemy"; after you found the picturebox so it is not the error.
  • In this control id is pictureBox1 and when i want to add in list i need ID is 1 but first element is added on position is 0 so why pictureBoxList[ID] we pass same ID variable always get error on first element add. I put answer but got negative but why i dint know
  • @Dharmeshsharma yea that might be true i will edit my answer.But there is not need for second variable
  • every time update all properties at position 0. If add another control then you have to do ID++
  • The code is to show the use of the guard, i.e. the if condition to check that Controls.Find contains items in its collection. This is what is causing the OutOfBoundsException. You can edit to add additional controls as you wish.