[英]Multi dimensional enum Array list
I have currently the following and I think it is very cumbersome. 我目前有以下内容,我认为这很麻烦。 I generically create different buttons on a form and accomplish it by passing an array of buttons. 我通常在窗体上创建不同的按钮,并通过传递按钮数组来完成它。 Each button have a set of properties to setup the button on the fly. 每个按钮都有一组属性来动态设置按钮。
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;
}
}
Then I call it like this and it will create 2 buttons for me. 然后我这样称呼它,它将为我创建2个按钮。
SetButtons(new BtnType[] { BtnType.bYes, BtnType.bNo });
What I would like to accomplish is the following and I cannot find a solution. 我要完成的工作如下,但找不到解决方案。
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;
}
}
Thank you. 谢谢。
C# doesn't have any associated values with enum cases. C#与枚举案例没有任何关联的值。 So to limit kinds of button to be created you can create a usual C# enum 因此,为了限制要创建的按钮种类,您可以创建一个普通的C#枚举
enum ButtonType { OK, Cancel, Yes, No }
When it would be convenient to create some factory which will create UI button for each of the enum value 什么时候方便创建一些工厂,该工厂将为每个枚举值创建UI按钮
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;
}
}
And then you can create SetButtons
which will create and add button to UI for each of the passed type 然后,您可以创建SetButtons
,该按钮将为每个传递的类型创建按钮并将按钮添加到UI
public static void SetButtons(params ButtonType[] buttonTypes)
{
var buttons = buttonTypes.Select(ButtonFactory.CreateButton);
// add buttons to UI
}
What you need is not an enum, you need real classes to put your button configuration in: 您所需要的不是枚举,您需要真实的类来将按钮配置放入:
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" },
// ...
]
You don't need SetButtons as you have it in the question, because the buttons are already complete. 您不需要问题中的SetButtons,因为按钮已经完成。
I don't know if this helps to solve your problem, because you don't have anything about what you actually want to do with this information in the question. 我不知道这是否有助于解决您的问题,因为您对问题中的此信息实际上不打算做什么。
To navigate from a button type to the button configuration, you may use a dictionary 要从按钮类型导航到按钮配置,可以使用字典
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" }},
};
Access: 访问:
var okButton = buttons[ButtonType.Ok];
Thanks for your answers. 感谢您的回答。 It is working very well and I use a bit from each answer to get to my requirement. 它工作得很好,我在每个答案中都花了一点时间来满足我的要求。
This is what I did: 这是我所做的:
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"}},
I then have my method 然后我有我的方法
public static XXX XXX(ButtonType[] _ButtonType, string onClick)
With I call like this: 我这样打电话:
XXX(new ButtonType[] { ButtonType.mbYes, ButtonType.mbNo },"onCLink");
and then I use a foreach loop 然后我使用一个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();
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.