2011-03-30 44 views
0

我試圖編寫一段代碼來設置我的表單中的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); 012RdrawRoad(route1.line_01_D,this.line_01_D); 012RdrawRoad(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; 
     } 
    } 

`

回答

0

解決這個的一種方式是通過將pictureboxes到2維陣列這樣就可以通過控制/地圖使用嵌套循環來循環:

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

在表單加載可以將所有PictureBox添加到數組中。所以你可以像這樣訪問它們:

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

這有道理嗎?你需要更多的幫助嗎?

+0

如果你正在學習,重構可能會比骯髒的方式更好。最終它會爲你節省很多工作。 – 2011-03-30 20:16:15

+0

謝謝,這很有幫助,但我正在學習Electrical Engeneering,這只是爲我們製作的地雷探測機器人制作一個無線控制界面。我們應該從命令行(C語言)來控制它,但是我想讓它好一點。學習C#不是主要目標。 感謝您的幫助。 Erik – 2011-03-31 21:28:12

相關問題