2009-05-11 22 views
0

編集可能なComboBoxのTextInputプロパティをmaxCharsに設定したいとします。私は現在、変更イベントを使用して文字のセット番号にテキストをトリミングしています:これはやり過ぎのように感じている編集可能なComboBoxでTextInputの「maxChars」を設定するにはどうすればよいですか?

private function nameOptionSelector_changeHandler(event:ListEvent):void 
{ 
    nameOptionSelector.text = nameOptionSelector.text.substr(0, MAX_LENGTH); 
} 

答えて

1

あなたはComboBoxを拡張し、内部TextInputのデフォルトmaxChars値をオーバーライドすることができます....これを行うには良い方法があるように持っています。動的に設定する必要がある場合は、拡張クラスのプロパティを設定するpublicメソッドを追加できます。 Stigglerの提案を使用して

+0

これは私の元々の考えであり、非常に良いアイデアであることが判明しました。以下の完全な解決策... –

0

は、ここで私が実装完全なソリューションは、次のとおりです。

package 
{ 
    import mx.controls.ComboBox; 

    public class ComboBoxWithMaxChars extends ComboBox 
    { 
     public function ComboBoxWithMaxChars() 
     { 
      super(); 
     } 

     private var _maxCharsForTextInput:int; 

     public function set maxCharsForTextInput(value:int):void 
     { 
      _maxCharsForTextInput = value; 

      if (super.textInput != null && _maxCharsForTextInput > 0) 
       super.textInput.maxChars = _maxCharsForTextInput; 
     } 

     public function get maxCharsForTextInput():int 
     { 
      return _maxCharsForTextInput; 
     } 

     override public function itemToLabel(item:Object):String 
     { 
      var label:String = super.itemToLabel(item); 

      if (_maxCharsForTextInput > 0) 
       label = label.substr(0, _maxCharsForTextInput); 

      return label; 
     } 
    } 
} 
+0

おそらく 'public function itemToLabel(item:Object、... rest):String'プロトタイプのため、これをコンパイルしようとしたときに"互換性のないオーバーライド "が発生しました。私は本当にそれらの種類のパラメータを使って何をすべきか分からないでしょう(私の推測は何もできないだろうということです)。どのSDKを使用していますか?私は3.5を使用しています – Opux

2

私の選択肢は直接保護にtextInputを使用することです。この方法では、通常のTextFieldの場合と同様に、GUIビルダまたはコードで「maxChars」プロパティを設定できます。ゼロはmaxCharsの有効な値であり、無制限の文字を示します。 TextInputオブジェクトが存在する前にmaxCharsを設定しないようにするには、.childrenCreated()のオーバーライドが必要です。

package my.controls 
{ 
    import mx.controls.ComboBox; 

    public class EditableComboBox extends ComboBox 
    { 
     public function EditableComboBox() 
     { 
      super(); 
     } 

     private var _maxChars:int = 0; 

     override protected function childrenCreated():void 
     { 
      super.childrenCreated(); 

      // Now set the maxChars property on the textInput field. 
      textInput.maxChars = _maxChars; 
     } 

     public function set maxChars(value:int):void 
     { 
      _maxChars = value; 
      if (textInput != null && value >= 0) 
       textInput.maxChars = value; 
     } 

     public function get maxChars():int 
     { 
      return textInput.maxChars; 
     } 

    } 
} 
関連する問題