2016-04-28 93 views
0

我目前有以下,我認爲這是非常繁瑣。我通常在表單上創建不同的按鈕,並通過傳遞一組按鈕來完成它。每個按鈕都有一組屬性來即時設置按鈕。多維枚舉Array列表

public enum BtnType { bOK , bCancel, bYes, bNo }; 

public enum bOk { Name = "OKBtn", Text = "OK" } 
public enum bCancel { Name = "CancelBtn", Text = "Cancel" } 
public enum bYes { Name = "YesBtn", Text = "Cancel" } 

public static void SetButtons(BtnType[] _btnType, string onClick) 
     { 
      foreach (int i in _btnType) 
      { 
       switch (_btnType[i]) 
       { 
        case BtnType.bOK: 
         { 
          btnp.Name = bOk.Name; 
          btnp.Text = bOk.Text; 
         } 
         break; 
        case BtnType.bCancel: 
         { 
          btnp.Name = bCancel.Name; 
          btnp.Text = bCancel.Text; 
         } 
         break; 
        case BtnType.bYes: 
         { 
          btnp.Name = bYes.Name; 
          btnp.Text = bYes.Text; 
         } 
         break; 
       } 

      } 

然後我這樣稱呼它,它會爲我創建2個按鈕。

SetButtons(new BtnType[] { BtnType.bYes, BtnType.bNo }); 

我想完成的是以下,我找不到解決方案。

public enum BtnType { bOK = { Name = "OKBtn", Text = "OK" }, 
         bCancel = {}, 
         bYes = {}, 
         bNo = {} }; 

public static void SetButtons(BtnType[] _btnType, string onClick) 
     {    
      foreach (int i in _btnType) 
      { 
       btnp.Name = _btnType[i].Name; 
       btnp.Text = _btnType[i].Text;     
      } 
     } 

謝謝。

+0

這看起來不像c#。它甚至編譯? –

+0

您不能將值指定給C#中的枚舉個案。你需要使用類而不是枚舉。此外,你的循環迭代拋出值是完全錯誤的。用'foreach'你不會得到索引,而是一個'BtnType'對象。 –

+0

字典可能是不錯的。 –

回答

0

感謝您的答案。它工作得很好,我從每個答案中使用一點來達到我的要求。

這是我做過什麼:

public enum ButtonType { mbOK, mbCancel, mbYes, mbNo }; 

class ButtonConfig 
    { 
     public string Name { get; set; } 
     public string Text { get; set; } 
     public string ModelResult { get; set; } 
    } 

private static Dictionary<ButtonType, ButtonConfig> ButtonsSettings = new Dictionary<ButtonType, ButtonConfig>() 
     { 
      {ButtonType.mbOK, new ButtonConfig { Name = "btnOK", Text = "OK", ModelResult = "mrOk"}}, 
      {ButtonType.mbCancel, new ButtonConfig { Name = "btnCancelBtn", Text = "Cancel", ModelResult = "mrCancel" }}, 
      {ButtonType.mbYes, new ButtonConfig { Name = "btnYes", Text = "Yes", ModelResult = "mrYes" }}, 
      {ButtonType.mbNo, new ButtonConfig { Name = "btnNo", Text = "No", ModelResult = "mrNo"}}, 

然後我有我的方法

public static XXX XXX(ButtonType[] _ButtonType, string onClick) 

有了我這樣調用:

XXX(new ButtonType[] { ButtonType.mbYes, ButtonType.mbNo },"onCLink"); 

,然後我使用foreach循環

foreach (ButtonType button in _ButtonType) 
{ 
    var Button = ButtonsSettings[button]; 
    htmlHelper.DevExpress().Button(buttonSettings => 
    { 
     buttonSettings.Name = Button.Name; 
     buttonSettings.ControlStyle.CssClass = "button"; 
     buttonSettings.Width = 80; 
     buttonSettings.Text = Button.Text; 
     buttonSettings.ClientSideEvents.Click = String.Format("function(s, e) {{" + onClick + "(\"{0}\"); pcMessage.Hide();}}", Button.ModelResult); 
}).Render(); 

謝謝

1

,你需要的不是一個枚舉,你需要真正的類來把你的按鈕配置:

class ButtonConfig 
{ 
    public string Name { get; set; } 
    public string Text { get; set; } 
} 

var myButtons = new [ 
    new ButtonConfig { Name = "OKBtn", Text = "OK" }, 
    new ButtonConfig { Name = "CancelBtn", Text = "Cancel" }, 
    // ... 
] 

,你在問題中有它你不需要SetButtons,因爲按鈕都已經完成。

我不知道這是否有助於解決您的問題,因爲您對問題中的這些信息實際上不想做什麼。

從一個鈕釦式的按鈕配置導航,您可以使用字典

enum ButtonType 
{ 
    Ok, 
    Cancel, 
    Whatever 
} 

Dictionary<ButtonType, ButtonConfig> buttons = new Dictionary<ButtonType, ButtonConfig>() 
{ 
    {ButtonType.Ok, new ButtonConfig { Name = "OKBtn", Text = "OK" }}, 
    {ButtonType.Cancel, new ButtonConfig { Name = "CancelBtn", Text = "Cancel" }}, 
    {ButtonType.Whatever, new ButtonConfig { Name = "WhateverBtn", Text = "Whatever" }}, 
}; 

訪問:

var okButton = buttons[ButtonType.Ok]; 
+0

謝謝@StefanSteinegger –

2

C#沒有任何相關的值與枚舉的情況。因此,要限制要創建各種按鈕,你可以創建一個通常的C#枚舉

enum ButtonType { OK, Cancel, Yes, No } 

時候會很方便地創建一些工廠,將每個枚舉值

static class ButtonFactory 
{ 
    public static Button CreateButton(ButtonType buttonType) 
    { 
     switch (buttonType) 
     { 
      case ButtonType.OK: 
       return CreateButton("OKBtn", "OK"); 

      // process other button types 
     } 
    } 

    private static Button CreateButton(string name, string text) 
    { 
     var button = new Button(); 
     button.Name = name; 
     button.Text = text; 
     return button; 
    } 
} 

的創建UI按鈕然後你可以創建SetButtons這將創建並添加按鈕到UI爲每個傳遞類型

public static void SetButtons(params ButtonType[] buttonTypes) 
{ 
    var buttons = buttonTypes.Select(ButtonFactory.CreateButton); 
    // add buttons to UI 
} 
+0

Thanks @Maxim Kosov –