Reputation: 75
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.
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.
Upvotes: 0
Views: 2274
Reputation: 75
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 (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
Upvotes: 0
Reputation: 1980
C# doesn't have any associated values with enum cases. So to limit kinds of button to be created you can create a usual C# enum
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
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
public static void SetButtons(params ButtonType[] buttonTypes)
{
var buttons = buttonTypes.Select(ButtonFactory.CreateButton);
// add buttons to UI
}
Upvotes: 2
Reputation: 64658
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.
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];
Upvotes: 1