none
how to convert a string to variable without making its copy and modifying it RRS feed

  • Question

  • i have about two hundred variable of picturebox in windows forum now i want to convert it to array

    the variabes looks like

    pictureBox1,pictureBox2,pictureBox3,........pictureBox200

    if there is another algo of making of these picureboxes to make an array without making them in design tab then please help

    void convert_to_coins()
            {
                for (int i = 0; i < 180; i++)
                {
                    string x = ("pictureBox" + i);
                    (x)pictureBox = arr[i];
                    
                }
            }


    but it isn't working

    pictureBox1
    pictureBox1

    or like this

    pictureBox[] arr=new pictureBox[200];
    int i=0;
    foreach (Control x in this.Controls)
                {
                    if (x is PictureBox && x.Tag == "wall")
                    {
                   
                        x.Tag = "coin";
                        arr[i]=x;
                        i++;
                    }
                }

    please help me i am bigginer

    Thursday, April 25, 2019 1:35 AM

All replies

  • Hi

    Thank you for posting here.

    Based on your description, you want to convert a string to variable and put them into an array.

    You could try the following code.

    public void convert_to_coins()
            {
                PictureBox[] arr = new PictureBox[200];
                for (int i = 0; i < 200; i++)
                {
                    string x = "pictureBox" + i.ToString();
                    PictureBox pictureBox = new PictureBox();
                    pictureBox.Name = x;
                    arr[i] = pictureBox;
                    if (i == 1)
                    {
                        this.Controls.Add(pictureBox);
                        pictureBox.Image = Image.FromFile(@"D:\pic\6.jpg");
                    }
                }
                MessageBox.Show("success");
    
            }
    
            private void Button1_Click(object sender, EventArgs e)
            {
                convert_to_coins();
            }
    

    Result:

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 25, 2019 6:12 AM
    Moderator
  • Hello,

    Rather than hard code the array size you can get the PictureBox controls on the form using the following extension method which you create a new file name Extensions and place it in the file.

    public static class Extensions
    {
        public static IEnumerable<T> Descendants<T>(this Control control) where T : class
        {
            foreach (Control child in control.Controls)
            {
    
                T childOfT = child as T;
                if (childOfT != null)
                {
                    yield return (T)childOfT;
                }
    
                if (child.HasChildren)
                {
                    foreach (T descendant in Descendants<T>(child))
                    {
                        yield return descendant;
                    }
                }
            }
        }
    }

    To use it for obtaining all of the PictureBox controls

    PictureBox[] pictureBoxesOnForm = this.Descendants<PictureBox>().ToArray();

    To get all PictureBox controls with Tag == wall.

    PictureBox[] walls = pictureBoxesOnForm
        .Where(pb => pb.Tag != null &&  pb.Tag.ToString() == "wall").ToArray();

    To iterate and change the tag (if there are no matches the for is not executed)

    for (int index = 0; index < walls.Length -1; index++)
    {
        walls[index].Tag = "coin";
    }
    In closing I may not have captured your exact requirements but have given you a way to work thru your task easily.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by Venkat786 Thursday, April 25, 2019 12:50 PM
    Thursday, April 25, 2019 10:17 AM
    Moderator