Announcement

Collapse
No announcement yet.

WPF Textbox KeyUp und PreviewKeyUp Eventverarbeitung zu langsam

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • WPF Textbox KeyUp und PreviewKeyUp Eventverarbeitung zu langsam

    Meine Umgebung: .Net 4.5.1, VS 2015, WPF

    Ich möchte gerne während der Eingabe in eine Textbox die Zeichen abfangen und nur Nummern zulassen.

    Ich benutze dazu die Events KeyUp und PreviewKeyUp (und KeyDown).

    Soweit funktioniert es schon, allerdings werden bei einer schnelleren Schreibweise dennoch die Buchstaben übernommen. Ich habe es im Event auch mit BeginInit und EndInit versucht, alles vergeblich. Auch mit e.Handled = true kein Ergebnis.

    Kann mir jemand helfen? Ich habe auch ein Testprogramm im Anhang.

    Mein Code:
    [highlight=csharp]
    ...
    ...
    private void textBox_KeyUp(object sender, KeyEventArgs e)
    {
    textBox.BeginInit();
    if (!IstNummernEingabe(e.Key))
    {
    if (_textBoxLaenge != textBox.Text.Trim().Length)
    textBox.Text = _textBoxKeyDown;
    }
    textBox.CaretIndex = textBox.Text.Length;
    label.Content = textBox.Text;
    textBox.EndInit();
    }

    private void textBox_PreviewKeyUp(object sender, KeyEventArgs e)
    {

    textBox.BeginInit();
    if (!IstNummernEingabe(e.Key))
    {
    if (_textBoxLaenge != textBox.Text.Trim().Length)
    textBox.Text = _textBoxKeyDown;
    }
    textBox.CaretIndex = textBox.Text.Length;
    label.Content = textBox.Text;
    textBox.EndInit();
    }

    private void textBox_KeyDown(object sender, KeyEventArgs e)
    {
    _textBoxLaenge = textBox.Text.Length;
    _textBoxKeyDown = textBox.Text.Trim().ToString();
    }


    private bool IstNummernEingabe(Key k)
    {
    bool returnwert = false;
    switch (k)
    {
    case Key.D0:
    case Key.D1:
    case Key.D2:
    case Key.D3:
    case Key.D4:
    case Key.D5:
    case Key.D6:
    case Key.D7:
    case Key.D8:
    case Key.D9:
    case Key.NumPad0:
    case Key.NumPad1:
    case Key.NumPad2:
    case Key.NumPad3:
    case Key.NumPad4:
    case Key.NumPad5:
    case Key.NumPad6:
    case Key.NumPad7:
    case Key.NumPad8:
    case Key.NumPad9:
    case Key.OemPeriod:
    case Key.Back:
    case Key.Tab:
    case Key.Delete:
    returnwert = true;
    break;
    }

    return returnwert;
    }
    [/highlight]

    Gebe ich die Zeichen langsam ein gibt es keine Probleme und er erkennt alles, ist es schnell, dann werden auch andere Zeichen mit angezeigt.

    Vielleicht benutze ich die falschen Events. Unter Forms hatte ich nie so ein Problem.

    Vielen Dank für Eure Unterstützung.

    Viele Grüße
    Attached Files

  • #2
    Hy, ich habe hier einen Workaround über TextChanged.
    [highlight=csharp]
    ...
    private const string zahlen = "1234567890.";
    private const string wandelnInPunkt = ",+:;";

    ...


    private void textBox_TextChanged(object sender, TextChangedEventArgs e)
    {
    if (string.IsNullOrEmpty(textBox.Text)) return; //jetzt is es leer

    //vorher noch zeichen wandeln
    if(wandelnInPunkt.Contains(textBox.Text.Substring( textBox.Text.Length - 1)))
    textBox.Text = textBox.Text.Substring(0, textBox.Text.Length - 1) + ".";

    if (!zahlen.Contains(textBox.Text.Substring(textBox.T ext.Length - 1)))
    textBox.Text = textBox.Text.Substring(0, textBox.Text.Length - 1);

    textBox.CaretIndex = textBox.Text.Length;

    }

    [/highlight]

    Allerdings bin ich über eine Alternative für KeyUp dankbar. Das hier funktioniert einwandfrei. Ich kann aber nicht abschätzen ob hier noch weitere Probleme auftauchen könnten.

    Comment

    Working...
    X