【问题标题】:How can I reuse a piece of code to edit multiple pictureboxes如何重用一段代码来编辑多个图片框
【发布时间】:2011-03-30 05:26:34
【问题描述】:

我正在尝试编写一段代码来设置表单中 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#,所以我希望这不是一个愚蠢的问题。提前感谢所有帮助!

编辑

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

public Control[] Find( string key, bool searchAllChildren )

Control.ControlCollection.Find Method (http://msdn.microsoft.com)

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

` 公共无效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);

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

`

【问题讨论】:

    标签: c# edit picturebox reusability


    【解决方案1】:

    解决此问题的一种方法是将图片框添加到二维数组中,这样您就可以使用嵌套循环来遍历控件/地图:

    private PictureBox[,] _cell = new PictureBox[9,10];
    

    在表单加载中,您可以将所有图片框添加到数组中。所以你可以像这样访问它们:

    for(int row = 0; row < 10; row++)
    {
        for(int column=0; column < 9; column++)
        {
            // drawing logic for _cell[column, row]
        }
    }
    

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

    【讨论】:

    • 如果你正在学习,重组可能比走脏路更好。最后,它会为您节省大量工作。
    • 谢谢,这很有帮助,但我正在学习电气工程,这只是为我们正在制作的探雷机器人制作无线控制界面。我们应该从命令行(在 C 中)控制它,但我想让它变得更好一点。学习 C# 不是主要目标。谢谢你的帮助。埃里克
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 2021-07-24
    • 2016-06-06
    • 1970-01-01
    • 2014-12-24
    • 2019-08-11
    相关资源
    最近更新 更多