我目前有以下内容,我认为这非常麻烦。我通常在表单上创建不同的按钮,并通过传递一组按钮来完成它。每个按钮都有一组属性可以动态设置按钮。
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 :(得分:2)
C#与枚举案例没有任何关联值。因此,要限制要创建的按钮类型,您可以创建通常的C#枚举
enum ButtonType { OK, Cancel, Yes, No }
如果可以方便地创建一些工厂,它将为每个枚举值
创建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;
}
}
然后你可以创建SetButtons
,它将为每个传递的类型创建和添加按钮
public static void SetButtons(params ButtonType[] buttonTypes)
{
var buttons = buttonTypes.Select(ButtonFactory.CreateButton);
// add buttons to UI
}
答案 1 :(得分: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" },
// ...
]
你不需要在问题中使用SetButton,因为按钮已经完成。
我不知道这是否有助于解决您的问题,因为您在问题中对此信息实际上没有任何意义。
要从按钮类型导航到按钮配置,您可以使用字典
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];
答案 2 :(得分: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();
由于