Feedback

Eingabe auf nummerische Texteingabe in Textbox prüfen

Sprache: C#

Es gibt zwar das eine oder andere abgewandelte Control was diese Aufgabe schon erfüllt, dennoch, vielleicht braucht man mal die "Quick and Dirty Lösung" für die Eingabe Prüfung. Um es richtig "Snipped like" zu machen muss hier auch noch der Visual Basic Namespace integriert werden. Dieses Snipped in das "TextChanged" Ereignis einfügen.
            Control CTRL = this.ActiveControl;
            if (CTRL != null)
            {
                if (Microsoft.VisualBasic.Information.IsNumeric(CTRL.Text) == false)
                {
                    foreach (Char C in CTRL.Text.ToCharArray())
                    {
                        if (Microsoft.VisualBasic.Information.IsNumeric(C) == false)
                        {
                            TextBox TXTBX = CTRL as TextBox;
                            String MyText = TXTBX.Text;
                            CTRL.Text = MyText.Replace(Convert.ToString(C), String.Empty);
                            TXTBX.Select(TXT_SMTPIN.Text.Length, 0);
                        }
                    }
                }
            }
            Control CTRL = this.ActiveControl;
            if (CTRL != null)
            {
                if (Microsoft.VisualBasic.Information.IsNumeric(CTRL.Text) == false)
                {
                    foreach (Char C in CTRL.Text.ToCharArray())
                    {
                        if (Microsoft.VisualBasic.Information.IsNumeric(C) == false)
                        {
                            TextBox TXTBX = CTRL as TextBox;
                            String MyText = TXTBX.Text;
                            CTRL.Text = MyText.Replace(Convert.ToString(C), String.Empty);
                            TXTBX.Select(TXT_SMTPIN.Text.Length, 0);
                        }
                    }
                }
            }

3 Kommentare

  1. Hi,
    sorry aber ich finde den Snippet nur dirty. Quick & Dirty sieht so vielleicht aus:
    [code]
    void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
    if (!Double.TryParse(this.Text, out outVar))
    {
    this.Text = oldValue;
    this.Select(this.Text.Length, 0);
    }
    else
    {
    oldValue = this.Text;
    }
    }
    [/code]

    Gruß
    Christoph

  2. Oh, da habe ich was falsches kopiert! Hier noch mal aus neue:
    [code]
    private Double outVar = 0;
    private String oldValue = „“;
    void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
    if (!Double.TryParse(textBox1.Text, out outVar))
    {
    textBox1.Text = oldValue;
    textBox1.Select(textBox1.Text.Length, 0);
    }
    else
    {
    oldValue = textBox1.Text;
    }
    }

    [/code]

  3. Hallo,

    wie wäre es hiermit:
    [code]
    private void textBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    // Nur Zahlen und die Rückstelltaste in einer TextBox zulassen
    if („1234567890,b“.IndexOf(e.KeyChar.ToString()) < 0) { e.Handled = true; } } [/code] Gruß Martin