C# 如何重用一段代码来编辑多个PictureBox

C# 如何重用一段代码来编辑多个PictureBox,c#,edit,picturebox,reusability,C#,Edit,Picturebox,Reusability,我正试图写一段代码来设置我表格中92个图片框的图像。我不想把同一段代码写92次,所以我想知道这是否可以做得更快。我的代码是: 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.ta

我正试图写一段代码来设置我表格中92个图片框的图像。我不想把同一段代码写92次,所以我想知道这是否可以做得更快。我的代码是:

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::MijnenvegerController.Properties.Resources.Red;
    }
}
我希望这样的事情是可能的:

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};
        }
    }
} 
其中{0}和{1}是某种占位符或变量

希望有人能帮助我。我本周才开始学C#,所以我认为这不是一个愚蠢的问题。提前感谢您的帮助

编辑

我发现了一些我认为可以使用的东西,但我不知道如何实现它:

公共控制[]查找(
字符串键,
布尔儿童
)

我意识到我现在可以这样做了。但是我已经用Disigner制作了所有不同的图片框。现在,我使用以下代码以一种非常肮脏的方式“解决”:

` 公共道路( { drawRoad(路线1.line\u 00\R,本线\u 00\R); drawRoad(路线1.line_00_,本线_00_); drawRoad(路线1.line_01_D,本线_01_D); drawRoad(路线1.line_01_R,本线_01_R)

//等等,等等,92次

        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;
        }
    }

`解决此问题的一种方法是将pictureboxes添加到二维数组中,这样您就可以使用嵌套循环来循环控件/映射:

private PictureBox[,] _cell = new PictureBox[9,10];
在表单加载中,您可以将所有PictureBoxe添加到数组中。因此,您可以按如下方式访问它们:

for(int row = 0; row < 10; row++)
{
    for(int column=0; column < 9; column++)
    {
        // drawing logic for _cell[column, row]
    }
}
for(int行=0;行<10;行++)
{
for(int列=0;列<9;列++)
{
//_单元[列,行]的绘图逻辑
}
}

这有意义吗?你需要更多的帮助吗?

如果你正在学习的话,重组可能比走肮脏的道路要好。最终它会为你节省很多工作。谢谢,这很有帮助,但我正在学习电气工程,这只是为我们正在制造的探雷机器人制作一个无线控制接口。我们是苏反对从命令行(C)控制它,但我想让它更好一点。学习C不是主要目标。谢谢你的帮助。Erik