1

I want to get a shorter code than now but I don't know how.

What I do now is like the code below.

arrPictureBox[0] = picChair0;
arrPictureBox[1] = picChair1;
arrPictureBox[2] = picChair2;
arrPictureBox[3] = picChair3;
arrPictureBox[4] = picChair4;
arrPictureBox[5] = picChair5;
arrPictureBox[6] = picChair6;
arrPictureBox[7] = picChair7;
arrPictureBox[8] = picChair8;
arrPictureBox[9] = picChair9;
arrPictureBox[10] = picChair10;
arrPictureBox[11] = picChair11;

(pic) is a picturebox.

But I want less code but I don't know if it possible to do this with a loop (for loop).

for (int i = 0 ; i < arrPictureBox.Length; i++)
{
    arrPictureBox[i] = picChair + i;
}
5
  • 1
    how are you creating the picChair's ? Commented Dec 15, 2015 at 22:27
  • @LOLslowSTi If "picChairXX" are controls on a form that would be duplicate of one of many questions like stackoverflow.com/questions/2168363/… (more can be found by search like bing.com/search?q=c%23+winform+array+controls)... So probably not controls but what? Commented Dec 15, 2015 at 22:33
  • Reflection via Type.GetFields? If you just want a concise notation, you could use arrPictureBox = new[] {picChar0,picChair1,picChair2}... Commented Dec 15, 2015 at 22:36
  • its funny how people are just assuming things and answering the question. There is clearly not enough information given here to produce a concise answer. With the information given, the only answer is SomeType[] arrPictureBox = { picChair0, picChair1, picChair2, picChair3, picChair4, picChair5, picChair6, picChair7, picChair8, picChair9, picChair10, picChair11}; Commented Dec 15, 2015 at 22:38
  • i just wondering if it was possible to put the code above in a loop , the type of aplication that i made is a windows form , and the picChairx are picturboxes (controls) Commented Dec 15, 2015 at 22:44

4 Answers 4

4

If picChairN is a local variable then there's nothing you can do to simplify it as much as you'd like. The best you can do is

arrPictureBox = new [] { picChair0, picChair1, picChair2, picChair3,
                         picChair4, picChair5, picChair6, picChair7, 
                         picChair8, picChair9, picChair10, picChair11};

If picChairN is a class member (e.g. a field created by the designer) then you could use reflection, but considering you already have the array method typed out I don't see much benefit.

Sign up to request clarification or add additional context in comments.

7 Comments

It looks like picChairN is a form control (PictureBox). You could loop through all the controls to get all the pictureboxes that start with picChair and add it to a List (then ToArray if needed).
If it's windows form application isn't there a possibility to get all typeof(picturebox) from form object or something?
@RonBeyer true, but then you're putting your trust in the designer to name the controls properly and trust in the developer not to rename a control in a way that would break the logic. It's possible but I really don;t see what's wrong with adding 12 items to an array explicitly.
@AlexeiLevenkov like 50% of the questions here is done without research so i'd say yes
@AlexeiLevenkov Maybe a Carnac hat for "reading the mind of the OP"
|
2

Let's predict you're on WinForms and the pictureBoxes already exist, then you can use the following:

for (int i = 0; i < arrPictureBox.Length; i++)
{
    arrPictureBox[i] = this.Controls["picChair" + i];
}

Which actually does this:

  • get the first Control (a PictureBox for example) with the given name
  • add the found control to the array of pictureboxes

EDIT:

It might be useful to check for non existing pictureBoxes:

for (int i = 0 ; i < arrPictureBox.Length; i++)
{
    var pb = this.Controls["picChair" + i] as PictureBox;
    if (pb != null)
    {
        arrPictureBox[i] = pb;
    }
}

2 Comments

Thanks , that was what i mentioned .
@Sandeerius no problem, that's what SO is for :D
0

You can use al List like below.

  List<string> arrPictureBox = new List<string>();
        for (int i = 0; i < 20; i++)
        {
            arrPictureBox.Add("picChair" + i);
        }
        var result = arrPictureBox.ToArray();

Hope it helps.

Comments

-1

If all the picture boxes are on the same form and are the ONLY picture boxes on the form, you can loop through them with something like the following:

int x = 0;
foreach(Control c in this.Controls)
{
   if(c is PictureBox)
   {
     arrPictureBox[x++] = c
   }
}

3 Comments

And just how is arrPictureBox sized? I did not vote you down.
you could make arrPictureBox by new PictureBox[this.Controls.Count(c => c is PictureBox)] but then you could just do this.Controls.Where(c => c is PictureBox).toArray();
My answer was meant as a quick example of how to loop through specific types of controls on a form, not a complete solution, on the assumption that this was being used on a Windows form (as the original poster later clarified) in his comments. Not sure why that's worth a down vote to someone. Just another Steve's solution is a more elegant way to do something similar but maybe a bit harder to understand for someone less experienced.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.