| 
                 分類:[C#] 
  WinXp VS2010Pro C# で行っています。
  VC6は10年ぐらいやってましたが、Win7対応のためC#もやりたいと思っています。C#は7日目です〜
  カスタムコントロールで数値(マイナス含む)だけしか入力できないコントロールを作りたい。 123と入力して先頭に”ー”を入れる→ -123 を実現するため OnKeyUpで文字列が確定してから”ー”の位置を確認し1-23などのチェックをしたいのです。 本当はOnKeyPressで現在の入力位置を確認できればいいのでしょうが またOnKeyUpで確定した文字列が得られるかも???です。
  でも、なぜOnKeyUpがオーバーライドできないのかを知りたいのです。 ソースはこんなもんです。 エラーは OnKeyUp:オーバーライドする適切なメソッドが見つかりませんでした。 宜しくお願いします。
  using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms;
  namespace WindowsFormsApplication4 {     public class NumericTextBox : TextBox     {         protected override void OnKeyPress(KeyPressEventArgs e)         {             base.OnKeyPress(e);
              String str = base.Text;               //"−”の処理             if (e.KeyChar == '-')             {                 if (str.Length == 0) return;//先頭は "−”                     if (str.Contains('-') == true)//"−”があれば入力不可                 {                     e.Handled = true;                     return;                 }             }             //これらはダメ             if (e.KeyChar != '\r' && e.KeyChar != '\t' && e.KeyChar != '\b' && (e.KeyChar < '0' || e.KeyChar > '9'))             {                 e.Handled = true;                 return;             }         }                  protected override void OnKeyUp(KeyPressEventArgs e)         {             base.OnKeyUp(e);
              String str = base.Text;         }     }  }
 
  |