Alluziion
Alluziion

Reputation: 181

Modifying Variable Names in C#

I'm not sure if the title is correct because I wasn't sure how to explain it. I've encountered many scenarios where being able to dynamically modify part of a variable name such a suffixing integer could save me a great deal of time and keep my code much cleaner but I'm not sure how to do it. Here's an example of my most recent encounter.

I have 9 PictureBox's in a 3 x 3 grid. Each PictureBox has a name of cell followed by it's number so cell1, cell2, cell3 etc. I want to get the background colour of each of these cells and assign them to a variable whilst converting them to strings... something like this:

        for (int i = 1; i < 10; i++)
        {
            string ci = celli.BackColor.ToString();
        }

Is there a way I can have the i variable insert only it's numeric value to the placeholder rather than appending an i to the variable name? Can I wrap it in some sort of bracket? I've tried Googleing this but I'm finding it difficult to search for using just keywords.

Thanks in advance.

Upvotes: 0

Views: 717

Answers (3)

CSharpie
CSharpie

Reputation: 9467

Your intend here is to dynamically reference those controls.

In order to achieve this, there is two options:

  • You create those controls dynamically
  • You create dynamic references for the controls created by your form-designer

The first point is explained ny Shreyas Kapur's answer. The second could be cone like this,

readonly Dictionary<Point,PictureBox> _dynamicMappedBoxes = 
         new Dictionary<Point,PictureBox>();

// Call this once in the beginning ofr your program
void createDynamicMapping()
{
    foreach(PictureBox box in Controls.OfType<PictureBox>())
    {
        Point coords = getCoordinatesFromName(box);
        _dynamicMappedBoxes.Add(coords, box);
    }
}

Point getCoordinatesFromName(PictrueBox box)
{
    int x = int.Parse(box.Name.SubString(IdontKnow);
    int y = int.Parse(box.Name.SubString(IdontKnow);
    retrun new Point(x,y);
}

//usage
string colorName = dynamicMappedBoxes[new Point(x,y)].BackColor.ToString();

Upvotes: 0

Shreyas Kapur
Shreyas Kapur

Reputation: 679

You would like to generate a list or collection of all your pictureboxes so that you can access them by specifying their index. One way is to generate the PictureBoxes on runtime:

Like this:

List<PictureBox> myPics = new List<PictureBox>();
int picWidth = 100;
int picHeight = 100;
for (x = 0; x <= this.Width; x += picWidth) {
    for (y = 0; y <= this.Height; y += picHeight) {
        PictureBox pic = new PictureBox();
        pic.Image = pic.Image;
        // Your image
        pic.Location = new Point(x, y);
        this.Controls.Add(pic);
        myPics.Add(pic);
    }
}

// Do something with myPics...

The other method is that when you do have all the pictureboxes on your form already, you can iterate through all the controls, check which ones are pictureboxes and then check their Name property to identify their index. Then do something with them accordingly.

foreach (void ctrl_loopVariable in this.Controls) {
    ctrl = ctrl_loopVariable;
    if (ctrl.GetType() == typeof(PictureBox)) {
        if (ctrl.Name == "your picture box name to test") {
            // Do something here with ctrl
        }
    }
}

(The above code is converted from VB to C#, excuse conversion issues)

Upvotes: 0

Fabio Iotti
Fabio Iotti

Reputation: 1540

You are probably using a visual form editor, the best way to do this whould probably be to generate the grid by code (and not visually).

Another solution is to make it a matrix:

PictureBox[,] cell = new PictureBox[,] {
    { cell1, cell2, cell3 },
    { cell4, cell5, cell6 },
    { cell7, cell8, cell9 }
};

string[,] c = new string[3, 3];

for(int y=0; y<3; y++)
    for(int x=0; x<3; x++)
        c[x, y] = cell[x, y].BackColor.ToString();

Good luck with your code.

Upvotes: 1

Related Questions