How can I reuse a piece of code to edit multiple pictureboxes
Im trying to write a piece of code to set the image of the 92 pictureboxes I have in my form. I don´t want to write the same piece of code 92 times, so i was wondering if this can be done faster. My code is:
public void drawRoute()
{
if (route1.line_00_R == null)
{
this.tabMap.Controls.Remove(this.line_00_R);
}
else if (route1.line_00_R == "Blue")
{
this.tabMap.Controls.Add(this.line_00_R);
this.line_00_R.Image = global::MijnenvegerController.Properties.Resources.Blue;
}
else if (route1.line_00_R == "Red")
{
this.tabMap.Controls.Add(this.line_00_R);
this.line_00_R.Image = global::MijnenvegerCont开发者_如何学Croller.Properties.Resources.Red;
}
}
I hope something like this is possible:
public void drawRoute()
{
for (//all values of {0})
{
if (route1.{0} == null)
{
this.tabMap.Controls.Remove(this.{0});
}
else
{
{1} = route1.{0}; // string that is the same as the name of the resource
this.tabMap.Controls.Add(this.{0});
this.{0}.Image = global::MijnenvegerController.Properties.Resources.{1};
}
}
}
where {0} and {1} are some kind of placeholders or variables.
Hope someone can help me. I only started with C# this week, so I hpe this is not a stupid question. Thanks in advance for all help!
EDIT
I found something I think I can use, but I dont know how to implement it:
public Control[] Find(
string key,
bool searchAllChildren
)
Control.ControlCollection.Find Method (http://msdn.microsoft.com)
I realise I can do it like that now. But I already made all the different PictureBoxes using the Disigner. I 'solved' in a very dirty way using this code now:
` public void drawRoute() { drawRoad(route1.line_00_R, this.line_00_R); drawRoad(route1.line_00_U, this.line_00_U); drawRoad(route1.line_01_D, this.line_01_D); drawRoad(route1.line_01_R, this.line_01_R);
// etc. etc. 92 times!
drawRoad(route1.line_6, this.line_6);
drawRoad(route1.line_7, this.line_7);
drawRoad(route1.line_8, this.line_8);
drawRoad(route1.line_9, this.line_9);
drawRoad(route1.line_10, this.line_10);
drawRoad(route1.line_11, this.line_11);
drawRoad(route1.line_12, this.line_12);
}
public void drawRoad(string color, PictureBox control)
{
if (color == null)
{
this.tabMap.Controls.Remove(control);
}
else if (color.Equals("Blue"))
{
this.tabMap.Controls.Add(control);
control.Image = global::MijnenvegerController.Properties.Resources.Blue;
}
else if (color.Equals("DarkRed"))
{
this.tabMap.Controls.Add(control);
control.Image = global::MijnenvegerController.Properties.Resources.DarkRed;
}
else if (color.Equals("Indigo"))
{
this.tabMap.Controls.Add(control);
control.Image = global::MijnenvegerController.Properties.Resources.Indigo;
}
else if (color.Equals("GreyBlue"))
{
this.tabMap.Controls.Add(control);
control.Image = global::MijnenvegerController.Properties.Resources.GreyBlue;
}
else if (color.Equals("Gold"))
{
this.tabMap.Controls.Add(control);
control.Image = global::MijnenvegerController.Properties.Resources.Gold;
}
else if (color.Equals("Orange"))
{
this.tabMap.Controls.Add(control);
control.Image = global::MijnenvegerController.Properties.Resources.Orange;
}
}
`
A way to solve this is by adding the pictureboxes to a 2 dimensional array so you can use a nested loop to loop through the controls/map:
private PictureBox[,] _cell = new PictureBox[9,10];
In the form load you could add all the PictureBoxes to the array. So you can access them like this:
for(int row = 0; row < 10; row++)
{
for(int column=0; column < 9; column++)
{
// drawing logic for _cell[column, row]
}
}
Does that make sense? Do you need more help?
精彩评论