2016-09-05 9 views
0

誰かがこのコードを短縮することはできますか? 14個のボタンと8個のテキストボックスがあります。テキストボックスが空でない場合はアクションが実行され、空でない場合は、テキストボックス内の文字に対応するボタンが再度表示され、テキストボックスが空になります。Cでif-else-if文を短縮する方法#

private void txt1_Click(object sender, EventArgs e) 
{ 
     if (txt1.Text == "J") 
     { 
      txt1.Text = ""; 
      btn1.Visible = true; 
     } 
     else if (txt1.Text == "M") 
     { 
      txt1.Text = ""; 
      btn2.Visible = true; 
     } 
     else if (txt1.Text == "Y") 
     { 
      txt1.Text = ""; 
      btn3.Visible = true; 
     } 
     else if (txt1.Text == "E") 
     { 
      if (btn4.Visible == true) 
      { 
       txt1.Text = ""; 
       btn5.Visible = true; 
      } 
      else 
      { 
       txt1.Text = ""; 
       btn4.Visible = true; 
      } 
     } 
     else if (txt1.Text == "Q") 
     { 
      txt1.Text = ""; 
      btn6.Visible = true; 
     } 
     else if (txt1.Text == "L") 
     { 
      if (btn7.Visible == true) 
      { 
       txt1.Text = ""; 
       btn10.Visible = true; 
      } 
      else 
      { 
       txt1.Text = ""; 
       btn7.Visible = true; 
      } 
     } 
     else if (txt1.Text == "B") 
     { 
      txt1.Text = ""; 
      btn8.Visible = true; 
     } 
     else if (txt1.Text == "C") 
     { 
      txt1.Text = ""; 
      btn9.Visible = true; 
     } 
     else if (txt1.Text == "P") 
     { 
      txt1.Text = ""; 
      btn11.Visible = true; 
     } 
     else if (txt1.Text == "I") 
     { 
      txt1.Text = ""; 
      btn12.Visible = true; 
     } 
     else if (txt1.Text == "K") 
     { 
      txt1.Text = ""; 
      btn13.Visible = true; 
     } 
     else if (txt1.Text == "O") 
     { 
      txt1.Text = ""; 
      btn14.Visible = true; 
     } 
} 
+0

'switch case function'の使用を検討することができます。 txt1.Textのクリアランスを 'else-if'関数の外に置いてください。 – Mark

答えて

4

に慣れる:

public Button Click(String txt) { 
    switch(txt) { 
     case "J": 
      return btn1; 
     case "M": 
      return btn2; 
     case "Y": 
      return btn3; 
     case "E": 
      return (btn4.Visible ? btn5 : btn4); 
     case "Q": 
      return btn6; 
     case "L": 
      return (btn7.Visible ? btn10 : btn7); 
     case "B": 
      return btn8; 
     case "C": 
      return btn9; 
     case "P": 
      return btn11; 
     case "I": 
      return btn12; 
     case "K": 
      return btn13; 
     case "O": 
      return btn14; 
    } 
    return null; 
} 

、その後、あなたが呼び出しますそれ:

var button = Click(txt1.Text); 
if(button != null) { 
    button.Visible = true; 
    txt1.Text = ""; 
} 

ただし、btnの変数にローカルスコープがある場合、あなただけのようにインラインFunc<String,Button>デリゲートを定義することができる代わりに、方法のより:

Func<String, Button> Click = txt => { 
    switch(txt) { 
     ... 
    } 
}; 
+0

"if(btn4.Visible == true)よりも が返されます。btn5; else return btn4;" – EJoshuaS

+0

2番目のコードはどこに置くのですかpublicの内側または外側Buttonをクリックします(String txt)? – PlusUltra

+0

よく@PlusUltra 2番目のコードスニペットがメソッドを呼び出す(または、そのルートに行く場合は委任する)ので、外に出る必要があります。 – Danny

0

これを見てください:私は、あなたがやっていることをもっと論理的な方法で考えると思います。

私の経験から、コード内のif-elseの巨大なチェーンはどこかに論理的な問題があることを示しています。また

、すべてbtn変数は、クラスの状態の一部であり、あなたがそうのようなメソッドを宣言することができると仮定すると、switch文

1

をあなたはまだ特別なケース("E""L")を処理する必要があると思いますが、あなたはあなたにできるようになるDictionary<string, Button>を使用することができます検索を行います:

var buttonDictionary = new Dictionary<string, Button>(); 
buttonDictionary["J"] = btn1; 
buttonDictionary["M"] = btn2; 
//etc... 

if (buttonDictionary.Keys.Contains(txt1.Text)) 
{ 
    txt1.Text = ""; 
    buttonDictionary[txt1.Text].Visible = false; 
} 

これは、繰り返しコードのほとんどを削減します。

関連する問題