2012-02-08 14 views
4

の種類:リミット私はこのようなコードを持って、基本クラスのパラメータ

public static void ToUpperCase(params Control[] controls) 
{ 
    foreach (Control oControl in controls) 
    { 
     if (oControl is TextBox) 
     { 
      oControl.TextChanged += (sndr, evnt) => 
      { 
       TextBox txtControl = sndr as TextBox; 
       int pos = txtControl.SelectionStart; 
       txtControl.Text = txtControl.Text.ToUpper(); 
       txtControl.SelectionStart = pos; 
      }; 
     } 
     else if (oControl is ComboBox) 
     { 
      oControl.TextChanged += (sndr, evnt) => 
      { 
       ComboBox cmbControl = sndr as ComboBox; 
       int pos = cmbControl.SelectionStart; 
       cmbControl.Text = cmbControl.Text.ToUpper(); 
       cmbControl.SelectionStart = pos; 
      }; 
     } 
     else throw new NotImplementedException(oControl.GetType().DeclaringType.ToString() + " is not allowed."); 
    } 
} 

私だけTextBoxComboBoxタイプを受け入れるようにparams Control[] controlsを制限したいです。

私のコードはC#、フレームワーク4、VS2010Proでビルドされています。プロジェクトはWinFormsにあります。

助けてください。前もって感謝します。

+0

1つまたは他のものと同様に、TextBoxの配列またはComboBoxの配列になりますか?または、それはTextBoxとComboBoxの配列である可能性がありますか? – Zenexer

答えて

4

通常、TextBoxまたはComboBoxに共通の基本クラスを使用する必要がありますが、これは既にControlです。また、それらの基本クラスを変更することはできません。

私が思いつくのは、タイプを確認するためにDebug.Assertを追加することです。あなたは、彼らが良い共通の祖先を持っていないcan't-

foreach (var control in controls) 
    Debug.Assert((control is TextBox) || (control is ComboBox)); 
+2

これは最も実行可能な答えです。 – Zenexer

5

:よう 何か。あなたが合格できるようにしたい場合は

public static void ToUpperCase(params TextBox[] controls) 
{ 
    foreach (TextBox oControl in controls) 
     oControl.TextChanged += (sndr, evnt) => 
     { 
      TextBox txtControl = sndr as TextBox ; 
      int pos = txtControl.SelectionStart; 
      txtControl.Text = txtControl.Text.ToUpper(); 
      txtControl.SelectionStart = pos; 
     }; 
} 

public static void ToUpperCase(params ComboBox[] controls) 
{ 
    foreach (ComboBoxControl oControl in controls) 
     oControl.TextChanged += (sndr, evnt) => 
     { 
      ComboBox txtControl = sndr as ComboBox; 
      int pos = txtControl.SelectionStart; 
      txtControl.Text = txtControl.Text.ToUpper(); 
      txtControl.SelectionStart = pos; 
     }; 
} 
+1

TextBoxとComboBoxオブジェクトを同時に渡す場合は、正解であるかどうかにかかわらず、最初のコメントに対する元のポスターの回答によって異なります。 – Zenexer

+0

もちろんです。しかし、私は彼らが良い共通の祖先を持っていないと言ったように、それのための何もありません。最も基本的なクラスはコントロールです。もう1つの選択肢は、実行時に検証することです(Michael Keijzersが示唆しているように)。 –

+2

はい、本当です。これらは私が考えることができる2つの最良の答えです。 – Zenexer

4

OPTION ONE

何することができます(そしておそらく必要があります)ですが、それぞれのパラメータを取り、あなたの方法の2つのオーバーロードを行うことですあなたの関数内でテキストボックスとコンボボックスが混在していて、静的型チェックをしている場合、次のように実装することです。

public interface ISupportUpperCase { 
    event EventHandler ValueChanged; 
    void TransformValueToUpperCase(); 
} 

public class UpperCaseableTextbox : Textbox, ISupportUpperCase { 
    //TextChanged event is already here, just use it. 

    //Implement TransformValueToUpperCase in a way that suits your control 
    public void TransformValueToUpperCase() { 
    int pos = this.SelectionStart; 
    this.Text = this.Text.ToUpper(); 
    this.SelectionStart = pos; 
    } 
} 

public class UpperCaseableComboBox : ComboBox, ISupportUpperCase { 
    //TextChanged event is already here, just use it. 
    //Implement TransformValueToUpperCase in a way that suits your control 
} 

その後、あなたの関数は次のようになります。

public static void ToUpperCase(params ISupportUpperCase[] controls) 
{ 
    foreach (var oControl in controls) 
    { 
     oControl.TextChanged += (sndr, evnt) => 
     { 
      oControl.TransformValueToUpperCase(); 
     } 
    } 
} 

これを行うことによってのみ、特定のコントロールがどこかの周りにいくつかの魔法の機能、その値を大文字にする方法を知らないはずですと、あなたがより良いカプセル化で終わります。 また、他の機能を変更することなくこの機能をサポートするコントロールを簡単に導入することもできます。ので、あなたのコントロールが完全に責任を負うことになります

public interface ISupportUpperCase { 
    bool AlwaysInUpperCase { get; set } 
} 

:実際にはTWO

OPTIONは、あなたがわずかにインターフェイスを変更、同じアプローチですべてでこの機能を取り除くこと

public class UpperCaseableTextbox : Textbox, ISupportUpperCase { 

    public bool AlwaysInUpperCase { get; set } 

    //constructor 
    public UpperCaseableTextbox() { 
    this.TextChanged += (sender, args) => { 
     if (this.AlwaysInUpperCase) { 
      int pos = this.SelectionStart; 
      this.Text = this.Text.ToUpper(); 
      this.SelectionStart = pos; 
     } 
    } 
    } 
} 

この機能を使用する代わりに、コントロールが常に大文字である必要がある場合はerty、コントロールはそれ自身を管理します。

関連する問題