[英]Reduce if else series in windows form checkedlistbox c#
在C#中,在Windows窗體中,我使用了一個checkedListBox。 到目前為止,在我的checkedListBox中,我有3個項目。 和3系列的if / elseif語句。
我想采用一種巧妙的方式,根據所選項目的組合采取具體措施。 我在想一棵二叉樹。
二叉樹可以嗎,或者在checkListListBox中有我不知道的方法/屬性可以幫助我?
下面是當前代碼:
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (checkedListBox1.GetItemCheckState(0) == CheckState.Checked)
{
do_action_item0(current_parameters);
}
else if(checkedListBox1.GetItemCheckState(0) != CheckState.Checked)
{
undo_action_item0(previous_parameters);
}
if (checkedListBox1.GetItemCheckState(1) == CheckState.Checked)
{
do_action_item1(current_parameters);
}
else if (checkedListBox1.GetItemCheckState(1) != CheckState.Checked)
{
undo_action_item1(previous_parameters);
}
if (checkedListBox1.GetItemCheckState(2) == CheckState.Checked)
{
do_action_item2(current_parameters);
}
else if (checkedListBox1.GetItemCheckState(2) != CheckState.Checked)
{
undo_action_item2(previous_parameters);
}
}
鑒於您在復選框列表中有多個選擇。 您可以嘗試針對此問題實施策略模式。 基本上,您需要為每個變體創建策略。 您可以在此處閱讀更多內容。
通過實現這一點,您將遵循開放關閉原則,這意味着無論以后要添加多少項,您現有的代碼都不需要修改,而是可以擴展。
首先創建一個這樣的枚舉:
public enum demo
{
none = 0,
AOnly = 1,
BOnly = 2,
AandB = 3,
COnly = 4,
AandC = 5,
BandC = 6,
ABC = 7
}
現在,在您的動作事件中,您可以填寫演示枚舉的實例,從而:
demo myDemo = demo.none;
if (checkedListBox1.CheckedItems.Contains("Option A"))
{
myDemo = myDemo | demo.AOnly;
}
if (checkedListBox1.CheckedItems.Contains("Option B"))
{
myDemo = myDemo | demo.BOnly;
}
if (checkedListBox1.CheckedItems.Contains("Option C"))
{
myDemo = myDemo | demo.COnly;
}
現在您可以擁有一個switch語句,這樣:
switch (myDemo)
{
case demo.AOnly:
MessageBox.Show("A Only");
break;
case demo.BOnly:
MessageBox.Show("B Only");
break;
case demo.COnly:
MessageBox.Show("C Only");
break;
case demo.AandB:
MessageBox.Show("A and B");
break;
case demo.AandC:
MessageBox.Show("A and C");
break;
case demo.BandC:
MessageBox.Show("B and C");
break;
case demo.ABC:
MessageBox.Show("ABC");
break;
}
這說明了“或”項一起使用。 從三個選項中,您可以獲得8種可能性(2 ^ 3)。 您需要做的就是確保您的“原理”選項是枚舉中的2的冪。
編輯
標志屬性與與此枚舉中2的冪的使用緊密相關。 例如:
[FlagsAttribute]
public enum demo2
{
none = 0,
A = 1,
B = 2,
C = 4
}
這允許您以與以前相同的方式創建枚舉的實例,但是現在您可以使用以下語法:
bool hasOptB = myDemo.HasFlag(demo2.B);
根據您的情況,這可能會更有用。
我建議您引入一個標志枚舉,它表示CheckedListBox
的項目。
[Flags]
enum ActionItemToPerform {
None = 0, ActionItem0 = 1, ActionItem1 = 2, ActionItem2 = 4
}
然后根據以下枚舉值為CheckedListBox
創建項目:
CheckedListBox clb = new CheckedListBox();
foreach(var possibleAction in Enum.GetValues(typeof(ActionItemToPerform))){
clb.Items.Add(possibleAction, false);
}
基於這些標志與要執行的Action的綁定封裝在一個類中:
public class ConditionalDoAction {
// initialize this with the conditions when this shall be performed
public ActionItemToPerform Condition { get; set; }
// TODO: tweak delegate to match your method signatures...
public Action<CurrentParameters> Do { get; set; }
}
您可以初始化這些動作和條件的清單。
var configuredActions = new List<ConditionalDoAction>();
configuredActions.Add(new ConditionalDoAction {
Condition = ActionItemToPerform.ActionItem0,
Do = do_action_item0
});
您可以通過組合標志來提供條件組合,例如
Condition = ActionItemToPerform.ActionItem0 & ActionItemToPerform.ActionItem1
當您讀取選中的項目時,請創建累積標志狀態tally
並與配置的操作進行比較:
btn.Click += delegate {
ActionItemToPerform tally = ActionItemToPerform.None;
foreach (var selectedAction in clb.CheckedItems) {
tally |= selectedAction;
}
foreach(var configuredAction in configuredActions) {
if (configuredAction.HasFlag(tally)) {
configuredAction.Do(current_parameters);
}
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.