2012-10-23 68 views
1

我正在創建一個包含圖像和文本的大按鈕菜單。選中時,邊框位於按鈕周圍。在c#中使用控件數組.net

按鈕文本並不總是相同,並且按鈕的結果也不會點擊。

我有形象的名字,和每個按鈕在這樣的結構設置文本:(有他們四個人,但我會告訴2)

struct ConfigDevSubmenu 
    { 

     public const string SubMenuBtnText1 = ""; 
     public const string SubMenuBtnText2 = "text submenu 3 button 1"; 
     public const string SubMenuBtnText3 = "text submenu 3 button 2"; 
     public const string SubMenuBtnText4 = ""; 
     public const string SubMenuBtnImg1 = null; 
     public const string SubMenuBtnImg2 = "Settings.png"; 
     public const string SubMenuBtnImg3 = "LoadFirmware.png"; 
     public const string SubMenuBtnImg4 = null; 
     public const string SubMenuBtnBorder1 = "Borderstyle.None"; 
     public const string SubMenuBtnBorder2 = "Borderstyle.FixedSingle"; 
     public const string SubMenuBtnBorder3 = "Borderstyle.FixedSingle"; 
     public const string SubMenuBtnBorder4 = "Borderstyle.None"; 
    } 
    struct AdvancedSubmenu 
    { 
     public const string SubMenuBtnText1 = "text submenu 4 button 1"; 
     public const string SubMenuBtnText2 = "text submenu 4 button 2"; 
     public const string SubMenuBtnText3 = "text submenu 4 button 3"; 
     public const string SubMenuBtnText4 = "text submenu 4 button 4"; 
     public const string SubMenuBtnImg1 = "GenerateEncKey.png"; 
     public const string SubMenuBtnImg2 = "Monitoring.png"; 
     public const string SubMenuBtnImg3 = "AdvancedSettings.png"; 
     public const string SubMenuBtnImg4 = "GenerateConfigFile.png"; 
     public const string SubMenuBtnBorder1 = "Borderstyle.FixedSingle"; 
     public const string SubMenuBtnBorder2 = "Borderstyle.FixedSingle"; 
     public const string SubMenuBtnBorder3 = "Borderstyle.FixedSingle"; 
     public const string SubMenuBtnBorder4 = "Borderstyle.FixedSingle"; 
    } 

我不認爲這是可以做到不使用數據庫文件更容易。

創建按鈕我有這個函數,它具有作爲它應該使用哪個結構的參數,並且在switch case結構中創建每個按鈕。 但是我發現自己在這些函數中複製粘貼很多,所以這一定更容易。因此我嘗試了類似下面的內容,但這不起作用。我想知道是否有可能做出這項工作,以及我該如何做到這一點。

private void createButtons(string Struct) 
    { 
     for (int i = 1; i < 5; i++) 
     { 
      SubBtnText[i].Text = Struct.SubMenuBtnText[i]; 
      pictureBoxSubBtn[i].Image = Image.FromFile(Struct.SubMenuBtnImg[i]); 
      panelSubBtn[i].BorderStyle = Struct.SubMenuBtnBorder[i]; 

     } 
    } 

任何暗示?

回答

1

創建一個類來保存按鈕文本,圖像名稱和邊框樣式 - 例如ButtonData

創建若干列表(或數組)ButtonData,每個菜單一個。

然後,您可以迭代列表並提取數據。

public class ButtonData 
{ 
    public ButtonData(string text, string image, BorderStyle border) 
    { 
     Text = text; 
     Image = image; 
     Border = border; 
    } 

    public string Text { get; private set; } 
    public string Image { get; private set; } 
    public BorderStyle Border { get; private set; } 
} 

var devMenuData = new List<ButtonData> { 
            new ButtonData("", null, "Borderstyle.None"), 
            new ButtonData("text submenu 3 button 1", 
               "Settings.png", 
               Borderstyle.FixedSingle), 
            ... 
             }; 

你的函數將是這樣的:

private void CreateButtons(IEnumerable<ButtonData> data) 
{ 
    foreach (var buttonData in data) 
    { 
     SubBtnText[i].Text = buttonData.Text; 
     pictureBoxSubBtn[i].Image = Image.FromFile(buttonData.Image); 
     panelSubBtn[i].BorderStyle = buttonData.Border; 
    } 
} 

上述修正功能將不能爲這樣的工作,因爲.NET沒有控件數組。您可以創建另一個列表/數組來迭代或索引以實現此功能。

+0

這看起來真棒。我現在就試試看! – 2pietjuh2

+0

對此做了一些調整,但我不確定這比我更好,但我從中學到了:) – 2pietjuh2

0

reflection

class ButtonParameters 
{ 
    public string SubMenuBtnText = string.Empty; 
    public string SubMenuBtnImg = string.Empty; 
    public string SubMenuBtnBorder = string.Empty; 
} 

public Dictionary<int, ButtonParameters> CreateParameters(Type type) 
{ 
    FieldInfo[] fieldInfos = type.GetFields(
     BindingFlags.Public | BindingFlags.Static); 
    Dictionary<int, ButtonParameters> parameters = new Dictionary<int, ButtonParameters>(); 
    foreach (FieldInfo fieldInfo in fieldInfos) 
    { 
     if (fieldInfo.Name.Contains("SubMenuBtnText")) 
     { 
      int index = Convert.ToInt32(fieldInfo.Name.Substring(14)); 
      if (!parameters.ContainsKey(index)) 
      { 
       parameters.Add(index, new ButtonParameters()); 
      } 
      parameters[index].SubMenuBtnText = (string)fieldInfo.GetValue(null); 
     } 
     else if (fieldInfo.Name.Contains("SubMenuBtnImg")) 
     { 
      int index = Convert.ToInt32(fieldInfo.Name.Substring(13)); 
      if (!parameters.ContainsKey(index)) 
      { 
       parameters.Add(index, new ButtonParameters()); 
      } 
      parameters[index].SubMenuBtnImg= (string)fieldInfo.GetValue(null); 
     } 
     else if (fieldInfo.Name.Contains("SubMenuBtnBorder")) 
     { 
      int index = Convert.ToInt32(fieldInfo.Name.Substring(16)); 
      if (!parameters.ContainsKey(index)) 
      { 
       parameters.Add(index, new ButtonParameters()); 
      } 
      parameters[index].SubMenuBtnBorder= (string)fieldInfo.GetValue(null); 
     } 
    } 
    return parameters; 
} 

private void createButtons() 
{ 
    Dictionary<int, ButtonParameters> buttons = CreateParameters(typeof(AdvancedSubmenu)); 
    for (int i = 1; i < 5; i++) 
    { 
     SubBtnText[i].Text = buttons[i].SubMenuBtnText; 
     pictureBoxSubBtn[i].Image = Image.FromFile(buttons[i].SubMenuBtnImg); 
     panelSubBtn[i].BorderStyle = buttons[i].SubMenuBtnBorder; 
    } 
}