Einzelnen Beitrag anzeigen

Elvis

Registriert seit: 25. Nov 2005
Ort: München
1.909 Beiträge
 
Delphi 2010 Professional
 
#7

Re: nur Ziffern in textBox

  Alt 6. Dez 2005, 02:47
Hier ein minimalistischer, generischer Ansatz: (weiß ja nicht, ob du integer, double, ... nehmen willst )
Code:
public class Int32TextBox : TypedTextBox<int> { }
public class DecimalTextBox : TypedTextBox<double> { }
public class DoubleTextBox : TypedTextBox<decimal> { }

public class TypedTextBox<T> : TextBox
   where T : IEquatable<T>
{
   T value;

   [Category("Appearance")]
   public T Value
   {
      get { return this.value; }
      set
      {
         if (!this.value.Equals(value))
         {
            this.value = value;
            base.Text = this.value.ToString();
         }
      }
   }

   [Browsable(false)]
   public override string Text
   {
      get { return base.Text; }
      set { base.Text = value; }
   }

   protected override void OnTextChanged(EventArgs e)
   {
      try
      {
         Value = (T)Convert.ChangeType(base.Text, typeof(T));
      }
      catch (Exception ex)
      {
         Undo();
         throw new InvalidCastException("bla bla", ex);
      }
   }

   public TypedTextBox()
   {
      base.Text = this.value.ToString();
   }
}
Ein vorkompilierte RegEx in Christians Stil könnte aber auch ganz nützlich sein.
Robert Giesecke
  Mit Zitat antworten Zitat